From 1e39d56cf751fd922d6fec86ecc8bc70b20bc6bb Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 8 Nov 2018 17:09:26 -0800 Subject: wip: BuyQuote heartbeat --- .../src/components/zero_ex_instant_provider.tsx | 13 +++++++++++-- packages/instant/src/constants.ts | 1 + packages/instant/src/util/buy_quote_fetcher.ts | 18 ++++++++++++++++++ packages/instant/src/util/hearbeats.ts | 18 +++++++++++++++--- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/packages/instant/src/components/zero_ex_instant_provider.tsx b/packages/instant/src/components/zero_ex_instant_provider.tsx index 1805a11af..24ae0c4b6 100644 --- a/packages/instant/src/components/zero_ex_instant_provider.tsx +++ b/packages/instant/src/components/zero_ex_instant_provider.tsx @@ -5,7 +5,7 @@ import * as _ from 'lodash'; import * as React from 'react'; import { Provider as ReduxProvider } from 'react-redux'; -import { ACCOUNT_UPDATE_INTERVAL_TIME_MS } from '../constants'; +import { ACCOUNT_UPDATE_INTERVAL_TIME_MS, BUY_QUOTE_UPDATE_INTERVAL_TIME_MS } from '../constants'; import { SelectedAssetThemeProvider } from '../containers/selected_asset_theme_provider'; import { asyncData } from '../redux/async_data'; import { DEFAULT_STATE, DefaultState, State } from '../redux/reducer'; @@ -15,7 +15,7 @@ import { AffiliateInfo, AssetMetaData, Network, OrderSource } from '../types'; import { assetUtils } from '../util/asset'; import { errorFlasher } from '../util/error_flasher'; import { gasPriceEstimator } from '../util/gas_price_estimator'; -import { generateAccountHeartbeater, Heartbeater } from '../util/hearbeats'; +import { generateAccountHeartbeater, Heartbeater, generateBuyQuoteHeartbeater } from '../util/hearbeats'; import { providerStateFactory } from '../util/provider_state_factory'; fonts.include(); @@ -40,6 +40,8 @@ export interface ZeroExInstantProviderOptionalProps { export class ZeroExInstantProvider extends React.Component { private readonly _store: Store; private _accountUpdateHeartbeat?: Heartbeater; + private _buyQuoteHeartbeat?: Heartbeater; + // TODO(fragosti): Write tests for this beast once we inject a provider. private static _mergeDefaultStateWithProps( props: ZeroExInstantProviderProps, @@ -101,6 +103,10 @@ export class ZeroExInstantProvider extends React.Component) => { + const { selectedAsset, selectedAssetAmount, affiliateInfo } = state; + const assetBuyer = state.providerState.assetBuyer; + + if (selectedAsset && selectedAssetAmount && selectedAsset.metaData.assetProxyId === AssetProxyId.ERC20) { + // TODO: maybe dont do in the case of an error showing + updateBuyQuoteOrFlashErrorAsync( + assetBuyer, + selectedAsset as ERC20Asset, // TODO: better way to do this? + selectedAssetAmount, + dispatch, + affiliateInfo, + ); + } +}; diff --git a/packages/instant/src/util/hearbeats.ts b/packages/instant/src/util/hearbeats.ts index 443fd13ec..ecd7c5068 100644 --- a/packages/instant/src/util/hearbeats.ts +++ b/packages/instant/src/util/hearbeats.ts @@ -1,9 +1,14 @@ // TODO: rename file import * as _ from 'lodash'; +import { Dispatch } from 'redux'; -import { asyncData } from './../redux/async_data'; -import { Store } from './../redux/store'; +import { Action } from '../redux/actions'; +import { asyncData } from '../redux/async_data'; +import { State } from '../redux/reducer'; +import { Store } from '../redux/store'; + +import { updateBuyQuoteOrFlashErrorAsyncForState } from './buy_quote_fetcher'; type HeartbeatableFunction = () => Promise; export class Heartbeater { @@ -39,7 +44,7 @@ export class Heartbeater { this._pendingRequest = true; try { - this._performingFunctionAsync(); + await this._performingFunctionAsync(); } finally { this._pendingRequest = false; } @@ -51,3 +56,10 @@ export const generateAccountHeartbeater = (store: Store): Heartbeater => { await asyncData.fetchAccountInfoAndDispatchToStore(store, { setLoading: false }); }); }; + +export const generateBuyQuoteHeartbeater = (store: Store): Heartbeater => { + return new Heartbeater(async () => { + await updateBuyQuoteOrFlashErrorAsyncForState(store.getState(), store.dispatch); + return Promise.resolve(); + }); +}; -- cgit