support filter callbacks

This commit is contained in:
Iuri Matias 2019-08-19 14:30:38 -04:00
parent 3c4ceabeca
commit 668b7ef65d
2 changed files with 16 additions and 2 deletions

View File

@ -49,11 +49,19 @@ class EventSyncer {
}
// trackEvent(eventName, filterConditions) {
trackEvent(contractInstance, eventName, filterConditions) {
trackEvent(contractInstance, eventName, filterConditionsOrCb) {
// let eventKey = eventName + "-from0x123";
let eventKey = eventName;
let namespace = randomString()
let filterConditions, filterConditionsCb;
if (typeof filterConditionsOrCb === 'function') {
filterConditionsCb = filterConditionsOrCb
} else {
filterConditions = filterConditionsOrCb
}
let tracked = this.db.getCollection('tracked')
let lastEvent = tracked.find({ "eventName": eventName })[0]
if (!lastEvent || lastEvent.length <= 0) {
@ -93,6 +101,10 @@ class EventSyncer {
return;
}
}
} else if (filterConditionsCb) {
if (!filterConditionsCb(event.returnValues)) {
return;
}
}
this.events.emit("event-" + eventName + "-" + namespace, event);

View File

@ -96,9 +96,11 @@ async function run() {
// TODO: would be nice if trackEvent was smart enough to understand the type of returnValues and do the needed conversions
// eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), myscan, mymap).subscribe((v) => {
// eventSyncer.trackEvent(RatingContract, 'Rating').pipe(map(x => parseInt(x.rating)), myscan, mymap).subscribe((v) => {
// eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => x.rating)).subscribe((v) => {
eventSyncer.trackEvent(RatingContract, 'Rating').pipe(map(x => parseInt(x.rating)), myscan, mymap).subscribe((v) => {
eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), myscan, mymap).subscribe((v) => {
console.dir("value is ")
console.dir(v)
});