diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-09 09:13:22 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-09 09:13:22 +0800 |
commit | 297a62fe80142897250f6dd6fddb4cdf1d3fe3ee (patch) | |
tree | 92bd8ba2f27d9144bdc1c8fbfda5b50e81015dd5 /packages/instant/src/util/heartbeater.ts | |
parent | 1e39d56cf751fd922d6fec86ecc8bc70b20bc6bb (diff) | |
download | dexon-0x-contracts-297a62fe80142897250f6dd6fddb4cdf1d3fe3ee.tar.gz dexon-0x-contracts-297a62fe80142897250f6dd6fddb4cdf1d3fe3ee.tar.zst dexon-0x-contracts-297a62fe80142897250f6dd6fddb4cdf1d3fe3ee.zip |
move files around and rename
Diffstat (limited to 'packages/instant/src/util/heartbeater.ts')
-rw-r--r-- | packages/instant/src/util/heartbeater.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/instant/src/util/heartbeater.ts b/packages/instant/src/util/heartbeater.ts new file mode 100644 index 000000000..c5a823953 --- /dev/null +++ b/packages/instant/src/util/heartbeater.ts @@ -0,0 +1,42 @@ +import * as _ from 'lodash'; + +type HeartbeatableFunction = () => Promise<void>; +export class Heartbeater { + private _intervalId?: number; + private _pendingRequest: boolean; + private _performingFunctionAsync: HeartbeatableFunction; + + public constructor(_performingFunctionAsync: HeartbeatableFunction) { + this._performingFunctionAsync = _performingFunctionAsync; + this._pendingRequest = false; + } + + public start(intervalTimeMs: number): void { + if (!_.isUndefined(this._intervalId)) { + throw new Error('Heartbeat is running, please stop before restarting'); + } + this._trackAndPerformAsync(); + this._intervalId = window.setInterval(this._trackAndPerformAsync.bind(this), intervalTimeMs); + } + + public stop(): void { + if (this._intervalId) { + window.clearInterval(this._intervalId); + } + this._intervalId = undefined; + this._pendingRequest = false; + } + + private async _trackAndPerformAsync(): Promise<void> { + if (this._pendingRequest) { + return; + } + + this._pendingRequest = true; + try { + await this._performingFunctionAsync(); + } finally { + this._pendingRequest = false; + } + } +} |