Simplify, consolidate uses between subscription managers

This commit is contained in:
Jaco Greeff 2016-12-09 00:52:21 +01:00
parent 3989e2642b
commit 10c356de65
2 changed files with 24 additions and 25 deletions

View File

@ -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) { _subscribe (event = null, _options, callback, autoRemove = false) {
const subscriptionId = nextSubscriptionId++; const subscriptionId = nextSubscriptionId++;
const { skipInitFetch } = _options; const { skipInitFetch } = _options;
@ -339,12 +354,7 @@ export default class Contract {
return this._api.eth return this._api.eth
.getFilterLogs(filterId) .getFilterLogs(filterId)
.then((logs) => { .then((logs) => {
const result = callback(null, this.parseEventLogs(logs)); this._sendData(subscriptionId, null, this.parseEventLogs(logs));
if (autoRemove && !result) {
this.unsubscribe(subscriptionId);
}
this._subscribeToChanges(); this._subscribeToChanges();
return subscriptionId; return subscriptionId;
}); });
@ -448,17 +458,11 @@ export default class Contract {
return; return;
} }
let result = false;
try { try {
result = subscriptions[subscriptionId].callback(null, this.parseEventLogs(logs)); this.sendData(subscriptionId, null, this.parseEventLogs(logs));
} catch (error) { } catch (error) {
console.error('_sendSubscriptionChanges', error); console.error('_sendSubscriptionChanges', error);
} }
if (subscriptions[subscriptionId].autoRemove && !result) {
this.unsubscribe(subscriptionId);
}
}); });
}) })
.catch((error) => { .catch((error) => {

View File

@ -82,11 +82,7 @@ export default class Manager {
if (!engine.isStarted) { if (!engine.isStarted) {
engine.start(); engine.start();
} else { } else {
const result = this._sendData(subscriptionId, error, data); this._sendData(subscriptionId, error, data);
if (autoRemove && !result) {
this.unsubscribe(subscriptionId);
}
} }
resolve(subscriptionId); resolve(subscriptionId);
@ -106,15 +102,18 @@ export default class Manager {
} }
_sendData (subscriptionId, error, data) { _sendData (subscriptionId, error, data) {
const { autoRemove, callback } = this.subscriptions[subscriptionId];
let result = false; let result = false;
try { try {
result = this.subscriptions[subscriptionId].callback(error, data); result = callback(error, data);
} catch (error) { } catch (error) {
console.error(`Unable to update callback for subscriptionId ${subscriptionId}`, error); console.error(`Unable to update callback for subscriptionId ${subscriptionId}`, error);
} }
return result; if (autoRemove && !result) {
this.unsubscribe(subscriptionId);
}
} }
_updateSubscriptions = (subscriptionName, error, data) => { _updateSubscriptions = (subscriptionName, error, data) => {
@ -125,11 +124,7 @@ export default class Manager {
subscriptions subscriptions
.forEach((subscription) => { .forEach((subscription) => {
const result = this._sendData(subscription.id, error, data); this._sendData(subscription.id, error, data);
if (subscription.autoRemove && !result) {
this.unsubscribe(subscription.id);
}
}); });
} }
} }