From cb596c1413938ce23901135f8235bb813cc6e784 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 8 Dec 2017 18:01:40 +0300 Subject: Move more shared utils into utils package and reuse them --- packages/utils/src/interval_utils.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 packages/utils/src/interval_utils.ts (limited to 'packages/utils/src/interval_utils.ts') diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts new file mode 100644 index 000000000..62b79f2f5 --- /dev/null +++ b/packages/utils/src/interval_utils.ts @@ -0,0 +1,20 @@ +import * as _ from 'lodash'; + +export const intervalUtils = { + setAsyncExcludingInterval(fn: () => Promise, intervalMs: number) { + let locked = false; + const intervalId = setInterval(async () => { + if (locked) { + return; + } else { + locked = true; + await fn(); + locked = false; + } + }, intervalMs); + return intervalId; + }, + clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void { + clearInterval(intervalId); + }, +}; -- cgit v1.2.3