diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-09 08:54:45 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2018-11-09 08:54:45 +0800 |
commit | e45b6c7e98a33de0e13f4ab7db8b630900dbb960 (patch) | |
tree | 12ca1aeb83063015b3124c6025af99abd3d3d5ea /packages/instant/src/util/hearbeats.ts | |
parent | dd4d3b10cf8c0bb08f981128232d3663d01806eb (diff) | |
download | dexon-0x-contracts-e45b6c7e98a33de0e13f4ab7db8b630900dbb960.tar.gz dexon-0x-contracts-e45b6c7e98a33de0e13f4ab7db8b630900dbb960.tar.zst dexon-0x-contracts-e45b6c7e98a33de0e13f4ab7db8b630900dbb960.zip |
Make heartbeat more generic
Diffstat (limited to 'packages/instant/src/util/hearbeats.ts')
-rw-r--r-- | packages/instant/src/util/hearbeats.ts | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/packages/instant/src/util/hearbeats.ts b/packages/instant/src/util/hearbeats.ts index 78ab86360..443fd13ec 100644 --- a/packages/instant/src/util/hearbeats.ts +++ b/packages/instant/src/util/hearbeats.ts @@ -1,48 +1,53 @@ +// TODO: rename file + import * as _ from 'lodash'; import { asyncData } from './../redux/async_data'; import { Store } from './../redux/store'; -export class AccountUpdateHeartbeat { +type HeartbeatableFunction = () => Promise<void>; +export class Heartbeater { private _intervalId?: number; - private _pendingRequest?: boolean; - private _store?: Store; + private _pendingRequest: boolean; + private _performingFunctionAsync: HeartbeatableFunction; + + public constructor(_performingFunctionAsync: HeartbeatableFunction) { + this._performingFunctionAsync = _performingFunctionAsync; + this._pendingRequest = false; + } - public start(store: Store, intervalTimeMs: number): void { + public start(intervalTimeMs: number): void { if (!_.isUndefined(this._intervalId)) { throw new Error('Heartbeat is running, please stop before restarting'); } - this._store = store; - // Kick off initial first request - // tslint:disable-next-line:no-floating-promises - this._performActionAsync(true); - // Set interval for heartbeat - this._intervalId = window.setInterval(this._performActionAsync.bind(this, false), intervalTimeMs); + this._trackAndPerformAsync(); + this._intervalId = window.setInterval(this._trackAndPerformAsync.bind(this), intervalTimeMs); } public stop(): void { - if (!_.isUndefined(this._intervalId)) { + if (this._intervalId) { window.clearInterval(this._intervalId); - this._resetState(); } - } - - private _resetState(): void { this._intervalId = undefined; this._pendingRequest = false; - this._store = undefined; } - private async _performActionAsync(setLoading: boolean): Promise<void> { - if (this._pendingRequest || _.isUndefined(this._store)) { + private async _trackAndPerformAsync(): Promise<void> { + if (this._pendingRequest) { return; } this._pendingRequest = true; try { - await asyncData.fetchAccountInfoAndDispatchToStore(this._store, { setLoading }); + this._performingFunctionAsync(); } finally { this._pendingRequest = false; } } } + +export const generateAccountHeartbeater = (store: Store): Heartbeater => { + return new Heartbeater(async () => { + await asyncData.fetchAccountInfoAndDispatchToStore(store, { setLoading: false }); + }); +}; |