diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-06 23:41:40 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-06 23:41:40 +0800 |
commit | a57b22a6bc20df951c70212943b018ba8039a914 (patch) | |
tree | 81a9f87d775b245fa165893227fd06cbe376f20d | |
parent | 7c61b09dce6ea294013a4745895b58ad855817a0 (diff) | |
download | dexon-0x-contracts-a57b22a6bc20df951c70212943b018ba8039a914.tar.gz dexon-0x-contracts-a57b22a6bc20df951c70212943b018ba8039a914.tar.zst dexon-0x-contracts-a57b22a6bc20df951c70212943b018ba8039a914.zip |
Fix overlapping async intervals issue
-rw-r--r-- | src/0x.ts | 7 | ||||
-rw-r--r-- | src/utils/interval_utils.ts | 20 |
2 files changed, 25 insertions, 2 deletions
@@ -13,6 +13,7 @@ import {utils} from './utils/utils'; import {signatureUtils} from './utils/signature_utils'; import {assert} from './utils/assert'; import {AbiDecoder} from './utils/abi_decoder'; +import {IntervalUtils} from './utils/interval_utils'; import {artifacts} from './artifacts'; import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; @@ -72,6 +73,7 @@ export class ZeroEx { public proxy: TokenTransferProxyWrapper; private _web3Wrapper: Web3Wrapper; private _abiDecoder: AbiDecoder; + private _intervalUtils: IntervalUtils; /** * Verifies that the elliptic curve signature `signature` was generated * by signing `data` with the private key corresponding to the `signerAddress` address. @@ -192,6 +194,7 @@ export class ZeroEx { const defaults = { gasPrice, }; + this._intervalUtils = new IntervalUtils(); this._web3Wrapper = new Web3Wrapper(provider, defaults); this.token = new TokenWrapper(this._web3Wrapper); this.proxy = new TokenTransferProxyWrapper(this._web3Wrapper); @@ -280,10 +283,10 @@ export class ZeroEx { txHash: string, pollingIntervalMs: number = 1000): Promise<TransactionReceiptWithDecodedLogs> { const txReceiptPromise = new Promise( (resolve: (receipt: TransactionReceiptWithDecodedLogs) => void, reject) => { - const intervalId = setInterval(async () => { + const intervalId = this._intervalUtils.setAsyncExcludingInterval(async () => { const transactionReceipt = await this._web3Wrapper.getTransactionReceiptAsync(txHash); if (!_.isNull(transactionReceipt)) { - clearInterval(intervalId); + this._intervalUtils.clearAsyncExcludingInterval(intervalId); const logsWithDecodedArgs = _.map(transactionReceipt.logs, (log: Web3.LogEntry) => { const decodedLog = this._abiDecoder.decodeLog(log); if (_.isUndefined(decodedLog)) { diff --git a/src/utils/interval_utils.ts b/src/utils/interval_utils.ts new file mode 100644 index 000000000..23e6acc5b --- /dev/null +++ b/src/utils/interval_utils.ts @@ -0,0 +1,20 @@ +import * as _ from 'lodash'; + +export class IntervalUtils { + private mutex: {[intervalId: number]: boolean} = {}; + public setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number) { + const intervalId = setInterval(async () => { + if (_.isUndefined(this.mutex[intervalId])) { + return; + } else { + this.mutex[intervalId] = true; + await fn(); + delete this.mutex[intervalId]; + } + }); + return intervalId; + } + public clearAsyncExcludingInterval(intervalId: number): void { + clearInterval(intervalId); + } +} |