Make heartbeat more generic
This commit is contained in:
parent
dd4d3b10cf
commit
e45b6c7e98
@ -15,7 +15,7 @@ import { AffiliateInfo, AssetMetaData, Network, OrderSource } from '../types';
|
|||||||
import { assetUtils } from '../util/asset';
|
import { assetUtils } from '../util/asset';
|
||||||
import { errorFlasher } from '../util/error_flasher';
|
import { errorFlasher } from '../util/error_flasher';
|
||||||
import { gasPriceEstimator } from '../util/gas_price_estimator';
|
import { gasPriceEstimator } from '../util/gas_price_estimator';
|
||||||
import { AccountUpdateHeartbeat } from '../util/hearbeats';
|
import { generateAccountHeartbeater, Heartbeater } from '../util/hearbeats';
|
||||||
import { providerStateFactory } from '../util/provider_state_factory';
|
import { providerStateFactory } from '../util/provider_state_factory';
|
||||||
|
|
||||||
fonts.include();
|
fonts.include();
|
||||||
@ -39,7 +39,7 @@ export interface ZeroExInstantProviderOptionalProps {
|
|||||||
|
|
||||||
export class ZeroExInstantProvider extends React.Component<ZeroExInstantProviderProps> {
|
export class ZeroExInstantProvider extends React.Component<ZeroExInstantProviderProps> {
|
||||||
private readonly _store: Store;
|
private readonly _store: Store;
|
||||||
private _accountUpdateHeartbeat?: AccountUpdateHeartbeat;
|
private _accountUpdateHeartbeat?: Heartbeater;
|
||||||
// TODO(fragosti): Write tests for this beast once we inject a provider.
|
// TODO(fragosti): Write tests for this beast once we inject a provider.
|
||||||
private static _mergeDefaultStateWithProps(
|
private static _mergeDefaultStateWithProps(
|
||||||
props: ZeroExInstantProviderProps,
|
props: ZeroExInstantProviderProps,
|
||||||
@ -97,8 +97,9 @@ export class ZeroExInstantProvider extends React.Component<ZeroExInstantProvider
|
|||||||
}
|
}
|
||||||
// tslint:disable-next-line:no-floating-promises
|
// tslint:disable-next-line:no-floating-promises
|
||||||
// asyncData.fetchAccountInfoAndDispatchToStore(this._store);
|
// asyncData.fetchAccountInfoAndDispatchToStore(this._store);
|
||||||
this._accountUpdateHeartbeat = new AccountUpdateHeartbeat();
|
|
||||||
this._accountUpdateHeartbeat.start(this._store, ACCOUNT_UPDATE_INTERVAL_TIME_MS);
|
this._accountUpdateHeartbeat = generateAccountHeartbeater(this._store);
|
||||||
|
this._accountUpdateHeartbeat.start(ACCOUNT_UPDATE_INTERVAL_TIME_MS);
|
||||||
|
|
||||||
// warm up the gas price estimator cache just in case we can't
|
// warm up the gas price estimator cache just in case we can't
|
||||||
// grab the gas price estimate when submitting the transaction
|
// grab the gas price estimate when submitting the transaction
|
||||||
|
@ -1,48 +1,53 @@
|
|||||||
|
// TODO: rename file
|
||||||
|
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
|
||||||
import { asyncData } from './../redux/async_data';
|
import { asyncData } from './../redux/async_data';
|
||||||
import { Store } from './../redux/store';
|
import { Store } from './../redux/store';
|
||||||
|
|
||||||
export class AccountUpdateHeartbeat {
|
type HeartbeatableFunction = () => Promise<void>;
|
||||||
|
export class Heartbeater {
|
||||||
private _intervalId?: number;
|
private _intervalId?: number;
|
||||||
private _pendingRequest?: boolean;
|
private _pendingRequest: boolean;
|
||||||
private _store?: Store;
|
private _performingFunctionAsync: HeartbeatableFunction;
|
||||||
|
|
||||||
public start(store: Store, intervalTimeMs: number): void {
|
public constructor(_performingFunctionAsync: HeartbeatableFunction) {
|
||||||
|
this._performingFunctionAsync = _performingFunctionAsync;
|
||||||
|
this._pendingRequest = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public start(intervalTimeMs: number): void {
|
||||||
if (!_.isUndefined(this._intervalId)) {
|
if (!_.isUndefined(this._intervalId)) {
|
||||||
throw new Error('Heartbeat is running, please stop before restarting');
|
throw new Error('Heartbeat is running, please stop before restarting');
|
||||||
}
|
}
|
||||||
this._store = store;
|
this._trackAndPerformAsync();
|
||||||
// Kick off initial first request
|
this._intervalId = window.setInterval(this._trackAndPerformAsync.bind(this), intervalTimeMs);
|
||||||
// tslint:disable-next-line:no-floating-promises
|
|
||||||
this._performActionAsync(true);
|
|
||||||
// Set interval for heartbeat
|
|
||||||
this._intervalId = window.setInterval(this._performActionAsync.bind(this, false), intervalTimeMs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public stop(): void {
|
public stop(): void {
|
||||||
if (!_.isUndefined(this._intervalId)) {
|
if (this._intervalId) {
|
||||||
window.clearInterval(this._intervalId);
|
window.clearInterval(this._intervalId);
|
||||||
this._resetState();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private _resetState(): void {
|
|
||||||
this._intervalId = undefined;
|
this._intervalId = undefined;
|
||||||
this._pendingRequest = false;
|
this._pendingRequest = false;
|
||||||
this._store = undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _performActionAsync(setLoading: boolean): Promise<void> {
|
private async _trackAndPerformAsync(): Promise<void> {
|
||||||
if (this._pendingRequest || _.isUndefined(this._store)) {
|
if (this._pendingRequest) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this._pendingRequest = true;
|
this._pendingRequest = true;
|
||||||
try {
|
try {
|
||||||
await asyncData.fetchAccountInfoAndDispatchToStore(this._store, { setLoading });
|
this._performingFunctionAsync();
|
||||||
} finally {
|
} finally {
|
||||||
this._pendingRequest = false;
|
this._pendingRequest = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const generateAccountHeartbeater = (store: Store): Heartbeater => {
|
||||||
|
return new Heartbeater(async () => {
|
||||||
|
await asyncData.fetchAccountInfoAndDispatchToStore(store, { setLoading: false });
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user