gpt4 book ai didi

system.reactive - 什么是热观测值和冷观测值?

转载 作者:行者123 更新时间:2023-12-03 06:40:01 24 4
gpt4 key购买 nike

I watched the video我知道一般原则 - 即使没有人订阅,热也会发生,冷会“按需”发生。另外,Publish() 将冷数据转换为热数据,Defer() 将热数据转换为冷数据。

但是,我仍然觉得我错过了细节。以下是我想回答的一些问题:

  • 您能为这些术语给出一个全面的定义吗?
  • 对热 observable 调用 Publish 或对冷 observable 调用 Defer 是否有意义?
  • 热/冷转换有哪些方面 - 例如,您是否会丢失消息?
  • IObservable 和 IEnumerable 的热定义和冷定义之间有区别吗?
  • 针对冷或热编程时应考虑哪些一般原则?
  • 关于热/冷可观察量还有其他提示吗?

最佳答案

来自:Anton Moiseev 的书 “Angular Development with Typescript, Second Edition.” :

Hot and cold observables

There are two types of observables: hot and cold. The maindifference is that a cold observable creates a dataproducer for each subscriber, whereas a hot observablecreates a data producer first, and each subscriber gets thedata from one producer, starting from the moment of subscription.

Let’s compare watching a movie on Netflix to going into amovie theater. Think of yourself as an observer. Anyone who decides to watch Mission: Impossible on Netflix will get the entiremovie, regardless of when they hit the play button. Netflix creates anew producer to stream a movie just for you. This is a coldobservable.

If you go to a movie theater and the showtime is 4 p.m., the produceris created at 4 p.m., and the streaming begins. If some people(subscribers) are late to the show, they miss the beginning of themovie and can only watch it starting from the moment of arrival. Thisis a hot observable.

A cold observable starts producing data when some code invokes asubscribe() function on it. For example, your app may declare an observable providing a URL on the server to get certain products. Therequest will be made only when you subscribe to it. If another scriptmakes the same request to the server, it’ll get the same set of data.

A hot observable produces data even if no subscribers areinterested in the data. For example, an accelerometer in yoursmartphone produces data about the position of your device, even if noapp subscribes to this data. A server can produce the latest stockprices even if no user is interested in this stock.

关于system.reactive - 什么是热观测值和冷观测值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2521277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com