From 2e8f74abce7be3a5799bb93a0360d299de55b621 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 9 Nov 2018 10:40:47 -0800 Subject: Use existing functions instead of writing our own --- packages/instant/src/util/heartbeater.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'packages/instant/src/util/heartbeater.ts') diff --git a/packages/instant/src/util/heartbeater.ts b/packages/instant/src/util/heartbeater.ts index bb4e99383..87af48423 100644 --- a/packages/instant/src/util/heartbeater.ts +++ b/packages/instant/src/util/heartbeater.ts @@ -4,18 +4,24 @@ type HeartbeatableFunction = () => Promise; export class Heartbeater { private _intervalId?: number; private _hasPendingRequest: boolean; + private _performImmediatelyOnStart: boolean; private _performFunction: HeartbeatableFunction; - public constructor(_performingFunctionAsync: HeartbeatableFunction) { - this._performFunction = _performingFunctionAsync; + public constructor(performingFunctionAsync: HeartbeatableFunction, performImmediatelyOnStart: boolean) { + this._performFunction = performingFunctionAsync; this._hasPendingRequest = false; + this._performImmediatelyOnStart = performImmediatelyOnStart; } public start(intervalTimeMs: number): void { if (!_.isUndefined(this._intervalId)) { throw new Error('Heartbeat is running, please stop before restarting'); } - this._trackAndPerformAsync(); + + if (this._performImmediatelyOnStart) { + this._trackAndPerformAsync(); + } + this._intervalId = window.setInterval(this._trackAndPerformAsync.bind(this), intervalTimeMs); } -- cgit