microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/executionsLimiter.ts
17lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | /* This class can be used to limit how often can some code be executed e.g. Max once every 10 seconds */ |
| 5 | export class ExecutionsLimiter { |
| 6 | private executionToLastTimestamp: {[id: string]: number} = {}; |
| 7 | |
| 8 | public execute(id: string, limitInSeconds: number, lambda: () => void) { |
| 9 | const now = new Date().getTime(); |
| 10 | |
| 11 | const lastExecution = this.executionToLastTimestamp[id] || 0; |
| 12 | if (now - lastExecution >= limitInSeconds * 1000) { |
| 13 | this.executionToLastTimestamp[id] = now; |
| 14 | lambda(); |
| 15 | } |
| 16 | } |
| 17 | } |