Rx.Subject loses events

旧巷老猫 提交于 2019-11-28 02:08:50

This all is in fact expected behavior.

The confusing thing is what happens when you reuse Subjectand an operator such as take() multiple times.

Operator take(1) takes just a single value and send complete notification. This notification is received by the Subject because of .subscribe(bs2). Now comes the most important part.
When a Subject receives a complete or error notification it marks itself as stopped. This means it will never remit any items or notifications which is correct and expected behavior in Rx. Notifications complete or error have to be the last emissions.

So the Subject is completed by the first take(1) which is triggered by value 0 (the bs2.next(0) call).

Then when value 2 triggers the second run of the Observable.interval(0).take(1) it's received by the Subject but it's automatically ignored because the Subject is already marked as stopped.

The process in you third demo is exactly the same.

You can see it in the source code in Subject.ts:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!