poc (almost)

This commit is contained in:
Iuri Matias 2019-05-31 15:47:02 -04:00
parent 87b6a56f45
commit 4f21c16f62
4 changed files with 2428 additions and 5 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

63
index.js Normal file
View File

@ -0,0 +1,63 @@
// var Web3 = require('web3')
const Events = require('events')
const events = new Events()
const { Observable, fromEvent, interval, Subject } = require('rxjs');
const { throttle, map, distinctUntilChanged, filter, average, reduce, count} = require('rxjs/operators');
var loki = require('lokijs')
var db = new loki('loki.json')
var children = db.addCollection('children')
// var result = children.insert({name: 'foo', legs: 8})
// console.dir(result)
// var result2 = children.insert({name: 'bar', legs: 8})
// console.dir(result2)
// console.dir(children.get(result2.$loki))
let contractEvents = [
{ id: 1, from: "0x123", type: "Rating", rating: 3 },
{ id: 2, from: "0x123", type: "Rating", rating: 1 },
{ id: 3, from: "0x234", type: "Rating", rating: 5 },
{ id: 4, from: "0x123", type: "Rating", rating: 4 },
{ id: 5, from: "0x123", type: "Rating", rating: 2 },
{ id: 6, from: "0x342", type: "Rating", rating: 2 }
]
function emitEvents() {
let i = 0
// emit contract event each 1 second
setInterval(() => {
if (i >= contractEvents.length) return
events.emit("contractEvent", contractEvents[i])
i += 1
}, 1 * 1000)
}
function trackEvent(eventName, filterConditions) {
let eventKey = eventName + "-from0x123";
let sub = new Subject();
for (let previous of children.find({'eventKey': eventKey})) {
sub.next(previous)
}
let contractObserver = fromEvent(events, eventName)
contractObserver.pipe(filter(filterConditions)).subscribe((e) => {
console.dir("------- syncing event");
e.eventKey = eventKey
console.dir(e);
children.insert(e)
sub.next(e)
// events.emit(eventKey)
console.dir("-------");
})
return sub;
}
trackEvent('contractEvent', ((x) => x.from === "0x123")).pipe(map(x => x.rating)).subscribe((v) => {
console.dir("current value is " + v)
})
emitEvents()

View File

@ -2,10 +2,13 @@
"name": "phoenix", "name": "phoenix",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "test.js", "main": "index.js",
"scripts": { "scripts": {},
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "", "author": "",
"license": "ISC" "license": "MIT",
"dependencies": {
"lokijs": "^1.5.6",
"rxjs": "^6.5.2",
"web3": "1.0.0-beta.37"
}
} }

2356
yarn.lock Normal file

File diff suppressed because it is too large Load Diff