microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.14

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/executionsLimiter.ts

37lines · 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 {
6private executionToLastTimestamp: {[id: string]: number} = {};
7
8public execute(id: string, limitInSeconds: number, lambda: () => void) {
9const 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) {
25this.sinceWhenToStopFiltering = this.now() + delayInSeconds * ExecutionsFilterBeforeTimestamp.MILLISECONDS_IN_ONE_SECOND;
26}
27
28public execute(lambda: () => void) {
29if (this.now() >= this.sinceWhenToStopFiltering) {
30lambda();
31}
32}
33
34private now(): number {
35return new Date().getTime();
36}
37}