I have read about the concept of using takeUntil
operator to manage unsubscribe
in ngDestroy
.
I would like to go a bit further and understand step by step the operator. It is my first step inside rxjs before going further (others operators).
To start I read line by line the source code and to help me this one too Another SO question
- I don't see any unsubscribe, except inside the
createOperatorSubscriber
= the notifier Observable - I don't see the link between when the notifier complete and the source Observable.
- is it the subscriber.complete which triggers the unsubscribe of the source Observable ? (the other SO question talk about that but I'm missing a piece)
- the source Observable is lifted (if it's a Subject we create an AnonymousSubject) but I can't see its role.
Is someone can go deep and explains to me via a code review the inner mechanisms of takeUntil
?
Update: Thanks @drenai. I split the post in two.
Below, I added more explanation of my journey. Welcome to my brain :) I got a simple Angular usecase :
- I have a service containing a BehaviorSubject monitoring a combobox and emiting the new value
- inside a component
ngOnInit
hook I subscribe to the previous subject - and when I receive a new value I fetch some data over http.
- my first try was using a
switchMap
to pipe the operations - and then I was wondering about the unsubscription
- if I store the resulting `Subscription' object is it enough to avoid a memory leak (does it unsubscrive from the first Observable)
- if I just don't do anything, is it the http call that manage the subscription ?
- do I need to add takeUntil
- this was the moment where my need to understand the implementation scratches me
- I'm hoping that understanding the "takeUntil" operator will permit me to have a better view of the others pipeable operators.
Bonus :
- if two components on the same page have subscribed to the BehaviorSubject. Is the first
takeUntil
destroy operation unsubscribe all subscribers ? I don't really imagine that but asking that is another way to see that I'm missing another piece :)