From 5c1b1a120362c1e84746d8a6695fe4de0ae6fb89 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 9 Nov 2018 15:28:08 -0800 Subject: Using built in intervalUtils instead of rolling own --- packages/instant/src/util/heartbeater.ts | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) (limited to 'packages') 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; 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 { - if (this._hasPendingRequest) { - return; - } - - this._hasPendingRequest = true; - try { - await this._performFunction(); - } finally { - this._hasPendingRequest = false; - } } } -- cgit