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
createOperatorSubscriber
= the notifier Observable 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 :
ngOnInit
hook I subscribe to the previous subjectswitchMap
to pipe the operationsBonus :
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 :)is it the subscriber.complete which triggers the unsubscribe of the source Observable ?
Yes. The subscriber will be automatically unsubscribed when its complete is triggered (also see _complete
in that link)
if I just don't do anything, is it the http call that manages the subscription ?
The HttpClient
call will eventually complete and the subscriber will be unsubscribed eg after a 500ms API call. But.... if you be navigate away from the page while the API call is in-flight you probably want to cancel the HttpClient call and unsubscribe - that's where the takeUntil
would come in
Is the first takeUntil destroy operation unsubscribing all subscribers ?
Nope. Each subscription's takeUntil
would just manage that particular subscription to the BehaviorSubject
.
Note: this unsubscribe/cancel feature is not something one would be familiar with if coming from Promise notification system, as there's no native way to cancel a Promise
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.