aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/interval_utils.ts
blob: 23e6acc5b94dc711454addc6505661ede5117875 (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);
    }
}