From 88791f732fc83bb273508ec8d4b1e87c6bea05dc Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 6 Sep 2017 18:22:28 +0200 Subject: Make intervalUtils an object instead of a class and make instance variable local --- src/0x.ts | 8 +++----- src/utils/interval_utils.ts | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/0x.ts b/src/0x.ts index 8709dadc9..5d5604780 100644 --- a/src/0x.ts +++ b/src/0x.ts @@ -13,7 +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 {intervalUtils} from './utils/interval_utils'; import {artifacts} from './artifacts'; import {ExchangeWrapper} from './contract_wrappers/exchange_wrapper'; import {TokenRegistryWrapper} from './contract_wrappers/token_registry_wrapper'; @@ -73,7 +73,6 @@ 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. @@ -194,7 +193,6 @@ 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); @@ -283,10 +281,10 @@ export class ZeroEx { txHash: string, pollingIntervalMs: number = 1000): Promise { const txReceiptPromise = new Promise( (resolve: (receipt: TransactionReceiptWithDecodedLogs) => void, reject) => { - const intervalId = this._intervalUtils.setAsyncExcludingInterval(async () => { + const intervalId = intervalUtils.setAsyncExcludingInterval(async () => { const transactionReceipt = await this._web3Wrapper.getTransactionReceiptAsync(txHash); if (!_.isNull(transactionReceipt)) { - this._intervalUtils.clearAsyncExcludingInterval(intervalId); + 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 index 6bcf5a643..1656318e6 100644 --- a/src/utils/interval_utils.ts +++ b/src/utils/interval_utils.ts @@ -1,20 +1,20 @@ import * as _ from 'lodash'; -export class IntervalUtils { - private mutex: {[intervalId: number]: boolean} = {}; - public setAsyncExcludingInterval(fn: () => Promise, intervalMs: number) { +export const intervalUtils = { + setAsyncExcludingInterval(fn: () => Promise, intervalMs: number) { + let locked = false; const intervalId = setInterval(async () => { - if (!_.isUndefined(this.mutex[intervalId])) { + if (locked) { return; } else { - this.mutex[intervalId] = true; + locked = true; await fn(); - delete this.mutex[intervalId]; + locked = false; } }); return intervalId; - } - public clearAsyncExcludingInterval(intervalId: number): void { + }, + clearAsyncExcludingInterval(intervalId: number): void { clearInterval(intervalId); - } -} + }, +}; -- cgit v1.2.3