feat: operators

This commit is contained in:
Richard Ramos 2019-08-21 08:32:57 -04:00
parent bb335aa4cb
commit 2badf46e4b
3 changed files with 48 additions and 12 deletions

View File

@ -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;

44
src/operators.js Normal file
View File

@ -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
};

View File

@ -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',