updated deps - couple minor fixes - working - before adding traverse

master
David Kebler 2020-06-07 08:47:24 -07:00
parent a67eff8eea
commit 95412d6d8d
4 changed files with 44 additions and 25 deletions

View File

@ -1,14 +1,26 @@
import DataStore from '../src/datastore' import Store from '../src/datastore'
const store = new Store({
path: process.cwd() + '/examples/foo.json'
})
// let circuits = await this.db.getAll('circuits') const foo = {bar:3, bash:{bin:2,what:5}}
// console.log(circuits)
// // let obs = this.db.getStore('circuits').addObserver(undefined,'bogus') store.set('foo',foo)
// let obs = this.db.getStore('circuits').addObserver('uy0cADaONfPv38th.prop1',{prop1:'test'}) // store.set('foo.bash',3)
// console.log(this.db.getStore('circuits')._observers) // store.set('foo.what',5)
// obs.subscribe(console.log)
// obs.next({prop1:'test'}) console.log('simple get', store.get('foo'))
// obs.next({prop1:'test'})
// obs.next({prop1:'test'}) console.log('current observers', store.getObs())
// obs.next({prop1:'test'})
// obs.next({prop1:'test2'}) // let temp = store.setObs('foo.bar')
let obs = store.setObs('foo.bar')
obs.subscribe(val=>console.log('foo bar handler 1',val))
obs.subscribe(val=>console.log('foo bar handler 2',val))
console.log('current observers', store._observers)
store.set('foo.bar',8)

3
nodemon.json Normal file
View File

@ -0,0 +1,3 @@
{
"ignore":["examples/*.json"]
}

View File

@ -23,17 +23,17 @@
"homepage": "https://github.com/uCOMmandIt/uci-utils#readme", "homepage": "https://github.com/uCOMmandIt/uci-utils#readme",
"dependencies": { "dependencies": {
"data-store": "^4.0.3", "data-store": "^4.0.3",
"deep-equal": "^2.0.1", "deep-equal": "^2.0.3",
"get-value": "^3.0.1", "get-value": "^3.0.1",
"is-plain-object": "^3.0.0", "is-plain-object": "^3.0.0",
"rxjs": "^6.5.4", "rxjs": "^6.5.5",
"set-value": "^3.0.1", "set-value": "^3.0.2",
"unset-value": "^1.0.0" "unset-value": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"chai": "^4.2.0", "chai": "^4.2.0",
"esm": "^3.2.25", "esm": "^3.2.25",
"mocha": "^6.2.2", "mocha": "^7.2.0",
"nodemon": "^1.19.4" "nodemon": "^2.0.4"
} }
} }

View File

@ -1,6 +1,7 @@
import { Store } from 'data-store' import { Store } from 'data-store'
import { BehaviorSubject, Subject, from } from 'rxjs' import { BehaviorSubject, Subject, from } from 'rxjs'
import { distinctUntilChanged, takeUntil } from 'rxjs/operators' import { distinctUntilChanged, takeUntil } from 'rxjs/operators'
import traverse from 'traverse'
import _get from 'get-value' import _get from 'get-value'
import _set from 'set-value' import _set from 'set-value'
import _del from 'unset-value' import _del from 'unset-value'
@ -17,12 +18,16 @@ export default class StoreRx extends Store {
this._deleted = {} this._deleted = {}
this.emitter = opts.emitter this.emitter = opts.emitter
this.event = opts.event || 'datastore' this.event = opts.event || 'datastore'
this.handler=opts.handler this.handler=opts.handler // common handler
this.get = this.get.bind(this) this.get = this.get.bind(this)
this.set = this.set.bind(this) this.set = this.set.bind(this)
this.makeObs = opts.makeObs // if set to true set will create observer
// todo recreate any observers/subscriptions here
} }
// TODO manage all subscriptions // TODO manage all subscriptions,
// save handlers for all subscriptions in
// recreate all saved subscriptions at instantiation
getObs(path) { getObs(path) {
path = this.formatPath(path) path = this.formatPath(path)
@ -37,10 +42,10 @@ export default class StoreRx extends Store {
_del(this._observers,path) _del(this._observers,path)
} }
setObs(path) { setObs(path, handler) {
path = this.formatPath(path) path = this.formatPath(path)
const obs = this.getObs(path) const obs = this.getObs(path)
if (obs) return obs if (obs && !handler) return obs
_set(this._deleted,path,from(new Subject())) _set(this._deleted,path,from(new Subject()))
_set(this._observers,path,from(new BehaviorSubject(super.get(path))).pipe( _set(this._observers,path,from(new BehaviorSubject(super.get(path))).pipe(
distinctUntilChanged(), distinctUntilChanged(),
@ -50,13 +55,12 @@ export default class StoreRx extends Store {
} }
get(path,handler){ get(path,handler){
// console.log('get>>>>',path,handler) if (handler) console.log('get>>>>',path,handler)
// console.log(this._observers) // console.log(this._observers)
path = this.formatPath(path) path = this.formatPath(path)
if (typeof handler==='function') { if (typeof handler==='function') {
let obs = this.getObs(path) let obs = this.getObs(path)
if (!obs) obs = this.setObs(path) if (!obs) obs = this.setObs(path)
// console.log(obs)
return obs.subscribe(handler) return obs.subscribe(handler)
} }
return super.get(path) return super.get(path)
@ -72,10 +76,10 @@ export default class StoreRx extends Store {
// console.log('set>>>>',path,curValue,value) // console.log('set>>>>',path,curValue,value)
if (!equal(curValue,value) || !value) { if (!equal(curValue,value) || !value) {
let obs = this.getObs(path) let obs = this.getObs(path)
if (obs) obs.next(value) if (obs) obs.next(value) // makes path reactive via BehaviorSubject
if (this.emit) this.emit(this.event,{path:path,key:path,value:value}) if (this.emit) this.emit(this.event,{path:path,key:path,value:value})
// console.log('pushing in datastore set',path,value) // console.log('pushing in datastore set',path,value)
if (this.handler) this.handler({path:path,key:path,value:value}) if (typeof this.handler==='function') this.handler({path:path,key:path,value:value})
return super.set(path,value) return super.set(path,value)
} }
return this return this