2018-10-10 14:44:44 +00:00
|
|
|
import * as plugins from './smartrx.plugins';
|
|
|
|
import { Observable, fromEvent } from 'rxjs';
|
|
|
|
import { Objectmap, Stringmap } from '@pushrocks/lik';
|
2017-10-26 20:56:01 +00:00
|
|
|
/**
|
|
|
|
* bundles an observable with an emitter
|
|
|
|
*/
|
|
|
|
export interface ObservableEmitterBundle {
|
2018-10-10 14:44:44 +00:00
|
|
|
observable: plugins.rxjs.Observable<any>;
|
|
|
|
emitter: plugins.events.EventEmitter;
|
|
|
|
event: string;
|
2017-10-26 20:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* manages observables by making sure that only one observable is regsitered per event
|
|
|
|
*/
|
|
|
|
export class Observablemap {
|
2018-10-10 14:44:44 +00:00
|
|
|
ObservableEmitterBundleObjectmap = new Objectmap<ObservableEmitterBundle>();
|
2017-10-26 20:56:01 +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.
|
|
|
|
*/
|
2018-10-10 14:44:44 +00:00
|
|
|
getObservableForEmitterEvent(emitterArg: plugins.events.EventEmitter, eventArg: string) {
|
|
|
|
let existingBundle = this.ObservableEmitterBundleObjectmap.find(bundleArg => {
|
|
|
|
return bundleArg.emitter === emitterArg && bundleArg.event === eventArg;
|
|
|
|
});
|
2017-10-26 20:56:01 +00:00
|
|
|
if (existingBundle) {
|
2018-10-10 14:44:44 +00:00
|
|
|
return existingBundle.observable;
|
2017-10-26 20:56:01 +00:00
|
|
|
} else {
|
2018-10-10 14:44:44 +00:00
|
|
|
let emitterObservable = fromEvent(emitterArg, eventArg);
|
2017-10-26 20:56:01 +00:00
|
|
|
this.ObservableEmitterBundleObjectmap.add({
|
|
|
|
observable: emitterObservable,
|
|
|
|
emitter: emitterArg,
|
|
|
|
event: eventArg
|
2018-10-10 14:44:44 +00:00
|
|
|
});
|
|
|
|
return emitterObservable;
|
2017-10-26 20:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|