From 10c356de65bd269833ef12d6927a00d89ca3d9f8 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 9 Dec 2016 00:52:21 +0100 Subject: [PATCH] Simplify, consolidate uses between subscription managers --- js/src/api/contract/contract.js | 30 ++++++++++++++++------------- js/src/api/subscriptions/manager.js | 19 +++++++----------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/js/src/api/contract/contract.js b/js/src/api/contract/contract.js index f34cd8575..233115d68 100644 --- a/js/src/api/contract/contract.js +++ b/js/src/api/contract/contract.js @@ -316,6 +316,21 @@ export default class Contract { } } + _sendData (subscriptionId, error, logs) { + const { autoRemove, callback } = this._subscriptions[subscriptionId]; + let result = false; + + try { + result = callback(error, logs); + } catch (error) { + console.warn('_sendData', subscriptionId, error); + } + + if (autoRemove && !result) { + this.unsubscribe(subscriptionId); + } + } + _subscribe (event = null, _options, callback, autoRemove = false) { const subscriptionId = nextSubscriptionId++; const { skipInitFetch } = _options; @@ -339,12 +354,7 @@ export default class Contract { return this._api.eth .getFilterLogs(filterId) .then((logs) => { - const result = callback(null, this.parseEventLogs(logs)); - - if (autoRemove && !result) { - this.unsubscribe(subscriptionId); - } - + this._sendData(subscriptionId, null, this.parseEventLogs(logs)); this._subscribeToChanges(); return subscriptionId; }); @@ -448,17 +458,11 @@ export default class Contract { return; } - let result = false; - try { - result = subscriptions[subscriptionId].callback(null, this.parseEventLogs(logs)); + this.sendData(subscriptionId, null, this.parseEventLogs(logs)); } catch (error) { console.error('_sendSubscriptionChanges', error); } - - if (subscriptions[subscriptionId].autoRemove && !result) { - this.unsubscribe(subscriptionId); - } }); }) .catch((error) => { diff --git a/js/src/api/subscriptions/manager.js b/js/src/api/subscriptions/manager.js index 685aa243c..a2716684b 100644 --- a/js/src/api/subscriptions/manager.js +++ b/js/src/api/subscriptions/manager.js @@ -82,11 +82,7 @@ export default class Manager { if (!engine.isStarted) { engine.start(); } else { - const result = this._sendData(subscriptionId, error, data); - - if (autoRemove && !result) { - this.unsubscribe(subscriptionId); - } + this._sendData(subscriptionId, error, data); } resolve(subscriptionId); @@ -106,15 +102,18 @@ export default class Manager { } _sendData (subscriptionId, error, data) { + const { autoRemove, callback } = this.subscriptions[subscriptionId]; let result = false; try { - result = this.subscriptions[subscriptionId].callback(error, data); + result = callback(error, data); } catch (error) { console.error(`Unable to update callback for subscriptionId ${subscriptionId}`, error); } - return result; + if (autoRemove && !result) { + this.unsubscribe(subscriptionId); + } } _updateSubscriptions = (subscriptionName, error, data) => { @@ -125,11 +124,7 @@ export default class Manager { subscriptions .forEach((subscription) => { - const result = this._sendData(subscription.id, error, data); - - if (subscription.autoRemove && !result) { - this.unsubscribe(subscription.id); - } + this._sendData(subscription.id, error, data); }); } }