This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1237 from 0xProject/feature/instant/buy-quote-hea…
…rtbeat [instant] Heartbeats for account info and buy quotes
- Loading branch information
Showing
7 changed files
with
106 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { asyncData } from '../redux/async_data'; | ||
import { Store } from '../redux/store'; | ||
|
||
import { Heartbeater } from './heartbeater'; | ||
|
||
export interface HeartbeatFactoryOptions { | ||
store: Store; | ||
shouldPerformImmediatelyOnStart: boolean; | ||
} | ||
export const generateAccountHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => { | ||
const { store, shouldPerformImmediatelyOnStart } = options; | ||
return new Heartbeater(async () => { | ||
await asyncData.fetchAccountInfoAndDispatchToStore({ store, shouldSetToLoading: false }); | ||
}, shouldPerformImmediatelyOnStart); | ||
}; | ||
|
||
export const generateBuyQuoteHeartbeater = (options: HeartbeatFactoryOptions): Heartbeater => { | ||
const { store, shouldPerformImmediatelyOnStart } = options; | ||
return new Heartbeater(async () => { | ||
await asyncData.fetchCurrentBuyQuoteAndDispatchToStore({ store, shouldSetPending: false }); | ||
}, shouldPerformImmediatelyOnStart); | ||
}; |