diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-10-12 06:22:55 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-10-12 06:35:39 +0800 |
commit | 0edd9b32bab41d3eda9bad297f7e5db4a289cc88 (patch) | |
tree | ad2f1ae002704fd31b9cc44dbce6c6c3f0434259 /packages/instant | |
parent | 63652df3b90ca4ced73d27e09984b67bd32050f4 (diff) | |
download | dexon-sol-tools-0edd9b32bab41d3eda9bad297f7e5db4a289cc88.tar dexon-sol-tools-0edd9b32bab41d3eda9bad297f7e5db4a289cc88.tar.gz dexon-sol-tools-0edd9b32bab41d3eda9bad297f7e5db4a289cc88.tar.bz2 dexon-sol-tools-0edd9b32bab41d3eda9bad297f7e5db4a289cc88.tar.lz dexon-sol-tools-0edd9b32bab41d3eda9bad297f7e5db4a289cc88.tar.xz dexon-sol-tools-0edd9b32bab41d3eda9bad297f7e5db4a289cc88.tar.zst dexon-sol-tools-0edd9b32bab41d3eda9bad297f7e5db4a289cc88.zip |
feat: debounce the fetching of new quotes
Diffstat (limited to 'packages/instant')
-rw-r--r-- | packages/instant/src/containers/selected_asset_amount_input.ts | 24 |
1 files 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<Action>, assetAmount?: BigNumber): Promise<void> => { + 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<Action>): ConnectedDispatch => ({ onChange: async value => { // Update the input @@ -38,16 +51,7 @@ const mapDispatchToProps = (dispatch: Dispatch<Action>): 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); }, }); |