diff --git a/src/index.js b/src/index.js index 1cac4a8..5deec59 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,4 @@ -const EventSyncer = require('./eventSyncer.js') +const EventSyncer = require('./eventSyncer.js'); +EventSyncer.operators = require('./operators'); -const eventSyncer = new EventSyncer(web3); - -eventSyncer.init(() => {}); - - // eventSyncer.trackEvent('contractEvent', ((x) => x.from === "0x123")).pipe(map(x => x.rating), myscan, mymap).subscribe((v) => { - // console.dir("current average is " + v) - // }) - - -// return Event +module.exports = EventSyncer; diff --git a/src/operators.js b/src/operators.js new file mode 100644 index 0000000..eacd559 --- /dev/null +++ b/src/operators.js @@ -0,0 +1,44 @@ +const { of, pipe } = require('rxjs'); +const { map, pluck, reduce } = require('rxjs/operators'); + +function $takeProps() { + const args = Object.values(arguments); + return pipe( + map(v => { + const r = {}; + args.forEach(a => { + r[a] = v[a]; + }); + return r; + }), + ); +} + +/* +// ex 1 +of({a: 1, b:2, e: 1}, {a: 0, c: 1, b:3}, {a: 0, d: 1, b:1}) + .pipe($takeProps("a", "b", "e")) + .subscribe((v) => console.log(v)); +*/ + +function $average() { + return pipe( + reduce((accum, curr) => ({ + sum: accum.sum + curr, + count: accum.count + 1 + }), { sum: 0, count: 0 }), + map(o => o.sum / o.count) + ); +} + +/* +of(10, 3, 4) + .pipe($average()) + .subscribe((v) => console.log(v)); +*/ + + +module.exports = { + $takeProps, + $average +}; \ No newline at end of file diff --git a/webpack.common.js b/webpack.common.js index 0b5f294..74f743a 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -2,7 +2,7 @@ const path = require('path'); const webConfig = { target: 'web', - entry: path.join(__dirname, "src/eventSyncer.js"), + entry: path.join(__dirname, "src/index.js"), output: { path: path.resolve(__dirname, "dist"), filename: 'browser.js',