From 0edd9b32bab41d3eda9bad297f7e5db4a289cc88 Mon Sep 17 00:00:00 2001 From: fragosti Date: Thu, 11 Oct 2018 15:22:55 -0700 Subject: feat: debounce the fetching of new quotes --- .../src/containers/selected_asset_amount_input.ts | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/instant/src/containers/selected_asset_amount_input.ts b/packages/instant/src/containers/selected_asset_amount_input.ts index b731b889a..fa675b385 100644 --- a/packages/instant/src/containers/selected_asset_amount_input.ts +++ b/packages/instant/src/containers/selected_asset_amount_input.ts @@ -30,6 +30,19 @@ const mapStateToProps = (state: State, _ownProps: SelectedAssetAmountInputProps) value: state.selectedAssetAmount, }); +const updateBuyQuote = async (dispatch: Dispatch, assetAmount?: BigNumber): Promise => { + if (_.isUndefined(assetAmount)) { + return; + } + // get a new buy quote. + const baseUnitValue = Web3Wrapper.toBaseUnitAmount(assetAmount, zrxDecimals); + const newBuyQuote = await assetBuyer.getBuyQuoteForERC20TokenAddressAsync(zrxContractAddress, baseUnitValue); + // invalidate the last buy quote. + dispatch({ type: ActionTypes.UPDATE_LATEST_BUY_QUOTE, data: newBuyQuote }); +}; + +const debouncedUpdateBuyQuote = _.debounce(updateBuyQuote, 200, { trailing: true }); + const mapDispatchToProps = (dispatch: Dispatch): ConnectedDispatch => ({ onChange: async value => { // Update the input @@ -38,16 +51,7 @@ const mapDispatchToProps = (dispatch: Dispatch): ConnectedDispatch => ({ dispatch({ type: ActionTypes.UPDATE_LATEST_BUY_QUOTE, data: undefined }); // reset our buy state dispatch({ type: ActionTypes.UPDATE_SELECTED_ASSET_BUY_STATE, data: AsyncProcessState.NONE }); - if (!_.isUndefined(value)) { - // get a new buy quote. - const baseUnitValue = Web3Wrapper.toBaseUnitAmount(value, zrxDecimals); - const newBuyQuote = await assetBuyer.getBuyQuoteForERC20TokenAddressAsync( - zrxContractAddress, - baseUnitValue, - ); - // invalidate the last buy quote. - dispatch({ type: ActionTypes.UPDATE_LATEST_BUY_QUOTE, data: newBuyQuote }); - } + debouncedUpdateBuyQuote(dispatch, value); }, }); -- cgit