Reputation: 2264
I was looking through some code and found this:
this.busy = this.service.getInfo().subscribe((info: InfoData[]) => {
this.setInfo(info);
});
Now busy
is a component's property of type boolean
which is used as input value for [ngBusy]
directive. Obviously, subscribe()
returns a Subscription
, which is not null and therefore is cast to true
. This triggers directive to show modal loading popup, which goes away once info is resolved. That means, that this.busy
is false by that moment, which I have to mean that Subscription
object exists no more.
Inside getInfo()
method there is only one line, which is return Observable.of(info)
, where info is stub variable.
So my question is:
Is Subscription
deleted automatically right after data is resolved? If yes, is it only for Observable.of()
or are there any other examples of such behavior?
Upvotes: 1
Views: 593
Reputation: 16892
Is Subscription deleted automatically right after data is resolved?
No, it is not. - The subscription-object will still exist, which is due to the fact, that in JavaScript an object cannot "remove itself". You can also check the code below, to see that there is still a subscription-object. It is not attached to the stream any more but it still exists.
I'm assuming, that you are using angular2-busy - if that is the case, then you should take a closer look at the documentation - [ngBusy]
is capable of evaluating a subscription-object and will stop showing the busy-indication when that subscription-object is closed.
In the code-sample below you can see that by the end of the subscription, it will still exist, but in a non-operational/closed state - angular2-busy is able to detect this.
const of$ = Rx.Observable.of("Test");
let sub = of$.subscribe();
// the subscription will be automatically closed, but not be "undefined" or "falsly"
console.log(sub);
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
Upvotes: 4