Align api.subscribe with contract

This commit is contained in:
Jaco Greeff 2016-12-09 00:15:39 +01:00
parent 4ce3142c63
commit 1ecda93de9

View File

@ -82,7 +82,11 @@ export default class Manager {
if (!engine.isStarted) { if (!engine.isStarted) {
engine.start(); engine.start();
} else { } else {
this._sendData(subscriptionId, error, data); const result = this._sendData(subscriptionId, error, data);
if (autoRemove && !result) {
this.unsubscribe(subscriptionId);
}
} }
resolve(subscriptionId); resolve(subscriptionId);
@ -102,19 +106,18 @@ export default class Manager {
} }
_sendData (subscriptionId, error, data) { _sendData (subscriptionId, error, data) {
const { callback } = this.subscriptions[subscriptionId]; let result = false;
try { try {
return callback(error, data); result = this.subscriptions[subscriptionId](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 true; return result;
} }
_updateSubscriptions = (subscriptionName, error, data) => { _updateSubscriptions = (subscriptionName, error, data) => {
const cleanup = [];
const subscriptions = this.subscriptions const subscriptions = this.subscriptions
.filter(subscription => subscription.name === subscriptionName); .filter(subscription => subscription.name === subscriptionName);
@ -125,13 +128,9 @@ export default class Manager {
const result = this._sendData(subscription.id, error, data); const result = this._sendData(subscription.id, error, data);
if (subscription.autoRemove && !result) { if (subscription.autoRemove && !result) {
cleanup.push(subscription.id); this.unsubscribe(subscription.id);
} }
}); });
cleanup.forEach((subscriptionId) => {
delete this.subscriptions[subscriptionId];
});
} }
} }