diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-10 07:28:08 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-10 07:28:08 +0800 |
commit | 5c1b1a120362c1e84746d8a6695fe4de0ae6fb89 (patch) | |
tree | 8412ecae1fc2af736ba6f782bb45fb3f146d80a8 | |
parent | acb7e876b20f89cf5e1df614cb81de38a66fa6b1 (diff) | |
download | dexon-sol-tools-5c1b1a120362c1e84746d8a6695fe4de0ae6fb89.tar dexon-sol-tools-5c1b1a120362c1e84746d8a6695fe4de0ae6fb89.tar.gz dexon-sol-tools-5c1b1a120362c1e84746d8a6695fe4de0ae6fb89.tar.bz2 dexon-sol-tools-5c1b1a120362c1e84746d8a6695fe4de0ae6fb89.tar.lz dexon-sol-tools-5c1b1a120362c1e84746d8a6695fe4de0ae6fb89.tar.xz dexon-sol-tools-5c1b1a120362c1e84746d8a6695fe4de0ae6fb89.tar.zst dexon-sol-tools-5c1b1a120362c1e84746d8a6695fe4de0ae6fb89.zip |
Using built in intervalUtils instead of rolling own
-rw-r--r-- | packages/instant/src/util/heartbeater.ts | 25 |
1 files changed, 5 insertions, 20 deletions
diff --git a/packages/instant/src/util/heartbeater.ts b/packages/instant/src/util/heartbeater.ts index a5b42d87f..dbc72da6a 100644 --- a/packages/instant/src/util/heartbeater.ts +++ b/packages/instant/src/util/heartbeater.ts @@ -1,15 +1,14 @@ +import { intervalUtils } from '@0x/utils'; import * as _ from 'lodash'; type HeartbeatableFunction = () => Promise<void>; export class Heartbeater { - private _intervalId?: number; - private _hasPendingRequest: boolean; + private _intervalId?: NodeJS.Timer; private readonly _performImmediatelyOnStart: boolean; private readonly _performFunction: HeartbeatableFunction; public constructor(performingFunctionAsync: HeartbeatableFunction, performImmediatelyOnStart: boolean) { this._performFunction = performingFunctionAsync; - this._hasPendingRequest = false; this._performImmediatelyOnStart = performImmediatelyOnStart; } @@ -20,30 +19,16 @@ export class Heartbeater { if (this._performImmediatelyOnStart) { // tslint:disable-next-line:no-floating-promises - this._trackAndPerformAsync(); + this._performFunction(); } - this._intervalId = window.setInterval(this._trackAndPerformAsync.bind(this), intervalTimeMs); + this._intervalId = intervalUtils.setAsyncExcludingInterval(this._performFunction, intervalTimeMs, () => {}); } public stop(): void { if (this._intervalId) { - window.clearInterval(this._intervalId); + intervalUtils.clearInterval(this._intervalId); } this._intervalId = undefined; - this._hasPendingRequest = false; - } - - private async _trackAndPerformAsync(): Promise<void> { - if (this._hasPendingRequest) { - return; - } - - this._hasPendingRequest = true; - try { - await this._performFunction(); - } finally { - this._hasPendingRequest = false; - } } } |