From 3bc3666215d4fdaa6d9db87e065ca5e762df9b25 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 20 Nov 2017 16:25:51 -0600 Subject: Check if callback exists --- packages/0x.js/src/order_watcher/expiration_watcher.ts | 8 ++++---- packages/0x.js/src/order_watcher/order_state_watcher.ts | 10 ++++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/0x.js/src/order_watcher/expiration_watcher.ts b/packages/0x.js/src/order_watcher/expiration_watcher.ts index 7d6ce6bdd..efedd3cf8 100644 --- a/packages/0x.js/src/order_watcher/expiration_watcher.ts +++ b/packages/0x.js/src/order_watcher/expiration_watcher.ts @@ -29,12 +29,12 @@ export class ExpirationWatcher { const comparator = (lhs: string, rhs: string) => scoreFunction(lhs) - scoreFunction(rhs); this.orderHashByExpirationRBTree = new RBTree(comparator); } - public subscribe(callback: (orderHash: string) => void): void { + public subscribe(callbackAsync: (orderHash: string) => Promise): void { if (!_.isUndefined(this.orderExpirationCheckingIntervalIdIfExists)) { throw new Error(ZeroExError.SubscriptionAlreadyPresent); } this.orderExpirationCheckingIntervalIdIfExists = intervalUtils.setAsyncExcludingInterval( - this.pruneExpiredOrders.bind(this, callback), this.orderExpirationCheckingIntervalMs, + this.pruneExpiredOrdersAsync.bind(this, callbackAsync), this.orderExpirationCheckingIntervalMs, ); } public unsubscribe(): void { @@ -52,7 +52,7 @@ export class ExpirationWatcher { this.orderHashByExpirationRBTree.remove(orderHash); delete this.expiration[orderHash]; } - private pruneExpiredOrders(callback: (orderHash: string) => void): void { + private async pruneExpiredOrdersAsync(callbackAsync: (orderHash: string) => Promise): Promise { const currentUnixTimestampMs = utils.getCurrentUnixTimestampMs(); while ( this.orderHashByExpirationRBTree.size !== 0 && @@ -63,7 +63,7 @@ export class ExpirationWatcher { const orderHash = this.orderHashByExpirationRBTree.min(); this.orderHashByExpirationRBTree.remove(orderHash); delete this.expiration[orderHash]; - callback(orderHash); + await callbackAsync(orderHash); } } } diff --git a/packages/0x.js/src/order_watcher/order_state_watcher.ts b/packages/0x.js/src/order_watcher/order_state_watcher.ts index 579fa388a..84f2128c0 100644 --- a/packages/0x.js/src/order_watcher/order_state_watcher.ts +++ b/packages/0x.js/src/order_watcher/order_state_watcher.ts @@ -128,7 +128,7 @@ export class OrderStateWatcher { } this._callbackIfExists = callback; this._eventWatcher.subscribe(this._onEventWatcherCallbackAsync.bind(this)); - this._expirationWatcher.subscribe(this._onOrderExpired.bind(this)); + this._expirationWatcher.subscribe(this._onOrderExpiredAsync.bind(this)); } /** * Ends an orderStateWatcher subscription. @@ -143,7 +143,7 @@ export class OrderStateWatcher { this._eventWatcher.unsubscribe(); this._expirationWatcher.unsubscribe(); } - private _onOrderExpired(orderHash: string): void { + private async _onOrderExpiredAsync(orderHash: string): Promise { const orderState: OrderState = { isValid: false, orderHash, @@ -151,8 +151,10 @@ export class OrderStateWatcher { }; if (!_.isUndefined(this._orderByOrderHash[orderHash])) { // We need this check because we never remove the orders from expiration watcher - this.removeOrder(orderHash); - (this._callbackIfExistsAsync as OnOrderStateChangeCallback)(orderState); + await this.removeOrderAsync(orderHash); + if (!_.isUndefined(this._callbackIfExists)) { + this._callbackIfExists(orderState); + } } } private async _onEventWatcherCallbackAsync(log: LogEvent): Promise { -- cgit