Do I have to unsubscribe from completed observable in rxjs? - rxjs

If an observable completes, do I still have to unsubscribe / dispose (in RxJS) the observable to remove the Observer (prevent memory leaks) or is this handled internally by Rxjs once a onComplete or onError event occurs?

No, you don't need to unsubscribe from an observable you know has completed.
If you look at the source code of the RxJS toPromise() function, you'll see a subscribe, but no unsubscribe. That's because it is not necessary, you know the observable is completed.

Related

Do we need to unsubscribe to rxjs `from` operator

when converting a promise to an observable with rxjs,
from(myPromise).subscribe(r => console.log(r))
do we need to unsubscribe to it ? or is it safe to assume it will always complete and never cause memory leak ?
In short: No
You do not need to unsubscribe from a promise. A promise should always complete or error, which causes the observable to complete or error as well.
There's no native way to cancel an in-flight promise, so unsubscribing from an observable that's wrapping a promise has no effect on the underlying promise.

rxjs forkJoin does not call subscribe

I have a set of RxJs Observable that uses forkJoin. The issue I have is that the forkJoin makes the calls but subscribe does not get calls.
However, if I use zip it works.
I have created an example in StackBlitz. Is this a bug or am I doing it all wrong?
forkJoin only runs once all inner observables have completed. In your example, the inner observables never complete. zip runs once everytime all observables fire in order. If you want something to run anytime any observable fires, use combineLatest

forkjoin not wait for all observables complete when no value emit from each observable

I have several observables request.
Each observable request is recursive call and when there is no more data, it will call EMPTY.
for example:
request1 = recursive observable call
request2 = recursive observable call
request3 = recursive observable call
Then i use forJoin
forJoin(request, request2, request3).subscribe();
The forkJoin will callback with complete when anyone of them finish. It actually not wait for all observables complete.
I check the rxjs document, it mention
When all observables complete, emit the last emitted value from each.
As I don't care these observables return value, so it would not emit value to the forkJoin observer. Then i found that in this case if only one complete, forkJoin observer is notified with complete.
Is this the observable limitation that i do need to emit value to able to wait for all observables complete?
Thanks a lot.
It is by design.
Reference here:
forkJoin short circuits if one of the streams completes without emitting a value.
Since EMPTY just emits 'complete' and nothing else, forkJoin will complete right there.
Actually, the other non-empty observables inside forkJoin will still be subscribed, but since forkJoin may complete before them (depends on when did the EMPTY appear in forkJoin), you may not be able to handle them inside the forkJoin's complete function.

Alternatives to subscribe for cold observable

In combineLatest the method gets invoked only when we apply a ".subscribe()" to its end.
I wanted to know if there are other alternatives than adding subscribe to combineLatest method.

NGXS Async Action Subscription

The NGXS docs indicate that the framework automatically subscribes to Observables returned from within an async action (https://ngxs.gitbook.io/ngxs/concepts/state#async-actions). This is convenient, but does the framework also handle unsubscribing from the observable, or am I leaking subscriptions every time I call an async action that returns an Observable in this way?
You don't have to unsubscribe from the Observables.
It is handled for you.

Resources