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

Reading how an operator works

Throughout this book, you will see a lot of diagrams explaining how an operator works. These diagrams are a graphical representation of observables and operations in those observables. Usually, the diagrams consist of three parts. On the top they shows a line with balls representing the initial state of an observable. The line itself represents an observable. The circles are events that happened in this observable and they are pushed from left to right; therefore, the leftmost ball is the first event, the second one is the second, and so on. This is illustrated in the following diagram:

So, the preceding line represents an observable with three emitted events. The string at the center of each circle is the value emitted by that event. The short vertical line on the right-hand side of the diagram represents the end of this observable. This diagram is a graphical representation of the following observable:

Bacon 
.fromArray([
'First Event',
'Second Event',
'Third Event'
]);

As we know, an observable can exist forever (the ones created using the Bacon.interval() method). To represent this kind of observable, we remove the short line on the right-hand side. This way, we show that other events might happen in this observable in the future, but we are not interested in them, , as shown in the following diagram:

This diagram is a possible representation of the following observable:

Bacon 
.interval(1000);

Keep in mind, the time between events is irrelevant in this case, so we decided to omit it from the graphical representation (we usually omit this information, as this is an implementation detail). The Bacon.interval() method always emits an empty object, and for this reason, we draw brackets at the center of each circle.

The main reason for using this graphical representation is to show how an operator transforms an observable. So to show which operator is being used, we add an arrow pointing downward with the name of the operator and the content of the operator if needed. So if we want to represent the use of the map() operator, we can use the following diagram:

As discussed before, when an operator is applied to an observable, it creates a new observable (it does not change the original observable). For this reason, we add a new line that represents the new observable created after the operation. On this new line, I decided to omit the value at the center of the circle (adding only three dots), because the way the map() operator works is out of the scope of this discussion.

Sometimes we want to represent multiple operators. In such cases, we just keep chaining more lines. Some operators let you work with multiple observables, which makes the diagram more complex. But don't worry, we will explain the changes the first time they happen.
主站蜘蛛池模板: 沈丘县| 三门县| 湛江市| 武定县| 吴川市| 兴城市| 遵化市| 旬阳县| 南靖县| 中卫市| 辉县市| 布尔津县| 芦山县| 伽师县| 徐汇区| 华池县| 石门县| 定结县| 左云县| 平顶山市| 富蕴县| 枞阳县| 云阳县| 镇巴县| 西丰县| 商河县| 玛多县| 大化| 原阳县| 康定县| 高邮市| 屯昌县| 林甸县| 全州县| 夏河县| 化德县| 五莲县| 东平县| 天台县| 怀远县| 凤山市|