move simulation to its own file
This commit is contained in:
parent
fd28e100ca
commit
f7d19a8871
|
@ -0,0 +1,8 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = false
|
||||
insert_final_newline = false
|
|
@ -1,3 +1,5 @@
|
|||
node_modules
|
||||
.vscode
|
||||
phoenix.db
|
||||
phoenix.db
|
||||
TODO
|
||||
test.js
|
|
@ -1,6 +1,8 @@
|
|||
// var Web3 = require('web3')
|
||||
const Events = require('events')
|
||||
const events = new Events()
|
||||
const Simulator = require('./simulator.js')
|
||||
const simulator = new Simulator(events);
|
||||
|
||||
const { Observable, fromEvent, interval, Subject } = require('rxjs');
|
||||
const { throttle, throttleTime, map, distinctUntilChanged, filter, average, reduce, count, scan} = require('rxjs/operators');
|
||||
|
@ -38,25 +40,6 @@ function run() {
|
|||
db.close()
|
||||
});
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
let dbChanges = fromEvent(events, "updateDB")
|
||||
|
||||
dbChanges.pipe(throttle(val => interval(400))).subscribe(() => {
|
||||
|
@ -115,6 +98,5 @@ function run() {
|
|||
console.dir("current value is " + v)
|
||||
})
|
||||
|
||||
emitEvents()
|
||||
|
||||
simulator.emitEvents()
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
class Simulator {
|
||||
|
||||
constructor(events) {
|
||||
this.events = events;
|
||||
this.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 }
|
||||
]
|
||||
}
|
||||
|
||||
emitEvents() {
|
||||
let i = 0
|
||||
// emit contract event each 1 second
|
||||
setInterval(() => {
|
||||
if (i >= this.contractEvents.length) return
|
||||
this.events.emit("contractEvent", this.contractEvents[i])
|
||||
i += 1
|
||||
}, 1 * 1000)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = Simulator;
|
Loading…
Reference in New Issue