7

I'm having trouble finding what happens to an Observable when it has completed. I have an observable that must complete before I do other work. To do this, my other functions subscribe that observable, and when it's complete, they do their work.

The question I have is what happens when they subscribe after the Observable is complete? From my testing, it appears they get the last value and then it completes (It fires value and complete callbacks on the subscribe method). I'd rather just not subscribe to it to begin with if it has already completed.

Is there a way to check if an Observable has completed without subscribing? I was setting it undefined when complete, but it creates an issue if I'm subscribed to observable from another function when that occurs.

I could create a behavior subject isLoaded which starts off as False, and goes to true when it's done, but I'm having trouble finding documentation on what a completed observable does when subscribed to and what the best practice is. Thank you!

Edit: This is not asking how to check if an observable is complete. It's asking what happens if an observable completed from a different subscription, how you can check that it had completed. What happens to an observable when it is complete and you look at the Observable object.

Diesel
  • 5,099
  • 7
  • 43
  • 81
  • duplicate? https://stackoverflow.com/questions/45032251/angular2-how-can-i-check-if-an-observable-is-completed – ABOS Jan 03 '19 at 16:56
  • Not a duplicate, completely different question. I'm not asking how to fire the complete method. I'm asking about documentation and what happens when you subscribe after an observable has already completed from a different subscription. – Diesel Jan 03 '19 at 17:17
  • it depends on what kind of observable it is, right? say, behavior subject or async subject? – ABOS Jan 03 '19 at 17:27
  • Is there documentation on what happens if you subscribe to an Observable (type Observable, not Behavior Subject) that has already completed. This one: `class Observable implements Subscribable { static create: Function static if: typeof iif static throw: typeof throwError constructor(subscribe?: (this: Observable, subscriber: Subscriber) => TeardownLogic)` – Diesel Jan 03 '19 at 19:04

2 Answers2

3

By default, observable behaves unicast. Unicast means it is 1:1 relation to subscription, as same as a function. Think most simple function you can think like below:

function boo() { //do something }
boo();

You need to call function to trigger something. Same applies to observable -

const someObservable = of(1,2,3);

someObservable.subscribe(...);

someObservable does nothing until you subscribe it. So there is no meaning to check if it's completed or not, as you didn't subscribe to anything. Also there is no meaning to check other subscription's completed or not, as it's 1:1 relation to subscription. You can check subscription's state, but that's different to completion state.

Now there's other type of observable, multicast - which is typically represented in implementation of Subject. That does emitting regardless of subscription exists, and you could check if it's closed to find source itself is closed already. Depends on nature of subject you may observe different behavior, typically you'd get nothing when try to subscribe into completed on for multicasting observables.

OJ Kwon
  • 4,385
  • 1
  • 20
  • 24
3

If you do not use endWith operator, it will simply not emit any value anymore, however it triggers complete callback if it's provided. Have not tested it with finalize operator but I suppose that it is triggered as well, because observable goes through the same lifecycle each time you subscribe to it.

In case if endWith operator is used, then it's value will be emitted on each subsequent subscription once it is completed.

Using RxJS v.7.1.0

Vlad
  • 985
  • 1
  • 6
  • 10