Merge pull request #3752 from ethcore/jg-subscription-check
Allow autoRemove from api.subscribe based on callback return values
This commit is contained in:
commit
45ec84e627
@ -240,8 +240,8 @@ export default class Contract {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_bindEvent = (event) => {
|
_bindEvent = (event) => {
|
||||||
event.subscribe = (options = {}, callback) => {
|
event.subscribe = (options = {}, callback, autoRemove) => {
|
||||||
return this._subscribe(event, options, callback);
|
return this._subscribe(event, options, callback, autoRemove);
|
||||||
};
|
};
|
||||||
|
|
||||||
event.unsubscribe = (subscriptionId) => {
|
event.unsubscribe = (subscriptionId) => {
|
||||||
@ -306,16 +306,31 @@ export default class Contract {
|
|||||||
return this._api.eth.newFilter(options);
|
return this._api.eth.newFilter(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe (eventName = null, options = {}, callback) {
|
subscribe (eventName = null, options = {}, callback, autoRemove) {
|
||||||
try {
|
try {
|
||||||
const event = this._findEvent(eventName);
|
const event = this._findEvent(eventName);
|
||||||
return this._subscribe(event, options, callback);
|
return this._subscribe(event, options, callback, autoRemove);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return Promise.reject(e);
|
return Promise.reject(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_subscribe (event = null, _options, callback) {
|
_sendData (subscriptionId, error, logs) {
|
||||||
|
const { autoRemove, callback } = this._subscriptions[subscriptionId];
|
||||||
|
let result = true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = callback(error, logs);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('_sendData', subscriptionId, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (autoRemove && result && typeof result === 'boolean') {
|
||||||
|
this.unsubscribe(subscriptionId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_subscribe (event = null, _options, callback, autoRemove = false) {
|
||||||
const subscriptionId = nextSubscriptionId++;
|
const subscriptionId = nextSubscriptionId++;
|
||||||
const { skipInitFetch } = _options;
|
const { skipInitFetch } = _options;
|
||||||
delete _options['skipInitFetch'];
|
delete _options['skipInitFetch'];
|
||||||
@ -325,6 +340,7 @@ export default class Contract {
|
|||||||
.then((filterId) => {
|
.then((filterId) => {
|
||||||
this._subscriptions[subscriptionId] = {
|
this._subscriptions[subscriptionId] = {
|
||||||
options: _options,
|
options: _options,
|
||||||
|
autoRemove,
|
||||||
callback,
|
callback,
|
||||||
filterId
|
filterId
|
||||||
};
|
};
|
||||||
@ -337,8 +353,7 @@ export default class Contract {
|
|||||||
return this._api.eth
|
return this._api.eth
|
||||||
.getFilterLogs(filterId)
|
.getFilterLogs(filterId)
|
||||||
.then((logs) => {
|
.then((logs) => {
|
||||||
callback(null, this.parseEventLogs(logs));
|
this._sendData(subscriptionId, null, this.parseEventLogs(logs));
|
||||||
|
|
||||||
this._subscribeToChanges();
|
this._subscribeToChanges();
|
||||||
return subscriptionId;
|
return subscriptionId;
|
||||||
});
|
});
|
||||||
@ -437,13 +452,13 @@ export default class Contract {
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
.then((logsArray) => {
|
.then((logsArray) => {
|
||||||
logsArray.forEach((logs, idx) => {
|
logsArray.forEach((logs, subscriptionId) => {
|
||||||
if (!logs || !logs.length) {
|
if (!logs || !logs.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
subscriptions[idx].callback(null, this.parseEventLogs(logs));
|
this.sendData(subscriptionId, null, this.parseEventLogs(logs));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('_sendSubscriptionChanges', error);
|
console.error('_sendSubscriptionChanges', error);
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ export default class Manager {
|
|||||||
return subscription;
|
return subscription;
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe (subscriptionName, callback) {
|
subscribe (subscriptionName, callback, autoRemove = false) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const subscription = this._validateType(subscriptionName);
|
const subscription = this._validateType(subscriptionName);
|
||||||
|
|
||||||
@ -75,6 +75,7 @@ export default class Manager {
|
|||||||
this.subscriptions[subscriptionId] = {
|
this.subscriptions[subscriptionId] = {
|
||||||
name: subscriptionName,
|
name: subscriptionName,
|
||||||
id: subscriptionId,
|
id: subscriptionId,
|
||||||
|
autoRemove,
|
||||||
callback
|
callback
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -101,13 +102,18 @@ export default class Manager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_sendData (subscriptionId, error, data) {
|
_sendData (subscriptionId, error, data) {
|
||||||
const { callback } = this.subscriptions[subscriptionId];
|
const { autoRemove, callback } = this.subscriptions[subscriptionId];
|
||||||
|
let result = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (autoRemove && result && typeof result === 'boolean') {
|
||||||
|
this.unsubscribe(subscriptionId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_updateSubscriptions = (subscriptionName, error, data) => {
|
_updateSubscriptions = (subscriptionName, error, data) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user