lik/ts/lik.observablemap.ts

42 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-09-22 10:00:33 +00:00
import * as plugins from './lik.plugins'
import { Objectmap } from './lik.objectmap'
import { Stringmap } from './lik.stringmap'
import { Observable } from 'rxjs/Rx'
/**
* bundles an observable with an emitter
*/
export interface ObservableEmitterBundle {
2017-06-18 11:13:12 +00:00
observable: plugins.rx.Observable<any>
emitter: plugins.events.EventEmitter
event: string
2016-09-22 10:00:33 +00:00
}
/**
* manages observables by making sure that only one observable is regsitered per event
*/
2016-09-22 10:00:33 +00:00
export class Observablemap {
2017-06-18 11:13:12 +00:00
ObservableEmitterBundleObjectmap = new Objectmap<ObservableEmitterBundle>()
2016-09-22 10:00:33 +00:00
2017-06-18 11:13:12 +00:00
/**
* creates a new observable if not yet registered for the same event.
* In case event has been registered before the same observable is returned.
*/
getObservableForEmitterEvent (emitterArg: plugins.events.EventEmitter, eventArg: string) {
let existingBundle = this.ObservableEmitterBundleObjectmap.find((bundleArg) => {
return (bundleArg.emitter === emitterArg && bundleArg.event === eventArg)
})
if (existingBundle) {
return existingBundle.observable
} else {
let emitterObservable = plugins.rx.Observable.fromEvent(emitterArg, eventArg)
this.ObservableEmitterBundleObjectmap.add({
observable: emitterObservable,
emitter: emitterArg,
event: eventArg
})
return emitterObservable
2016-09-22 10:00:33 +00:00
}
2017-06-18 11:13:12 +00:00
}
2016-09-22 10:00:33 +00:00
}