parent
41e41683a0
commit
f9e71b443c
|
@ -21,14 +21,15 @@ console.log('--------------------------')
|
||||||
|
|
||||||
example.on('changed', prop => console.log('emitted----a property changed-----',prop))
|
example.on('changed', prop => console.log('emitted----a property changed-----',prop))
|
||||||
example.on('test', (val) => console.log('emitted value of test',val))
|
example.on('test', (val) => console.log('emitted value of test',val))
|
||||||
|
|
||||||
console.log('making \'test\' reactive with default amend of add 3')
|
console.log('making \'test\' reactive with default amend of add 3')
|
||||||
example.rxAdd('test')
|
example.rxAdd('test')
|
||||||
|
example.rxSubscribe('test','early',(prop)=>console.log('>>>>>>before test changed subscription - grabs initial value',prop))
|
||||||
console.log('changing test to 20, 30 directly')
|
console.log('changing test to 20, 30 directly')
|
||||||
|
example.rxUnsubscribe('test','early')
|
||||||
example.test = 20
|
example.test = 20
|
||||||
example.rxAmendValue() // removes global amend
|
example.rxAmendValue() // removes global amend
|
||||||
example.test = 30
|
example.test = 30
|
||||||
example.rxSubscribe('test','custom',(prop)=>console.log('#############late subscription to \'test\' after 30 ############',prop))
|
example.rxSubscribe('test','late',(prop)=>console.log('#############late subscription to \'test\' after 30 ############',prop))
|
||||||
console.log('amending emitted value with path',example.rxAmendValue('test','path'))
|
console.log('amending emitted value with path',example.rxAmendValue('test','path'))
|
||||||
console.log('changing test to 40 via set')
|
console.log('changing test to 40 via set')
|
||||||
example.set('test',40)
|
example.set('test',40)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@uci-utils/rx-class",
|
"name": "@uci-utils/rx-class",
|
||||||
"version": "0.1.3",
|
"version": "0.1.4",
|
||||||
"description": "class that support reactive properites",
|
"description": "class that support reactive properites",
|
||||||
"main": "src/rx-class.js",
|
"main": "src/rx-class.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -15,11 +15,12 @@ export default class RxClass extends EventEmitter {
|
||||||
super(opts)
|
super(opts)
|
||||||
const rxopts = opts._rx_||{}
|
const rxopts = opts._rx_||{}
|
||||||
this._rx_ = {
|
this._rx_ = {
|
||||||
event: rxopts.event || 'changed',
|
emitter: rxopts.emitter !=null ? rxopts.emitter : true,
|
||||||
|
event: rxopts.event === false ? false : rxopts.event || 'changed',
|
||||||
handler: rxopts.handler,
|
handler: rxopts.handler,
|
||||||
props: {},
|
props: {},
|
||||||
amendValue: rxopts.amendValue,
|
amendValue: rxopts.amendValue,
|
||||||
emit_path : rxopts.emit_path,
|
amend_path : rxopts.amend_path, // by default amend value with path
|
||||||
hooks: [], // will add to all setters
|
hooks: [], // will add to all setters
|
||||||
root: '', // root path/namespace to added to all get/set paths
|
root: '', // root path/namespace to added to all get/set paths
|
||||||
operators:[], // will add to all pipes
|
operators:[], // will add to all pipes
|
||||||
|
@ -74,7 +75,8 @@ export default class RxClass extends EventEmitter {
|
||||||
rx.value = opts.value != null ? opts.value : value
|
rx.value = opts.value != null ? opts.value : value
|
||||||
rx.path = path
|
rx.path = path
|
||||||
rx.amendValue = opts.amendValue || this._rx_.amendValue || ((val) => val)
|
rx.amendValue = opts.amendValue || this._rx_.amendValue || ((val) => val)
|
||||||
if (opts.emit_path || this._rx_.emit_path) rx.amendValue = (value,path) => {return {value:value, path: path}}
|
if (opts.amend_path || this._rx_.amend_path) rx.amendValue = (value,path) => {return {value:value, path: path}}
|
||||||
|
// console.log(path,': initial value===>',rx.amendValue(rx.value,rx.path))
|
||||||
rx.obs = from(new BehaviorSubject(rx.amendValue(rx.value,rx.path)).pipe(
|
rx.obs = from(new BehaviorSubject(rx.amendValue(rx.value,rx.path)).pipe(
|
||||||
// rx.obs = from(new ReplaySubject(1).pipe(
|
// rx.obs = from(new ReplaySubject(1).pipe(
|
||||||
skip(this._rx_.skip),
|
skip(this._rx_.skip),
|
||||||
|
@ -99,9 +101,12 @@ export default class RxClass extends EventEmitter {
|
||||||
rx.value = value
|
rx.value = value
|
||||||
value = rx.amendValue(value,path)
|
value = rx.amendValue(value,path)
|
||||||
rx.obs.next(value)
|
rx.obs.next(value)
|
||||||
self.emit(opts.event || self._rx_.event,value,path)
|
if (self._rx_.emitter) {
|
||||||
|
if (opts.event) self.emit(opts.event,value)
|
||||||
|
if (self._rx_.event) self.emit(self._rx_.event,value,path)
|
||||||
self.emit(path,value)
|
self.emit(path,value)
|
||||||
self.emit(name,value,path)
|
self.emit(name,value,path)
|
||||||
|
}
|
||||||
// any hook function that already bound will use that context
|
// any hook function that already bound will use that context
|
||||||
self._rx_.hooks.forEach(hook=>hook.call(self,value))
|
self._rx_.hooks.forEach(hook=>hook.call(self,value))
|
||||||
rx.hooks.forEach(hook=>hook.call(self,value))
|
rx.hooks.forEach(hook=>hook.call(self,value))
|
||||||
|
@ -166,17 +171,31 @@ export default class RxClass extends EventEmitter {
|
||||||
// console.log(this.id,'making subscription',name,rxObj.path || rxObj,!!handler)
|
// console.log(this.id,'making subscription',name,rxObj.path || rxObj,!!handler)
|
||||||
// }
|
// }
|
||||||
if (typeof name !== 'string') return false
|
if (typeof name !== 'string') return false
|
||||||
rxObj = (typeof rxObj === 'string' || Array.isArray(rxObj) ) ? this._rxGetObj(rxObj) : rxObj
|
const rx = (typeof rxObj === 'string' || Array.isArray(rxObj) ) ? this._rxGetObj(rxObj) : rxObj
|
||||||
// if (name && name!=='_default' && (rxObj||{}).path) console.log('rx object for',rxObj.path)
|
// if (name && name!=='_default' && (rxObj||{}).path) console.log('rx object for',rxObj.path)
|
||||||
if ((rxObj||{}).path) {
|
if ((rx||{}).path) {
|
||||||
if (typeof handler==='function') {
|
if (typeof handler==='function') {
|
||||||
// console.log('-----------subscription made-----------')
|
rx.subs[name] = rx.obs.subscribe(handler)
|
||||||
return rxObj.subs[name] = rxObj.obs.subscribe(handler)
|
// console.log(rx.path, 'current subscriptions---',Object.keys(rx.subs))
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rxUnsubscribe(rxObj,name){
|
||||||
|
if (typeof name !== 'string') return false
|
||||||
|
const rx = (typeof rxObj === 'string' || Array.isArray(rxObj) ) ? this._rxGetObj(rxObj) : rxObj
|
||||||
|
// if (name && name!=='_default' && (rxObj||{}).path) console.log('rx object for',rxObj.path)
|
||||||
|
if (rx.subs[name]){
|
||||||
|
rx.subs[name].unsubscribe()
|
||||||
|
delete rx.subs[name]
|
||||||
|
// console.log(rx.path, ':current subscriptions>',Object.keys(rx.subs))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// takes several formats for a path to an objects property and return only the . string
|
// takes several formats for a path to an objects property and return only the . string
|
||||||
formatObjPath(path) {
|
formatObjPath(path) {
|
||||||
if (path==null) return path
|
if (path==null) return path
|
||||||
|
@ -243,6 +262,7 @@ export default class RxClass extends EventEmitter {
|
||||||
// console.log(path,value,opts,!this.isRx(path))
|
// console.log(path,value,opts,!this.isRx(path))
|
||||||
if (!opts.noRx && !this.isRx(path)) {
|
if (!opts.noRx && !this.isRx(path)) {
|
||||||
opts = Object.assign(isPlainObject(value) ? {values:value,traverse:true} : {value:value},opts)
|
opts = Object.assign(isPlainObject(value) ? {values:value,traverse:true} : {value:value},opts)
|
||||||
|
// console.log('set opts', opts)
|
||||||
this.rxAdd(path,opts)
|
this.rxAdd(path,opts)
|
||||||
} else {
|
} else {
|
||||||
const curValue = this.$get(path)
|
const curValue = this.$get(path)
|
||||||
|
|
Loading…
Reference in New Issue