rxjs takeuntil tepth
我已经阅读了有关使用takeuntil
运算符在ngdestroy
中管理dismubscribe
的概念。
我想走得更远,一步一步地理解操作员。这是我在进一步(其他操作员)之前的第一步。
首先,我读行 a> 并为我提供帮助,也又一个问题
- 我不't请参阅任何退订,除了
create> createPoeratorSubScriber
= =可观察的通知器, - 我看不到通知器完成和可观察到的源时之间的链接。
- 是subscriber.com platete触发可观察到的来源的取消订阅吗? (另一个这样谈论的问题,但我错过了一件)
- 可以观察到的来源(如果是主题,我们会创建一个匿名者),但我看不到它的作用。
是否有人可以深入并通过代码回顾takeuntil
的内部机制向我解释?
更新:谢谢@drenai。我将帖子分为两分。
在下面,我添加了更多关于旅程的解释。欢迎来到我的大脑:) 我有一个简单的角度用户:
- 我有一项包含行为的服务,可以监视组合并在组件内发出新值
ngoninit
钩子i i订阅上一个主题- ,当我收到一个新值时,我会取得一些信息。通过HTTP的数据。
- 我的第一次尝试是使用
switchmap
对操作进行 - 管道
- 如果我存储了结果的“订阅”对象,足以避免内存泄漏(是否可以从第一个可观察的可观察到) )
- 如果我什么都不做,是管理订阅的HTTP调用吗?
- 我需要添加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
- 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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。当订阅者 >是触发的(另请参见该链接中的
_complete
)HTTPCLIENT
呼叫最终将完成,并在500ms API调用后取消订阅,例如。但是....如果您在API呼叫是在线飞行时从页面上导航,则可能要取消httpclient呼叫和退订 - 这就是takeuntil
将进入的地方没有。每个订阅的
takeuntil
只会将特定订阅管理到caping> cistionubject
。注意:此取消订阅/取消功能如果来自Promise Notification System,这是不熟悉的,因为没有本地方法可以取消承诺
Yes. The subscriber will be automatically unsubscribed when its complete is triggered (also see
_complete
in that link)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 thetakeUntil
would come inNope. Each subscription's
takeUntil
would just manage that particular subscription to theBehaviorSubject
.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