aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/interval_utils.ts
blob: 6bcf5a6433fb2a23d16bf72fa37dd4420ac57dcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import * as _ from 'lodash';

export class IntervalUtils {
    private mutex: {[intervalId: number]: boolean} = {};
    public setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number) {
        const intervalId = setInterval(async () => {
            if (!_.isUndefined(this.mutex[intervalId])) {
                return;
            } else {
                this.mutex[intervalId] = true;
                await fn();
                delete this.mutex[intervalId];
            }
        });
        return intervalId;
    }
    public clearAsyncExcludingInterval(intervalId: number): void {
        clearInterval(intervalId);
    }
}