Reputation: 253
If I return a BehaviorSubject as an Observable from a service and subscribe to that Observable in a component and then call either take(1) or unsubscribe, does the BehaviorSubject keep emitting values? Is it effected?
Edit Thank you everyone for the responses. That clears things up for me.
Upvotes: 3
Views: 330
Reputation: 51
In this scenarios there are two different roles: The Observable and the Observer (the one who subscribes to the Observable).
Actions taken by the Observer
don't affect the behaviour of the Observable, therefore if an Observer unsubscribe to an Observable it does not affect the Observable itself. Other Observers to that Observable continue to be subscribed and the Observable continues to emit values.
Upvotes: 0
Reputation: 14149
Observables (which are actually just the factories for observable streams) are in general not affected by their subscribers. However, they CAN be implemented in a way that unsubscribing to them affects other subscribers. This is not the case for BehaviorSubject
though.
Generally, you wouldn't want to alter other streams when a subscriber unsubscribes. This would go against the elasticity and reciliency goals of reactive programming (shared state potentially creates bottlenecks and leads to errors being propagated to other streams)
So yes, BehaviorSubject
will keep emitting to other subscribers as long as it's not completed. It won't, however, emit to the take(1)
subscriber at any time after it has sent the first notification.
Upvotes: 3