Add autoRemove functionality to api.contract.subscribe
This commit is contained in:
parent
ad971a444c
commit
4ce3142c63
@ -240,8 +240,8 @@ export default class Contract {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_bindEvent = (event) => {
|
_bindEvent = (event) => {
|
||||||
event.subscribe = (options = {}, callback) => {
|
event.subscribe = (options = {}, callback, autoRemove = false) => {
|
||||||
return this._subscribe(event, options, callback);
|
return this._subscribe(event, options, callback, autoRemove);
|
||||||
};
|
};
|
||||||
|
|
||||||
event.unsubscribe = (subscriptionId) => {
|
event.unsubscribe = (subscriptionId) => {
|
||||||
@ -307,16 +307,16 @@ 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 = false) {
|
||||||
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) {
|
_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'];
|
||||||
@ -326,6 +326,7 @@ export default class Contract {
|
|||||||
.then((filterId) => {
|
.then((filterId) => {
|
||||||
this._subscriptions[subscriptionId] = {
|
this._subscriptions[subscriptionId] = {
|
||||||
options: _options,
|
options: _options,
|
||||||
|
autoRemove,
|
||||||
callback,
|
callback,
|
||||||
filterId
|
filterId
|
||||||
};
|
};
|
||||||
@ -338,7 +339,11 @@ 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));
|
const result = callback(null, this.parseEventLogs(logs));
|
||||||
|
|
||||||
|
if (autoRemove && !result) {
|
||||||
|
this.unsubscribe(subscriptionId);
|
||||||
|
}
|
||||||
|
|
||||||
this._subscribeToChanges();
|
this._subscribeToChanges();
|
||||||
return subscriptionId;
|
return subscriptionId;
|
||||||
@ -438,16 +443,22 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let result = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
subscriptions[idx].callback(null, this.parseEventLogs(logs));
|
result = subscriptions[subscriptionId].callback(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) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user