24

The class Subject has 2 properties closed and isStopped. I know that closed can be used to check whether the Subject can still be subscribed to, but what should isStopped be used for exactly?

I am asking this because I am trying to find a way to know when a next operation of a BehaviourSubject is completed. Can I use isStopped for that or is it used for something else?

Wilt
  • 41,477
  • 12
  • 152
  • 203
Maurice
  • 6,698
  • 9
  • 47
  • 104
  • You can check the source code https://github.com/ReactiveX/rxjs/blob/master/src/internal/Subject.ts – Igor Sep 28 '18 at 19:44
  • i did already so. I can see that isStopped is set to true together with closed, but what exactly is the unique purpose of isStopped as opposed to closed??? – Maurice Sep 28 '18 at 19:52
  • 2
    `unsubscribe` sets both to `true` but `isStopped` is also set to `true` on `error` and `complete`. One also throws an exception, the other does not, if being called. There are differences between the 2. Which one you check depends on what you end up calling when you want to stop using the `Subject` instance. – Igor Sep 28 '18 at 19:54

1 Answers1

40

The compared behavior of closed and isStopped can be seen in terms of resultant values after each operation:

  • On error:
    • closed: false
    • isStopped: true.
  • If Subject gets completed:
    • closed: false
    • isStopped: true.
  • If unsubscribed:
    • closed: true
    • isStopped: true

This is non-exhaustive just show the commons scenarios.

nilsandrey
  • 1,030
  • 11
  • 28