microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
78c2aaee56e1edfea55cdebbdccbf5541dff7beb

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/executionsLimiter.ts

39lines · modeblame

7cc67271digeff10 years ago1// 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 */
5export class ExecutionsLimiter {
34472878RedMickey5 years ago6private executionToLastTimestamp: { [id: string]: number } = {};
7cc67271digeff10 years ago7
34472878RedMickey5 years ago8public execute(id: string, limitInSeconds: number, lambda: () => void): void {
7cc67271digeff10 years ago9const now = new Date().getTime();
10
11const lastExecution = this.executionToLastTimestamp[id] || 0;
12if (now - lastExecution >= limitInSeconds * 1000) {
13this.executionToLastTimestamp[id] = now;
14lambda();
15}
16}
17}
c2bf3c4fdigeff10 years ago18
19export class ExecutionsFilterBeforeTimestamp {
20private static MILLISECONDS_IN_ONE_SECOND = 1000;
21
22private sinceWhenToStopFiltering: number;
23
24constructor(delayInSeconds: number) {
34472878RedMickey5 years ago25this.sinceWhenToStopFiltering =
26this.now() +
27delayInSeconds * ExecutionsFilterBeforeTimestamp.MILLISECONDS_IN_ONE_SECOND;
c2bf3c4fdigeff10 years ago28}
29
34472878RedMickey5 years ago30public execute(lambda: () => void): void {
c2bf3c4fdigeff10 years ago31if (this.now() >= this.sinceWhenToStopFiltering) {
32lambda();
33}
34}
35
36private now(): number {
37return new Date().getTime();
38}
39}