一个 Rx Observable 有多少 "temperatures"?

How many "temperatures" are there for a Rx Observable?

在所有 Rx.Net 文献中,都提到了通常被称为可观测值的 温度 的东西。

cold observables(就像 Observable.Interval() 和类似工厂方法创建的那些),每次创建新订阅时都会产生副作用。

在频谱的另一端,有 hot observables(如 Subject<T>),它们会在新订阅到来时加入。

还有 warm observables,就像 RefCount() 返回的那些,它会在每次创建一个订阅时执行初始化,但前提是没有其他订阅主动订阅。 Dave Sexton here 解释了这些暖观测值的行为:

Alternatively, you can call Publish then RefCount to get an IObservable that is shared among multiple consecutive observers. Note that this isn't truly a hot observable - it's more like a warm observable. RefCount makes a single subscription to the underlying observable while there's at least one observer of your query. When your query has no more observers, changing the reference count to 0, the underlying subscription is disposed. If another observer subscribes to your query later, moving the reference count from 0 to 1 again, then RefCount makes a new subscription to the underlying observable, causing subscription side-effects to occur again.

还有其他需要注意的温度吗?是否可以通过编程方式获取 Observable 的温度?

先问简单问题:

Is it possible to obtain programmatically the temperature of an Observable?

没有。您能做的最好的事情就是订阅并看看会发生什么。

observable 'contract' 指定当您订阅一个 observable 时,您会收到零个或多个 OnNext 消息,可以选择后跟一个 OnCompleted 或一个 OnError 消息。合同没有具体说明如何处理多个或 earlier/later 订阅者,这是 observable 'temperature' 最关心的。

Are there any other temperatures that one should be aware of?

我什至不会像您指定的那样具体或离散地考虑它。

我从订阅效果的角度来考虑它:最冷的可观察对象的所有效果都发生在订阅上(比如 Observable.Return(42))。最热门的 observables 对订阅没有影响 (new Subject<int>())。在这两个极点之间是一个连续体。

例如,

Observable.Interval(TimeSpan.FromMilliseconds(100)) 将每 100 毫秒发出一个新数字。该示例与 Observable.Return(42) 不同,可能主要是 'warmed-over' 通过 .Publish().RefCount():第一个订阅者开始数字,但第二个订阅者将只看到最新的数字,而不是从 0 开始。但是,如果您使用的不是 .Publish(),而是 .Replay(2).RefCount(),那么您会产生一些订阅效果。 PublishReplay 可观察量是否具有相同的 'temperature'?

TL;DR:不要太关注分类。了解两者之间的区别,知道一些可观察物具有较冷的属性,而一些具有较暖的属性。