move db to eventSyncer
This commit is contained in:
parent
17fbaab75c
commit
8d5467f42f
|
@ -1,13 +1,47 @@
|
|||
const { Observable, fromEvent, interval, Subject, ReplaySubject } = require('rxjs');
|
||||
const { throttle, throttleTime, map, distinctUntilChanged, filter, average, reduce, count, scan } = require('rxjs/operators');
|
||||
const loki = require('lokijs')
|
||||
//const db = new loki('loki.json', {autosave: true, autoload: true})
|
||||
|
||||
class EventSyncer {
|
||||
|
||||
constructor(db, events) {
|
||||
this.db = db;
|
||||
constructor(events) {
|
||||
this.events = events;
|
||||
}
|
||||
|
||||
init(cb) {
|
||||
this.db = new loki('phoenix.db', {
|
||||
autoload: true,
|
||||
autoloadCallback: () => {
|
||||
this.databaseInitialize(cb)
|
||||
},
|
||||
autosave: true,
|
||||
autosaveInterval: 2000 // save every four seconds for our example
|
||||
})
|
||||
}
|
||||
|
||||
databaseInitialize(cb) {
|
||||
let children = this.db.getCollection('children')
|
||||
if (!children) {
|
||||
children = this.db.addCollection('children')
|
||||
this.db.saveDatabase()
|
||||
}
|
||||
let tracked = this.db.getCollection('tracked')
|
||||
if (!tracked) {
|
||||
tracked = this.db.addCollection('tracked')
|
||||
this.db.saveDatabase()
|
||||
}
|
||||
|
||||
let dbChanges = fromEvent(this.events, "updateDB")
|
||||
|
||||
dbChanges.pipe(throttle(val => interval(400))).subscribe(() => {
|
||||
console.dir("saving database...")
|
||||
this.db.saveDatabase()
|
||||
})
|
||||
|
||||
cb();
|
||||
}
|
||||
|
||||
trackEvent(eventName, filterConditions) {
|
||||
let eventKey = eventName + "-from0x123";
|
||||
|
||||
|
@ -51,4 +85,8 @@ class EventSyncer {
|
|||
|
||||
}
|
||||
|
||||
// process.on('exit', function () {
|
||||
// db.close()
|
||||
// });
|
||||
|
||||
module.exports = EventSyncer;
|
44
src/index.js
44
src/index.js
|
@ -6,51 +6,12 @@ const { throttle, throttleTime, map, distinctUntilChanged, filter, average, redu
|
|||
const Simulator = require('./simulator.js')
|
||||
const EventSyncer = require('./eventSyncer.js')
|
||||
|
||||
|
||||
var loki = require('lokijs')
|
||||
//var db = new loki('loki.json', {autosave: true, autoload: true})
|
||||
|
||||
var db = new loki('phoenix.db', {
|
||||
autoload: true,
|
||||
autoloadCallback: databaseInitialize,
|
||||
autosave: true,
|
||||
autosaveInterval: 2000 // save every four seconds for our example
|
||||
})
|
||||
//db.loadDatabase()
|
||||
|
||||
const events = new Events()
|
||||
const simulator = new Simulator(events);
|
||||
const eventSyncer = new EventSyncer(db, events);
|
||||
const eventSyncer = new EventSyncer(events);
|
||||
|
||||
function databaseInitialize() {
|
||||
let children = db.getCollection('children')
|
||||
if (!children) {
|
||||
children = db.addCollection('children')
|
||||
db.saveDatabase()
|
||||
}
|
||||
let tracked = db.getCollection('tracked')
|
||||
if (!tracked) {
|
||||
tracked = db.addCollection('tracked')
|
||||
db.saveDatabase()
|
||||
}
|
||||
run()
|
||||
}
|
||||
eventSyncer.init(run);
|
||||
|
||||
function run() {
|
||||
let children = db.getCollection('children')
|
||||
let tracked = db.getCollection('tracked')
|
||||
|
||||
process.on('exit', function () {
|
||||
db.close()
|
||||
});
|
||||
|
||||
let dbChanges = fromEvent(events, "updateDB")
|
||||
|
||||
dbChanges.pipe(throttle(val => interval(400))).subscribe(() => {
|
||||
console.dir("saving database...")
|
||||
db.saveDatabase()
|
||||
})
|
||||
|
||||
let myscan = scan((acc, curr) => {
|
||||
acc.push(curr);
|
||||
if (acc.length > 4) {
|
||||
|
@ -65,5 +26,6 @@ function run() {
|
|||
console.dir("current value is " + v)
|
||||
})
|
||||
|
||||
const simulator = new Simulator(events);
|
||||
simulator.emitEvents()
|
||||
}
|
Loading…
Reference in New Issue