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) {
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) => {

View File

@ -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);
});
}
}