diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-11-10 08:22:36 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-11-10 08:22:36 +0800 |
commit | ff027ee36a5c2384004c9b55baa42919b5d9fa65 (patch) | |
tree | 72300769affc9d33f5c8ef32aadd1b8cc31f98d6 /packages/instant/src/util | |
parent | ba292ead45f451e4d7d2fac74582b8406cd4a586 (diff) | |
parent | 397b4e289015f9bb0831c1a0ce6fee601670b487 (diff) | |
download | dexon-0x-contracts-ff027ee36a5c2384004c9b55baa42919b5d9fa65.tar dexon-0x-contracts-ff027ee36a5c2384004c9b55baa42919b5d9fa65.tar.gz dexon-0x-contracts-ff027ee36a5c2384004c9b55baa42919b5d9fa65.tar.bz2 dexon-0x-contracts-ff027ee36a5c2384004c9b55baa42919b5d9fa65.tar.lz dexon-0x-contracts-ff027ee36a5c2384004c9b55baa42919b5d9fa65.tar.xz dexon-0x-contracts-ff027ee36a5c2384004c9b55baa42919b5d9fa65.tar.zst dexon-0x-contracts-ff027ee36a5c2384004c9b55baa42919b5d9fa65.zip |
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/instant/metamask-connect-flow
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/buy_quote_updater.ts | 7 | ||||
-rw-r--r-- | packages/instant/src/util/heartbeater.ts | 35 | ||||
-rw-r--r-- | packages/instant/src/util/heartbeater_factory.ts | 22 |
3 files changed, 62 insertions, 2 deletions
diff --git a/packages/instant/src/util/buy_quote_updater.ts b/packages/instant/src/util/buy_quote_updater.ts index e697d3ef7..c33e28f1c 100644 --- a/packages/instant/src/util/buy_quote_updater.ts +++ b/packages/instant/src/util/buy_quote_updater.ts @@ -16,12 +16,15 @@ export const buyQuoteUpdater = { dispatch: Dispatch<Action>, asset: ERC20Asset, assetAmount: BigNumber, + setPending = true, affiliateInfo?: AffiliateInfo, ): Promise<void> => { // get a new buy quote. const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetAmount, asset.metaData.decimals); - // mark quote as pending - dispatch(actions.setQuoteRequestStatePending()); + if (setPending) { + // mark quote as pending + dispatch(actions.setQuoteRequestStatePending()); + } const feePercentage = oc(affiliateInfo).feePercentage(); let newBuyQuote: BuyQuote | undefined; try { 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; + } +} diff --git a/packages/instant/src/util/heartbeater_factory.ts b/packages/instant/src/util/heartbeater_factory.ts new file mode 100644 index 000000000..96a8ac4e6 --- /dev/null +++ b/packages/instant/src/util/heartbeater_factory.ts @@ -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); +}; |