官术网_书友最值得收藏!

Hot and cold observables

The preceding sections covered most of the factory operators available in RxPY. All of these operators start to emit items at subscription time. This is the default behavior in ReactiveX. Nothing happens until an observer subscribes to an observable. It is the subscription that makes the observable start emitting its items. This behavior is very important to understand when coding with ReactiveX, and it has a name: cold observables. Another behavior of observables is possible: hot observables.

So, what is the difference between cold and hot observables?

Cold and hot observables can be described as similar to the different ways of watching TV shows. Sometimes people watch TV shows on demand, and sometimes people watch TV shows live.

When somebody watches a TV show on demand, the content is sent to his TV only when he effectively asks to view the show (that is, when he presses the play button). If another person, or one thousand other people, want to view the same show, each of them will also receive this content; exactly when they request it, they will all receive their own video stream. This allows each person to pause the playback, go forward, or resume the playback without any impact on the other people viewing. This is the behavior of a cold observable. A cold observable starts to emit items at subscription time, and each time a new subscription occurs, a new sequence of items is started.

The following figure shows this behavior; each observer receives its own sequence, starting at subscription time:

Figure 4.12: A cold observable

On the other hand, when somebody watches a live show on TV then he does not choose when the event will start. The viewer has to be in front of his TV when the show starts; otherwise, he will miss the beginning of it. Also, if thousands of people watch the same live show, they all see the same content. At the very same time, they all receive the same video stream. This is great for events that people want to watch as they happen in real time, such as sports events, music shows, and so on. This is the behavior of a hot observable. A hot observable emits items whenever their triggering event happens. These items are emitted whatever the number of subscribers to it.

If an observer does not register before the observable starts to emit items then they will miss some items. The following figure shows this behavior; all observers receive the same items at the same time when they are emitted by the observable:

Figure 4.13: A hot observable

Both behaviors are necessary, and the best pattern must be used for each use case. Most of the time, cold observables correspond to the required behavior. That is probably why they are the default type of observable in ReactiveX. If a hot observable is needed, there are two ways to implement it.

The first way is with the create operator. Since it can be used to implement any logic, one can expose a hot observable with the create operator. For example, with the create operator, it is possible to create an observable which emits an item each time lightning hits the ground somewhere on earth. This is a typical hot behavior; observers need to know when lightning occurs, but there is no way for them to force lightning to happen.

The second one is with dedicated operators that can convert a cold observable to a hot observable. Let's look at how to use them in the next section.

主站蜘蛛池模板: 横山县| 德清县| 苍梧县| 厦门市| 潞西市| 新安县| 巩义市| 册亨县| 清远市| 扎兰屯市| 得荣县| 榆中县| 阆中市| 南平市| 宝清县| 平原县| 永州市| 商城县| 阳朔县| 鹤岗市| 乐安县| 贡嘎县| 清水河县| 西林县| 铜梁县| 无为县| 迁西县| 偏关县| 宁安市| 河间市| 罗定市| 曲靖市| 岳阳县| 广东省| 西安市| 惠安县| 平武县| 清河县| 徐水县| 四平市| 新营市|