I have the following code snippet in my constructor
to initialize the variables.
public Elements: Observable<dcElementDataStoreItem>;
private elementSubjectData: BehaviorSubject<dcElementDataStoreItem>;
constructor() {
this.elementSubjectData = new BehaviorSubject([]) as BehaviorSubject<ElementDataStoreItem>;
this.Elements = this.elementSubjectData.asObservable();
}
Currently, there are only 2 lines of code for initialization but I may have multiple similar items to set up. Is it possible to optimize it like the proposed code below (although it does not work)?
constructor() {
this.initSubjectData<dcElementDataStoreItem>(this.elementSubjectData, this.Elements);
}
private initSubjectData<T>(privateList: BehaviorSubject<T>, publicList: Observable<T>) {
privateList = new BehaviorSubject([]) as BehaviorSubject<T>;
publicList = privateList.asObservable();
}
This way, I aim to make use of the initSubjectData
method for initializing all other potential elements.