简体   繁体   English

用作PartialObserver的BehaviorSubject会中断BehaviorSubject上的订阅

[英]BehaviorSubject used as PartialObserver breaks the subscription on BehaviorSubject

I was looking through the interfaces of observables and saw you can pass anything that implements PartialObserver to the subscribe function. 我正在查看observables的接口,看到你可以将实现PartialObserver的任何东西传递给subscribe函数。 So I did that with BehaviorSubject. 所以我用BehaviorSubject做到了。

Like this (A) 像这样(A)

source$
      .pipe(
        tap(() => console.log('X')),
      )
      .subscribe(this._titlesX$);

So I did, but found something very strange. 所以我做了,但发现了一些非常奇怪的东西。 If I pass the behavior subject to the subscribe function the values get emitted, but to see that, you have to subscribe before using the BehaviorSubject as PartialObserver. 如果我将行为主题传递给subscribe函数,则会发出值,但要看到这一点,您必须在使用BehaviorSubject作为PartialObserver之前进行订阅。

In the docs for BehaviorSubject you can find this: 在BehaviorSubject的文档中,您可以找到:

A variant of Subject that requires an initial value and emits its current value whenever it is subscribed to.

So when using BehaviorSubject as partialObserver this behavior breaks.(No pun intended) 因此,当使用BehaviorSubject作为partialObserver时,此行为会中断。(无双关语)

But when I use this method: Like this (B) 但是当我使用这种方法时:像这样(B)

source$
     .pipe(
        tap(() => console.log('X')),
      )
      .subscribe(res => this._titlesX$(res));

Everything works as expected. 一切都按预期工作。

Why does method A not work? 方法A为什么不起作用? Is this a bug or am I just using it wrong? 这是一个错误还是我只是错误使用它?

Here is a link to the stackblitz all setup. 这是stackblitz所有设置的链接。 Link to stackblitz project 链接到stackblitz项目

titleX: represents method A titleY: represents method B titleX:表示方法A titleY:表示方法B.

Subscribing with an BehaviorSubject should be possible. 应该可以订阅BehaviorSubject。 And the time when you subscribed should not matter. 订阅的时间应该不重要。 Otherwise it shouldn't be allowed as a parameter for the subscribe method. 否则,不应将其作为subscribe方法的参数。

the reason is because when you do 原因是因为当你这样做

source$.subscribe(this._titlesX$)

you've fully casted the source to the BehaviorSubject, as BehaviorSubject is a full observer, including the complete and error handlers. 您已将源完全转换为BehaviorSubject,因为BehaviorSubject是一个完整的观察者,包括完整和错误处理程序。 of completes after one emission, so it also completes your BehaviorSubject, and completed subjects do not emit. of一个发射之后完成,所以它也完成了你的BehaviorSubject,并完成主体不发射。 but just calling next in the subscribe of source$ clearly doesn't pass through the complete / error handlers. 但只是在source $的订阅中调用next,显然不会通过完整/错误处理程序。

if your source was more like: 如果您的来源更像是:

const source$ = interval(1000).pipe(map(v => [v.toString()]));

where it does not complete, you'll see expected behavior. 如果它没有完成,你会看到预期的行为。

blitz: https://stackblitz.com/edit/angular-gfmdgf?file=src/app/app.component.ts 闪电战: https ://stackblitz.com/edit/angular-gfmdgf file = src / app / app.component.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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