From 668b7ef65db6f7b4d23407f13b8f1a0e088cd363 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 19 Aug 2019 14:30:38 -0400 Subject: [PATCH] support filter callbacks --- src/eventSyncer.js | 14 +++++++++++++- test/test1.js | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/eventSyncer.js b/src/eventSyncer.js index d3c6b33..56b64a7 100644 --- a/src/eventSyncer.js +++ b/src/eventSyncer.js @@ -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); diff --git a/test/test1.js b/test/test1.js index eb220da..de7dcba 100644 --- a/test/test1.js +++ b/test/test1.js @@ -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) });