简体   繁体   中英

Rxjs takeUntil in-depth

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
    1. if I store the resulting `Subscription' object is it enough to avoid a memory leak (does it unsubscrive from the first Observable)
    2. if I just don't do anything, is it the http call that manage the subscription ?
    3. 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 :)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM