I am currently in the process of refactoring some code to utilize RXJS and the angular async pipe. The task at hand involves adding and removing items from an array. I'm wondering if there might be a more efficient approach to manipulating a shared array other than using tap to set store the state in a BehaviorSubject.
this.listSubject = new BehaviorSubject(['a','b','c']);
this.addSubject = new Subject<string>();
this.addAction$ = this.addSubject.asObservable().pipe(
withLatestForm(this.listSubject),
map(([itemToAdd, list]) => {list.push(itemToAdd); return list;}),
tap((data) => this.listSubject.next(data))
);
this.removeSubject = new Subject<number>();
this.removeAction$ = this.removeSubject.asObservable().pipe(
withLatestForm(this.listSubject),
map(([indexToRemove, list]) => {list.splice(indexToRemove, 1); return list;}),
tap((data) => this.listSubject.next(data))
);
this.list$ = merge(this.addAction$, this.removeAction$);
EDIT: The UI code is using async pipe,
list$ | async