From f05a04fbc122c70389840d266c7a7d8d0900da67 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 19 Aug 2019 14:23:54 -0400 Subject: [PATCH 1/3] update test to remove custom block --- test/test1.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test1.js b/test/test1.js index b2268db..eb220da 100644 --- a/test/test1.js +++ b/test/test1.js @@ -95,8 +95,10 @@ async function run() { eventSyncer.init(() => { // 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', ((x) => true)).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) => { console.dir("value is ") console.dir(v) }); @@ -106,8 +108,10 @@ async function run() { return acc; }, []) + // eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), max, distinctUntilChanged()).subscribe((v) => { // eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), last()).subscribe((v) => { - eventSyncer.trackEvent(RatingContract, 'Rating', ((x) => true)).pipe(map(x => parseInt(x.rating)), max, distinctUntilChanged()).subscribe((v) => { + + eventSyncer.trackEvent(RatingContract, 'Rating').pipe(map(x => parseInt(x.rating)), max, distinctUntilChanged()).subscribe((v) => { console.dir("max known rating is") console.dir(v) }); From 3c4ceabecaab6b2c067bb2072a2545f87f4b86ce Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 19 Aug 2019 14:24:17 -0400 Subject: [PATCH 2/3] only filter conditions if one is given; namespace events to avoid conflicts --- src/eventSyncer.js | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/eventSyncer.js b/src/eventSyncer.js index 607eca3..d3c6b33 100644 --- a/src/eventSyncer.js +++ b/src/eventSyncer.js @@ -3,6 +3,10 @@ const { throttle, filter } = require('rxjs/operators'); const loki = require('lokijs') const Events = require('events') +function randomString() { + return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); +} + class EventSyncer { constructor(web3) { @@ -48,6 +52,7 @@ class EventSyncer { trackEvent(contractInstance, eventName, filterConditions) { // let eventKey = eventName + "-from0x123"; let eventKey = eventName; + let namespace = randomString() let tracked = this.db.getCollection('tracked') let lastEvent = tracked.find({ "eventName": eventName })[0] @@ -66,29 +71,31 @@ class EventSyncer { sub.next(previous) } - let contractObserver = fromEvent(this.events, "event-" + eventName) + let contractObserver = fromEvent(this.events, "event-" + eventName + "-" + namespace) // TODO: this should be moved to a 'smart' module // for e.g, it should start fromBlock, from the latest known block (which means it should store block info) // it should be able to do events X at the time to avoid slow downs as well as the 10k limit contractInstance.events[eventName].apply(contractInstance.events[eventName], [(filterConditions || {fromBlock: 0}), (err, event) => { - let propsToFilter = []; - for (let prop in filterConditions.filter) { - if (Object.keys(event.returnValues).indexOf(prop) >= 0) { - propsToFilter.push(prop) + if (filterConditions) { + let propsToFilter = []; + for (let prop in filterConditions.filter) { + if (Object.keys(event.returnValues).indexOf(prop) >= 0) { + propsToFilter.push(prop) + } + } + if (propsToFilter.length === 0) { + return this.events.emit("event-" + eventName + "-" + namespace, event); } - } - if (propsToFilter.length === 0) { - return this.events.emit("event-" + eventName, event); - } - for (let prop of propsToFilter) { - if (filterConditions.filter[prop] !== event.returnValues[prop]) { - return; + for (let prop of propsToFilter) { + if (filterConditions.filter[prop] !== event.returnValues[prop]) { + return; + } } } - this.events.emit("event-" + eventName, event); + this.events.emit("event-" + eventName + "-" + namespace, event); }]) // contractObserver.pipe(filter((x) => x.id > lastEvent.id)).pipe(filter(filterConditions)).subscribe((e) => { @@ -100,12 +107,15 @@ class EventSyncer { console.dir("event already synced: " + e.id) } else { // TODO: would be nice if trackEvent was smart enough to understand the type of returnValues and do the needed conversions + if (e.returnValues['$loki']) { // was already saved / synced + console.dir("already synced") + return sub.next(e.returnValues) + } children.insert(e.returnValues) tracked.updateWhere(((x) => x.eventName === eventName), ((x) => x.id = e.id)) this.events.emit("updateDB") sub.next(e.returnValues) } - console.dir("-------"); }) return sub; From 668b7ef65db6f7b4d23407f13b8f1a0e088cd363 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 19 Aug 2019 14:30:38 -0400 Subject: [PATCH 3/3] 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) });