rxjs takeuntil tepth

发布于 2025-01-30 13:38:43 字数 1415 浏览 2 评论 0原文

我已经阅读了有关使用takeuntil运算符在ngdestroy中管理dismubscribe的概念。

我想走得更远,一步一步地理解操作员。这是我在进一步(其他操作员)之前的第一步。

首先,我读行 a> 并为我提供帮助,也又一个问题

  • 我不't请参阅任何退订,除了create> createPoeratorSubScriber = =可观察的通知器,
  • 我看不到通知器完成和可观察到的源时之间的链接。
  • 是subscriber.com platete触发可观察到的来源的取消订阅吗? (另一个这样谈论的问题,但我错过了一件)
  • 可以观察到的来源(如果是主题,我们会创建一个匿名者),但我看不到它的作用。

是否有人可以深入并通过代码回顾takeuntil的内部机制向我解释?

更新:谢谢@drenai。我将帖子分为两分。

在下面,我添加了更多关于旅程的解释。欢迎来到我的大脑:) 我有一个简单的角度用户:

  • 我有一项包含行为的服务,可以监视组合并在组件内发出新值
  • ngoninit钩子i i订阅上一个主题
  • ,当我收到一个新值时,我会取得一些信息。通过HTTP的数据。
  • 我的第一次尝试是使用switchmap对操作进行
  • 管道
    1. 如果我存储了结果的“订阅”对象,足以避免内存泄漏(是否可以从第一个可观察的可观察到)
    2. 如果我什么都不做,是管理订阅的HTTP调用吗?
    3. 我需要添加takeuntil
  • 这是我需要了解实施的时刻,
  • 我希望理解“ takeuntil”操作员可以使我可以更好地了解其他可管道的操作员。

奖励:

  • 如果同一页面上的两个组件已订阅了行为主题。第一个takeuntil销毁操作退订所有订户吗?我真的没有想到,但是问那是另一种看法我错过了另一件作品的方式:)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慕巷 2025-02-06 13:38:43

是订户的splete,它触发了可观察到的源的取消订阅?

是的。当订阅者 >是触发的(另请参见该链接中的_complete

如果我什么都不做,是管理订阅的HTTP调用吗?

HTTPCLIENT呼叫最终将完成,并在500ms API调用后取消订阅,例如。但是....如果您在API呼叫是在线飞行时从页面上导航,则可能要取消httpclient呼叫和退订 - 这就是takeuntil将进入的地方

是第一个takeuntil销毁操作,请订阅所有订户?

没有。每个订阅的takeuntil只会将特定订阅管理到caping> cistionubject

注意:此取消订阅/取消功能如果来自Promise Notification System,这是不熟悉的,因为没有本地方法可以取消承诺

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 e.g. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文