aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/interval_utils.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-09-06 23:41:40 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-09-06 23:41:40 +0800
commita57b22a6bc20df951c70212943b018ba8039a914 (patch)
tree81a9f87d775b245fa165893227fd06cbe376f20d /src/utils/interval_utils.ts
parent7c61b09dce6ea294013a4745895b58ad855817a0 (diff)
downloaddexon-sol-tools-a57b22a6bc20df951c70212943b018ba8039a914.tar
dexon-sol-tools-a57b22a6bc20df951c70212943b018ba8039a914.tar.gz
dexon-sol-tools-a57b22a6bc20df951c70212943b018ba8039a914.tar.bz2
dexon-sol-tools-a57b22a6bc20df951c70212943b018ba8039a914.tar.lz
dexon-sol-tools-a57b22a6bc20df951c70212943b018ba8039a914.tar.xz
dexon-sol-tools-a57b22a6bc20df951c70212943b018ba8039a914.tar.zst
dexon-sol-tools-a57b22a6bc20df951c70212943b018ba8039a914.zip
Fix overlapping async intervals issue
Diffstat (limited to 'src/utils/interval_utils.ts')
-rw-r--r--src/utils/interval_utils.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/utils/interval_utils.ts b/src/utils/interval_utils.ts
new file mode 100644
index 000000000..23e6acc5b
--- /dev/null
+++ b/src/utils/interval_utils.ts
@@ -0,0 +1,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);
+ }
+}