updated deps - couple minor fixes - working - before adding traverse
parent
a67eff8eea
commit
95412d6d8d
|
@ -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')
|
||||
// console.log(circuits)
|
||||
// // let obs = this.db.getStore('circuits').addObserver(undefined,'bogus')
|
||||
// let obs = this.db.getStore('circuits').addObserver('uy0cADaONfPv38th.prop1',{prop1:'test'})
|
||||
// console.log(this.db.getStore('circuits')._observers)
|
||||
// obs.subscribe(console.log)
|
||||
// obs.next({prop1:'test'})
|
||||
// obs.next({prop1:'test'})
|
||||
// obs.next({prop1:'test'})
|
||||
// obs.next({prop1:'test'})
|
||||
// obs.next({prop1:'test2'})
|
||||
const foo = {bar:3, bash:{bin:2,what:5}}
|
||||
|
||||
store.set('foo',foo)
|
||||
// store.set('foo.bash',3)
|
||||
// store.set('foo.what',5)
|
||||
|
||||
console.log('simple get', store.get('foo'))
|
||||
|
||||
console.log('current observers', store.getObs())
|
||||
|
||||
// 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)
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"ignore":["examples/*.json"]
|
||||
}
|
10
package.json
10
package.json
|
@ -23,17 +23,17 @@
|
|||
"homepage": "https://github.com/uCOMmandIt/uci-utils#readme",
|
||||
"dependencies": {
|
||||
"data-store": "^4.0.3",
|
||||
"deep-equal": "^2.0.1",
|
||||
"deep-equal": "^2.0.3",
|
||||
"get-value": "^3.0.1",
|
||||
"is-plain-object": "^3.0.0",
|
||||
"rxjs": "^6.5.4",
|
||||
"set-value": "^3.0.1",
|
||||
"rxjs": "^6.5.5",
|
||||
"set-value": "^3.0.2",
|
||||
"unset-value": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^4.2.0",
|
||||
"esm": "^3.2.25",
|
||||
"mocha": "^6.2.2",
|
||||
"nodemon": "^1.19.4"
|
||||
"mocha": "^7.2.0",
|
||||
"nodemon": "^2.0.4"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { Store } from 'data-store'
|
||||
import { BehaviorSubject, Subject, from } from 'rxjs'
|
||||
import { distinctUntilChanged, takeUntil } from 'rxjs/operators'
|
||||
import traverse from 'traverse'
|
||||
import _get from 'get-value'
|
||||
import _set from 'set-value'
|
||||
import _del from 'unset-value'
|
||||
|
@ -17,12 +18,16 @@ export default class StoreRx extends Store {
|
|||
this._deleted = {}
|
||||
this.emitter = opts.emitter
|
||||
this.event = opts.event || 'datastore'
|
||||
this.handler=opts.handler
|
||||
this.handler=opts.handler // common handler
|
||||
this.get = this.get.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) {
|
||||
path = this.formatPath(path)
|
||||
|
@ -37,10 +42,10 @@ export default class StoreRx extends Store {
|
|||
_del(this._observers,path)
|
||||
}
|
||||
|
||||
setObs(path) {
|
||||
setObs(path, handler) {
|
||||
path = this.formatPath(path)
|
||||
const obs = this.getObs(path)
|
||||
if (obs) return obs
|
||||
if (obs && !handler) return obs
|
||||
_set(this._deleted,path,from(new Subject()))
|
||||
_set(this._observers,path,from(new BehaviorSubject(super.get(path))).pipe(
|
||||
distinctUntilChanged(),
|
||||
|
@ -50,13 +55,12 @@ export default class StoreRx extends Store {
|
|||
}
|
||||
|
||||
get(path,handler){
|
||||
// console.log('get>>>>',path,handler)
|
||||
if (handler) console.log('get>>>>',path,handler)
|
||||
// console.log(this._observers)
|
||||
path = this.formatPath(path)
|
||||
if (typeof handler==='function') {
|
||||
let obs = this.getObs(path)
|
||||
if (!obs) obs = this.setObs(path)
|
||||
// console.log(obs)
|
||||
return obs.subscribe(handler)
|
||||
}
|
||||
return super.get(path)
|
||||
|
@ -72,10 +76,10 @@ export default class StoreRx extends Store {
|
|||
// console.log('set>>>>',path,curValue,value)
|
||||
if (!equal(curValue,value) || !value) {
|
||||
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})
|
||||
// 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 this
|
||||
|
|
Loading…
Reference in New Issue