各种 ISubject 实现的作用是什么以及何时使用它们?
我非常清楚Subject类的作用以及何时使用它,但我刚刚浏览了msdn上的语言参考,发现还有各种其他ISubject实现,例如:
- AsyncSubject
- BehaviorSubject
- ReplaySubject
因为文档很漂亮地面上很薄,这些类型的意义是什么?在什么情况下您会使用它们?
I have a fairly good idea of what the Subject class does and when to use it, but I've just been looking through the language reference on msdn and see there are various other ISubject implementations such as:
- AsyncSubject
- BehaviorSubject
- ReplaySubject
As the documentation is pretty thin on the ground, whats the point of each of these types and under what situations would you use them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些主题都共享一个共同的属性 - 他们获取通过 OnNext 发布给他们的部分(或全部)内容,并将其记录下来并播放给您 - 即,他们获取一个 Hot Observable 并使其成为 Cold。这意味着,如果您多次订阅其中任何一个(即订阅 => 取消订阅 => 再次订阅),您将再次看到至少一个相同的值。
ReplaySubject:每次您订阅该主题时,您都会尽快获得已发布内容重播的完整历史记录(或子集,例如最近的n items)
AsyncSubject:始终回放最后发布并完成的项目,但仅在源完成之后。这个主题对于异步函数来说非常棒,因为您可以编写它们而不必担心竞争条件:即使有人在异步方法完成后订阅,他们也会得到结果。
BehaviorSubject:有点像 ReplaySubject,但缓冲区为 1,因此您始终可以获得最后发布的内容。您还可以提供初始值。始终在订阅上立即提供一件商品。
These subjects all share a common property - they take some (or all) of what gets posted to them via OnNext and record it and play it back to you - i.e. they take a Hot Observable and make it Cold. This means, that if you Subscribe to any of these more than once (i.e. Subscribe => Unsubscribe => Subscribe again), you'll see at least one of the same value again.
ReplaySubject: Every time you subscribe to the Subject, you get the entire history of what has been posted replayed back to you, as fast as possible (or a subset, like the last n items)
AsyncSubject: Always plays back the last item posted and completes, but only after the source has completed. This Subject is awesome for async functions, since you can write them without worrying about race conditions: even if someone Subscribes after the async method completes, they get the result.
BehaviorSubject: Kind of like ReplaySubject but with a buffer of one, so you always get the last thing that was posted. You also can provide an initial value. Always provides one item instantly on Subscribe.
根据最新版本( v1.0.2856.0)并为了使这个问题保持最新状态,出现了一组新的主题类:
FastSubject
、FastBehaviorSubject
、FastAsyncSubject
和FastReplaySubject
根据 发行说明 他们
In light of the latest version (v1.0.2856.0) and to keep this question up to date, there has been a new set of subject classes:
FastSubject
,FastBehaviorSubject
,FastAsyncSubject
andFastReplaySubject
As per the release notes they
关于 AsyncSubject
此代码:
打印单个值 3。如果订阅在完成后移至,则打印相同的值。所以它回放的不是第一个,而是最后一个,完成后播放(直到完成,它不会产生值),并且它不像完成前的Subject那样工作。
有关详细信息,请参阅此 Prune 讨论(AsyncSubject 与 Prune 基本相同)
In regards to AsyncSubject
This code:
prints a single value 3. And it prints same if subscription is moved to after completion. So it plays back not the first, but the last item, plays it after completion (until complete, it does not produce values), and it does not work like Subject before completion.
See this Prune discussion for more info (AsyncSubject is basically the same as Prune)
保罗的回答几乎说明了这一点。不过,有一些事情值得补充:
AsyncSubject 正如 Paul 所说,但只有在 source 完成之后才能工作。在此之前,它的工作方式类似于Subject
(订阅者接收“实时”值)AsyncSubject 自从我上次对其进行测试以来已经发生了变化。它在完成之前不再充当活动主题,而是在发出值之前等待完成。而且,正如 Sergey 提到的,它返回最后一个值,而不是第一个值(尽管我应该注意到这一点,因为情况一直如此)
AsyncSubject 由
Prune
使用,< code>FromAsyncPattern、ToAsync
以及其他一些的
Publish
重载使用BehaviorSubject 由接受初始值 ReplaySubject 由
Replay
使用注意:上面的所有操作符引用均指操作符的发布集,因为它们在修订版 2838 中被通用发布操作符取代(Christmas '10) )正如前面提到的,原来的运算符将被重新添加
Paul's answer pretty much nails it. There's a few things worth adding, though:
AsyncSubject works as Paul says, but only after the source completes. Before that, it works likeSubject
(where "live" values are received by subscribers)AsyncSubject has changed since I last ran tests against it. It no longer acts as a live subject before completion, but waits for completion before it emits a value. And, as Sergey mentions, it returns the last value, not the first (though I should have caught that as that's always been the case)
AsyncSubject is used by
Prune
,FromAsyncPattern
,ToAsync
and probably a few othersBehaviorSubject is used by overloads of
Publish
that accept an initial valueReplaySubject is used by
Replay
NOTE: All operator references above refer to the publishing set of operators as they were before they were replaced with generalised publish operators in rev 2838 (Christmas '10) as it has been mentioned that the original operators will be re-added