nope/lib/helpers/getSubject.ts

52 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-11-14 22:16:07 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
*/
import { BehaviorSubject, ReplaySubject, Subject } from "rxjs";
export interface TSubjectOptions {
/**
* Definition whether to show the current value on subscription.
2021-11-14 22:16:07 +00:00
*
* @author M.Karkowski
* @type {boolean}
* @memberof TSubjectOptions
*/
2021-12-04 07:25:26 +00:00
showCurrent?: boolean;
2021-11-14 22:16:07 +00:00
/**
* Definition, whether to playback the history every
* time or not.
*
* @author M.Karkowski
* @type {boolean}
* @memberof TSubjectOptions
*/
playHistory?: boolean;
}
/**
* Helper to define the correct RXJS Subject.
*
* @author M.Karkowski
* @export
* @template T
* @param {{
* useHistory?: boolean,
* playBackhistoryEveryTime?: boolean;
* }} [options={}]
2021-12-04 07:25:26 +00:00
* @return {*}
2021-11-14 22:16:07 +00:00
*/
2021-12-04 07:25:26 +00:00
export function getSubject<T>(
options: TSubjectOptions = {}
): Subject<T> | ReplaySubject<T> | BehaviorSubject<T> {
2021-11-14 22:16:07 +00:00
if (options.showCurrent) {
if (options.playHistory) {
return new ReplaySubject<T>();
}
return new BehaviorSubject<T>(undefined);
}
return new Subject<T>();
2021-12-04 07:25:26 +00:00
}