NGXS Async Action Subscription - ngxs

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.

Related

Does RxJS `share` operator really makes an Observable hot?

The documentation of a share operator states that:
As long as there is at least one Subscriber this Observable will be subscribed and emitting data. ... Because the Observable is multicasting it makes the stream hot
I thought that hot observable is the one that starts emitting data without a need to be subscribed to?
Am I wrong?
You've misunderstood what hot and cold observables are.
Cold Observable
In RxJS an observable is cold (also known as unicasted), when there is one source per subscriber. When subscribing to a cold source, the pipeline gets recreated for each subscriber. Note the following: if your pipeline is computation heavy, then you'd be doing that computation everytime a new subscriber subscribes to that source.
Hot Observable
In RxJS an observable is hot (also known as multicasting), when there is one source for all subscribers. When subscribing to a hot source, the pipeline doesn't get recreated for each subscriber. Hot sources are useful for when you don't want to compute the pipeline for every subscriber. The pipeline will be computed once, and then the answer will be sent to all subscribers.
StackBlitz Example showing this in code.
Yes, hot observables emit data even when no subscriber is active just like a websocket or mouse events. They are independent of subscribers.
Cold observables emit data only when subscribed to it, e.g. without subscribing to an obervable wrapping a http request, your http request will never happen.
Using share, your cold observable gets hot, as no new http request will happen but the old response will be remitted.

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.

Do I have to unsubscribe from completed observable in 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.

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.

From IObservable<T> to Task

So the case is this. Suppose somewhere I am filling a Collection. Each time an element is added, an IObservable calls OnNext for its subscribers.
Now, there will be a point where the collection will be filled. (I was reading something and I finished reading .. whatever). At that point, OnComplete() is called on the subscribers.
The user, however, won't observe this method. He will rather call an async method that he will await for ... he doesn't care much about the things he read, he just cares that he finished reading.
So basically, I want, from an IObservable, a Task that returns when the IObservable calls OnComplete() to its subscribers. I specifically want the user not to use an observer, but just to be happy with knowing that whatever happens after his await call will happen after the collection is filled.
Maybe the ToTask() method does the trick? I can't really tell by the documentation.
If you are using Rx 2.0 or later you can await IObservable which returns the last item in the observable. I.e. after the observable has completed.
var lastItem = await myObservable;
This is possible because in Rx 2.0 a GetAwaiter extension method on IObservable was added making it possible to await observables. There are also some handy extension methods that allow you to specify which element you want to await.
There is a nice blog about it here.

Resources