smartrx/ts/smartrx.classes.observablemap.ts
2022-01-24 07:30:54 +01:00

44 lines
1.4 KiB
TypeScript

import * as plugins from './smartrx.plugins';
import * as rxjs from './smartrx.plugins.rxjs';
export interface IEventEmitter<T = any> {
on: (eventNameArg: string, eventHandlerArg: (eventPayload: T) => any) => void;
}
/**
* bundles an observable with an emitter
*/
export interface IObservableEmitterBundle {
observable: rxjs.Observable<any>;
emitter: IEventEmitter<unknown>;
event: string;
}
/**
* manages observables by making sure that only one observable is regsitered per event
*/
export class Observablemap {
public observableEmitterBundleObjectmap = new plugins.lik.ObjectMap<IObservableEmitterBundle>();
/**
* creates a new observable if not yet registered for the same event.
* In case event has been registered before the same observable is returned.
*/
public getObservableForEmitterEvent<T>(emitterArg: IEventEmitter<T>, eventArg: string) {
const existingBundle = this.observableEmitterBundleObjectmap.findSync((bundleArg) => {
return bundleArg.emitter === emitterArg && bundleArg.event === eventArg;
});
if (existingBundle) {
return existingBundle.observable;
} else {
const emitterObservable = rxjs.fromEvent<T>(emitterArg as any, eventArg);
this.observableEmitterBundleObjectmap.add({
observable: emitterObservable,
emitter: emitterArg,
event: eventArg,
});
return emitterObservable;
}
}
}