2020-06-07 21:56:54 -07:00
|
|
|
import RxClass from '../src/rx-class.js'
|
|
|
|
|
|
|
|
class Example extends RxClass {
|
|
|
|
constructor(opts={}){
|
|
|
|
super(opts)
|
|
|
|
this.test = 10
|
|
|
|
this.another = { bar:'bust', foo:3}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const example = new Example({
|
|
|
|
_rx_ : {
|
2020-06-15 14:43:09 -07:00
|
|
|
handler: value => {console.log('-----------default subscription handler-------', value)},
|
|
|
|
amendValue: val => val + 3
|
2020-06-07 21:56:54 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
console.log('instance before doing rx on properties\n')
|
|
|
|
console.log(example)
|
|
|
|
console.log('--------------------------')
|
|
|
|
|
2020-06-16 09:53:36 -07:00
|
|
|
example.on('changed', prop => console.log('emitted----a property changed-----',prop))
|
2020-06-07 21:56:54 -07:00
|
|
|
example.on('test', (val) => console.log('emitted value of test',val))
|
|
|
|
|
2020-06-15 14:43:09 -07:00
|
|
|
console.log('making \'test\' reactive with default amend of add 3')
|
2020-06-07 21:56:54 -07:00
|
|
|
example.rxAdd('test')
|
|
|
|
console.log('changing test to 20, 30 directly')
|
|
|
|
example.test = 20
|
2020-06-15 14:43:09 -07:00
|
|
|
example.rxAmendValue() // removes global amend
|
2020-06-07 21:56:54 -07:00
|
|
|
example.test = 30
|
2020-06-15 10:08:52 -07:00
|
|
|
example.rxSubscribe('test','custom',(prop)=>console.log('#############late subscription to \'test\' after 30 ############',prop))
|
2020-06-15 14:43:09 -07:00
|
|
|
console.log('amending emitted value with path',example.rxAmendValue('test','path'))
|
2020-06-08 14:32:52 -07:00
|
|
|
console.log('changing test to 40 via set')
|
|
|
|
example.set('test',40)
|
2020-06-15 14:43:09 -07:00
|
|
|
console.log(
|
|
|
|
'amending emitted value custom function',
|
|
|
|
example.rxAmendValue('test',val => { return { value:val+5, msg:'additional 5 was added to set value'}})
|
|
|
|
)
|
|
|
|
example.set('test',50)
|
|
|
|
|
|
|
|
console.log('removing amend function ',example.rxAmendValue('test',null))
|
|
|
|
example.set('test',60)
|
2020-06-07 21:56:54 -07:00
|
|
|
console.log('making a deeper property \'another.foo\' reactive')
|
2020-06-15 14:43:09 -07:00
|
|
|
console.log('changing any default amend function ',example.rxAmendValue(val=> val + ':amended'))
|
2020-06-07 21:56:54 -07:00
|
|
|
example.rxAdd('another.foo')
|
|
|
|
|
2020-06-16 09:53:36 -07:00
|
|
|
example.on('foo', (val,path) => { console.log('emitted via just \'foo\' ',val,'path is also emitted:',path)})
|
|
|
|
example.on('another.foo', (val,path) => { console.log('emitted via just \'another.foo\',',val,'path is undefined',path)})
|
|
|
|
|
2020-06-07 21:56:54 -07:00
|
|
|
example.another.foo = 7
|
|
|
|
|
|
|
|
console.log('direct access of another.foo',example.another.foo)
|
2020-06-08 14:32:52 -07:00
|
|
|
console.log('access via get of another.foo',example.get('another.foo'))
|
2020-06-07 21:56:54 -07:00
|
|
|
|
2020-06-15 10:08:52 -07:00
|
|
|
console.log('making a deeper property \'some.thing\' reactive that did not exist by setting property')
|
|
|
|
example.set('some.thing','this will be reactive')
|
|
|
|
console.log(example.$get('some'))
|
|
|
|
console.log(example.get('some'))
|
|
|
|
example.set('some.thing','still reactive')
|
|
|
|
example.some.thing='directly assigned'
|
2020-06-07 21:56:54 -07:00
|
|
|
console.log('--------------------------')
|
|
|
|
console.log('instance after adding rx to properties\n')
|
|
|
|
console.log(example)
|
|
|
|
console.log('--------------------------')
|
|
|
|
|
|
|
|
console.log('now removing reactivity from \'test\'')
|
2020-06-15 10:08:52 -07:00
|
|
|
example.rxRemove('test',{confirm:true})
|
2020-06-07 21:56:54 -07:00
|
|
|
console.log('\'test\' now holds unreactive value')
|
|
|
|
console.log('--------------------------')
|
|
|
|
console.log('instance after removing rx from \'test\'\n')
|
|
|
|
console.log(example)
|
|
|
|
console.log('--------------------------')
|
2020-06-15 10:08:52 -07:00
|
|
|
console.log('now removing reactivity from \'test\' again')
|
|
|
|
example.rxRemove('test',{confirm:true})
|
2020-06-07 21:56:54 -07:00
|
|
|
|
2020-06-15 10:08:52 -07:00
|
|
|
console.log('---------trying out raw $get and $set-----------')
|
|
|
|
example.a = {}
|
|
|
|
console.log('$set does to make reactive and returns value set\n',example.$set(example.a,'test.object',{do:'dah', bing:'bong'}))
|
|
|
|
console.log('getting directy:',example.a)
|
|
|
|
console.log('using $get with passed object:',example.$get(example.a.test,'object.do'))
|
|
|
|
console.log('deleting do prop only', example.$del(example.a,'test.object.do',true))
|
|
|
|
console.log('it\'s gone see:',example.a)
|
2020-06-16 09:53:36 -07:00
|
|
|
|
|
|
|
example.on('demo',(value) => {
|
|
|
|
console.log('shoud do simple emit of test>', value)
|
|
|
|
})
|
|
|
|
example.emit('demo','test')
|