uci-utils-ready/examples/example2.js

118 lines
4.0 KiB
JavaScript

import { Observable } from 'rxjs'
import { Ready, readyChanged as changed, map, tap} from '../src/ready'
import { EventEmitter } from 'events'
let emitter = new EventEmitter()
// let verbose = process.env.VERBOSE==='true'
let combo = false
const late=3000
let example = new Ready({emitter: emitter})
console.log('bound change function for use in customized observer', changed)
console.log('Boolean TEST: {online:\'yes\'}',example.toBoolean({online:'yes'}))
console.log('Boolean TEST: \'yes\'',example.toBoolean('yes'))
let subscribe = ()=>{
console.log('subscribing at',late, 'ms')
example.subscribe(ready => {
console.log(`-----------Subscriber at ${late} ms--------------?`,ready)
console.log('the failed observer:', example.failure, ',details:', example.getObserverDetails(example.failure))
console.log('the total state', example.state)
console.log('---------------------------------------')
})
}
if (!late) subscribe()
else setTimeout(subscribe,late)
const tObs = new Observable(subscriber => {
subscriber.next('on')
subscriber.next('off')
subscriber.next('enabled')
setTimeout(() => {
subscriber.next('F')
}, 7000)
setTimeout(() => {
subscriber.next('T')
}, 8000)
})
example.addObserver('obs',tObs,{details:{desc:'this is a constructed observable from the Observable Class'}})
const tPromise = new Promise(function(resolve) {
setTimeout(()=>{
console.log('promise observer is resolving')
resolve('yes')
},1000)
})
example.addObserver('e',{details:{desc:'this is the e observer which comes from an emitter'}})
// example.addObserver('ce',{condition: (ev)=>ev.ready, event:'ec'})
example.addObserver('ce',{event:'ec'})
example.addObserver('pe',emitter)
example.addObserver('pr',tPromise)
example.addObserver('extnd',example.getObserver('e').pipe(
map(state => {
// console.log('+++++++++++\n','extend before:',state)
if (state) {
let val = Math.floor(Math.random() * Math.floor(10))
// console.log(val)
state = val <11 ? true:false
}
// console.log('extend after:',state,'\n-------------')
return state
})
),{details:'an observer extension of the e observer'})
let madeonly = example.makeObserver({event:'madeonly'})
madeonly ? console.log('observer \'madeonly\' was only made and was not added so won\'t appear in \'all\' list')
: console.log('made observer failed')
console.log('list of observers made and added to the all combination\n',example.observers)
if (combo) {
let combo = example.combineObservers('combo',['e','pr'])
.pipe(
tap(state=>console.log('++++++++++++++ tap log of extened combo e and pr state:+++++++++++++++',state)),
)
if (combo){
example.subscribe('e', ready => console.log('e',ready))
example.subscribe('e', ready => console.log('pr',ready))
console.log('a combination name \'combo\' made of e and pr was created')
console.log('now subscribing to combination e, pr')
example.subscribe('combo', val => console.log('saved combo subscription state:',val))
combo.subscribe(val => console.log('extened combo subscription state:',val))
}
}
console.log('all current combinations',example.combinations)
console.log('------------------starting to emit values--------------------------')
// emitter.emit('ec',{ready:true})
emitter.emit('ec',true)
emitter.emit('pe','yup')
emitter.emit('e')
console.log('===============done initial emitting, pe,e,ec/ce=================')
setTimeout(async () => {
console.log('============emitting e, pe false===================')
example.setObserverDetails('e',{moreinfo:'an additional property of details added later'})
emitter.emit('e',false)
emitter.emit('pe',false)
}
,2000)
setTimeout(async () => {
console.log('=================emitting e true, removing pe which is false ================')
emitter.emit('e',true)
example.removeObserver('pe')
example.subscribe(ready=>console.log('a second all subscriber', ready),{add:true})
}
,4000)
setTimeout(async () => {console.log('timeout done');process.exit()},10000)