Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/tokens/common.ts
3294 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
export class RateLimiter {
7
private _lastRun: number;
8
private readonly _minimumTimeBetweenRuns: number;
9
10
constructor(public readonly timesPerSecond: number = 5) {
11
this._lastRun = 0;
12
this._minimumTimeBetweenRuns = 1000 / timesPerSecond;
13
}
14
15
public runIfNotLimited(callback: () => void): void {
16
const now = Date.now();
17
if (now - this._lastRun >= this._minimumTimeBetweenRuns) {
18
this._lastRun = now;
19
callback();
20
}
21
}
22
}
23
24