I have a merged stream of observables,
const nextClickHandler$ = merge(doA$, doB$, doC$)
,
do A$/B$/C$
are observables which makes HTTPRequests or performs some sideeffects, they operate on some mutually exclusive conditions, where either doA$/doB$/doC$ are active and perform the network calls / side-effects.
The use-case is, on the click of the next button, do either A/B/C, I am facing an issue on how to subscribe to this merged observable, if I subscribe to it before-click of next, i.e.
ngOnInit(){
this.nextClickHandler$.subscribe()
}
This will be eagerly evaluating those mutually exclusive conditions and executing, without even clicking next,
I tried subscribing on click of goForward:
goForward(): void {
if (this.getCurrentStep() == 1) {
if (this.validateForm()) {\
//performs side-effects for either A$/B$/C$
this.nextClickHandler$.subscribe();
}
return;
}
But now, when I am on the Step I again, it doesnt wait for the click of the next, since it is a long-living subscription, it just eagerly executes the behaviour, which is not what I want, I want them to be lazy-executed only when clicking next, I tried take(1)
but that does not fix the issue, How can I achieve this?
goForward()
? So your issue is you want to cancel subscription when they go back?goForward()
, but is there a better way than managing the subscriptions manually, I don't like the idea of creating a subscription every time on the click of the next.currentStep$
observable that internally usesswitchMap()
to subscibe to whatever observable is relevant to each step. I can elaborate if you share more details / code. If you could put it in a StackBlitz, that would make it easier to help you. :-)