Hi, I would like to perform some chained effects on elements based on the status of my modal
To chain calls, I would like to use concatMap to queue execution, but also iif to conditionally run one pipeline or another
So far I am using BehaviorSubjects to achieve the following:
readonly #closeModal$ = new Subject<void>();
readonly #openModal$ = new Subject<void>();
constructor() {
this.#closeModal$
.pipe(
takeUntilDestroyed(),
tap(() => this.#htmlEl.classList.add("modal-is-closing")),
delay(modalAnimationDuration),
tap(() => this.isModalOpen.set(false)),
tap(() => this.#htmlEl.classList.remove("modal-is-open", "modal-is-closing"))
)
.subscribe();
this.#openModal$
.pipe(
takeUntilDestroyed(),
tap(() => this.isModalOpen.set(true)),
tap(() => this.#htmlEl.classList.add("modal-is-open", "modal-is-opening")),
delay(modalAnimationDuration),
tap(() => this.#htmlEl.classList.remove("modal-is-opening"))
)
.subscribe();
toObservable(this.open)
.pipe(
takeUntilDestroyed(),
skip(1),
// 👇 This does not queue anything
tap((shouldOpen) => (shouldOpen ? this.#openModal$.next() : this.#closeModal$.next())),
)
.subscribe();
}
However, this is not taking advantage of RxJS and instead calls tap, how can I change this so that my observable can look like the following:
toObservable(this.open)
.pipe(
takeUntilDestroyed(),
skip(1),
// 👇 No more `tap` but rather Observables branching
concatMap((shouldOpen) => iif(() => shouldOpen, this.#closeModal$, this.#openModal$))
)
.subscribe();
Thanks for the help and suggestions!