diff options
author | Steve Klebanoff <steve@0xproject.com> | 2018-11-10 08:02:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-10 08:02:09 +0800 |
commit | 26cbe7ae6646d97c286e58756c23a93de32ae37b (patch) | |
tree | 96943e8dd87f0e3ef8c994101bce5a16a47082ea /packages/instant/src/util/heartbeater.ts | |
parent | 2c585bfbdc4940e3e6089ac1cf595dd009b141d2 (diff) | |
parent | fd83ca2cb893ab7cc027efd6829ba2eb07cdb539 (diff) | |
download | dexon-sol-tools-26cbe7ae6646d97c286e58756c23a93de32ae37b.tar dexon-sol-tools-26cbe7ae6646d97c286e58756c23a93de32ae37b.tar.gz dexon-sol-tools-26cbe7ae6646d97c286e58756c23a93de32ae37b.tar.bz2 dexon-sol-tools-26cbe7ae6646d97c286e58756c23a93de32ae37b.tar.lz dexon-sol-tools-26cbe7ae6646d97c286e58756c23a93de32ae37b.tar.xz dexon-sol-tools-26cbe7ae6646d97c286e58756c23a93de32ae37b.tar.zst dexon-sol-tools-26cbe7ae6646d97c286e58756c23a93de32ae37b.zip |
Merge pull request #1237 from 0xProject/feature/instant/buy-quote-heartbeat
[instant] Heartbeats for account info and buy quotes
Diffstat (limited to 'packages/instant/src/util/heartbeater.ts')
-rw-r--r-- | packages/instant/src/util/heartbeater.ts | 35 |
1 files changed, 35 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..e700d489e --- /dev/null +++ b/packages/instant/src/util/heartbeater.ts @@ -0,0 +1,35 @@ +import { intervalUtils } from '@0x/utils'; +import * as _ from 'lodash'; + +type HeartbeatableFunction = () => Promise<void>; +export class Heartbeater { + private _intervalId?: NodeJS.Timer; + private readonly _performImmediatelyOnStart: boolean; + private readonly _performFunction: HeartbeatableFunction; + + public constructor(performingFunctionAsync: HeartbeatableFunction, performImmediatelyOnStart: boolean) { + this._performFunction = performingFunctionAsync; + this._performImmediatelyOnStart = performImmediatelyOnStart; + } + + public start(intervalTimeMs: number): void { + if (!_.isUndefined(this._intervalId)) { + throw new Error('Heartbeat is running, please stop before restarting'); + } + + if (this._performImmediatelyOnStart) { + // tslint:disable-next-line:no-floating-promises + this._performFunction(); + } + + // tslint:disable-next-line:no-unbound-method + this._intervalId = intervalUtils.setAsyncExcludingInterval(this._performFunction, intervalTimeMs, _.noop); + } + + public stop(): void { + if (this._intervalId) { + intervalUtils.clearInterval(this._intervalId); + } + this._intervalId = undefined; + } +} |