diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-06 23:41:40 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-06 23:41:40 +0800 |
commit | a57b22a6bc20df951c70212943b018ba8039a914 (patch) | |
tree | 81a9f87d775b245fa165893227fd06cbe376f20d /src/utils/interval_utils.ts | |
parent | 7c61b09dce6ea294013a4745895b58ad855817a0 (diff) | |
download | dexon-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.ts | 20 |
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); + } +} |