各种 ISubject 实现的作用是什么以及何时使用它们?

发布于 2024-10-13 09:46:19 字数 202 浏览 3 评论 0原文

我非常清楚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 技术交流群。

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

发布评论

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

评论(4

孤者何惧 2024-10-20 09:46:19

这些主题都共享一个共同的属性 - 他们获取通过 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.

坠似风落 2024-10-20 09:46:19

根据最新版本( v1.0.2856.0)并为了使这个问题保持最新状态,出现了一组新的主题类:

FastSubjectFastBehaviorSubjectFastAsyncSubjectFastReplaySubject

根据 发行说明 他们

比普通科目快得多
但是:

  • 不要通过 IScheduler 将生产者和消费者解耦
    (有效地将它们限制为
    立即调度程序);
  • 不防止堆栈溢出;
  • 不同步输入消息。

快速主题由 Publish 和
如果没有调度程序,则修剪运算符
指定。

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 and FastReplaySubject

As per the release notes they

are much faster than regular subjects
but:

  • don’t decouple producer and consumer by an IScheduler
    (effectively limiting them to
    ImmediateScheduler);
  • don’t protect against stack overflow;
  • don’t synchronize input messages.

Fast subjects are used by Publish and
Prune operators if no scheduler is
specified.

蝶…霜飞 2024-10-20 09:46:19

关于 AsyncSubject

此代码:

        var s = new AsyncSubject<int>();
        s.OnNext(1);
        s.Subscribe(Console.WriteLine);
        s.OnNext(2);
        s.OnNext(3);
        s.OnCompleted();

打印单个值 3。如果订阅在完成后移至,则打印相同的值。所以它回放的不是第一个,而是最后一个,完成后播放(直到完成,它不会产生值),并且它不像完成前的Subject那样工作。
有关详细信息,请参阅此 Prune 讨论(AsyncSubject 与 Prune 基本相同)

In regards to AsyncSubject

This code:

        var s = new AsyncSubject<int>();
        s.OnNext(1);
        s.Subscribe(Console.WriteLine);
        s.OnNext(2);
        s.OnNext(3);
        s.OnCompleted();

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)

近箐 2024-10-20 09:46:19

保罗的回答几乎说明了这一点。不过,有一些事情值得补充:

AsyncSubject 正如 Paul 所说,但只有在 source 完成之后才能工作。在此之前,它的工作方式类似于 Subject (订阅者接收“实时”值)

AsyncSubject 自从我上次对其进行测试以来已经发生了变化。它在完成之前不再充当活动主题,而是在发出值之前等待完成。而且,正如 Sergey 提到的,它返回最后一个值,而不是第一个值(尽管我应该注意到这一点,因为情况一直如此)

AsyncSubjectPrune 使用,< code>FromAsyncPattern、ToAsync 以及其他一些

Publish 重载使用

BehaviorSubject 由接受初始值 ReplaySubjectReplay 使用

注意:上面的所有操作符引用均指操作符的发布集,因为它们在修订版 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 like Subject (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 others

BehaviorSubject is used by overloads of Publish that accept an initial value

ReplaySubject 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

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