From f9e71b443c360329f617172af19d7c443e713218 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Wed, 17 Jun 2020 11:56:48 -0700 Subject: [PATCH] 0.1.4 added option to disable emitter added unsubscribe method --- examples/example.js | 5 +++-- package.json | 2 +- src/rx-class.js | 40 ++++++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/examples/example.js b/examples/example.js index fcb0a2e..14544a0 100644 --- a/examples/example.js +++ b/examples/example.js @@ -21,14 +21,15 @@ console.log('--------------------------') example.on('changed', prop => console.log('emitted----a property changed-----',prop)) example.on('test', (val) => console.log('emitted value of test',val)) - console.log('making \'test\' reactive with default amend of add 3') 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') +example.rxUnsubscribe('test','early') example.test = 20 example.rxAmendValue() // removes global amend 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('changing test to 40 via set') example.set('test',40) diff --git a/package.json b/package.json index 312e165..6132b19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci-utils/rx-class", - "version": "0.1.3", + "version": "0.1.4", "description": "class that support reactive properites", "main": "src/rx-class.js", "scripts": { diff --git a/src/rx-class.js b/src/rx-class.js index c394db2..853235a 100644 --- a/src/rx-class.js +++ b/src/rx-class.js @@ -15,11 +15,12 @@ export default class RxClass extends EventEmitter { super(opts) const rxopts = opts._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, props: {}, 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 root: '', // root path/namespace to added to all get/set paths 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.path = path 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 ReplaySubject(1).pipe( skip(this._rx_.skip), @@ -99,9 +101,12 @@ export default class RxClass extends EventEmitter { rx.value = value value = rx.amendValue(value,path) rx.obs.next(value) - self.emit(opts.event || self._rx_.event,value,path) - self.emit(path,value) - self.emit(name,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(name,value,path) + } // any hook function that already bound will use that context self._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) // } 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 ((rxObj||{}).path) { + if ((rx||{}).path) { if (typeof handler==='function') { - // console.log('-----------subscription made-----------') - return rxObj.subs[name] = rxObj.obs.subscribe(handler) + rx.subs[name] = rx.obs.subscribe(handler) + // console.log(rx.path, 'current subscriptions---',Object.keys(rx.subs)) + return true } } 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 formatObjPath(path) { if (path==null) return path @@ -243,6 +262,7 @@ export default class RxClass extends EventEmitter { // console.log(path,value,opts,!this.isRx(path)) if (!opts.noRx && !this.isRx(path)) { opts = Object.assign(isPlainObject(value) ? {values:value,traverse:true} : {value:value},opts) + // console.log('set opts', opts) this.rxAdd(path,opts) } else { const curValue = this.$get(path)