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

The merge operator

The merge operator combines several observables into a single observable by merging all items emitted by the source observables in its output observable. The merge operator accepts several observables as input and returns one output observable:

Figure 3.7: The merge operator

The items emitted by each source observable are interleaved on the output observable. As shown in the Figure 3.7, the output observable completes once all input observables have completed. However, if a source observable completes on error then the output observable immediately completes on error:

Figure 3.8: The merge operator error behavior

The prototype of the merge operator is the following one:

Observable.merge(self, *args)
Observable.merge(*args)

This method can be used as a method of an Observable object, or as a static method of the Observable class. The arguments passed as input can be several observables to merge, or a list containing the observables to merge:

numbers = Observable.from_([1, 2, 3, 4])
strings = Observable.from_(["one", "two", "three", "four"])

print("Merge from object method:")
numbers.merge(strings) \
.subscribe(
on_next=lambda i: print("item: {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)

print("Merge from class static method:")
Observable.merge([numbers, strings]) \
.subscribe(
on_next=lambda i: print("item: {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)
主站蜘蛛池模板: 油尖旺区| 郁南县| 邵东县| 广河县| 贵州省| 巴楚县| 安陆市| 化隆| 南靖县| 浦城县| 海原县| 缙云县| 天镇县| 张家口市| 公安县| 全州县| 水富县| 伊宁县| 阿克| 社会| 平陆县| 鱼台县| 仁寿县| 东兴市| 新安县| 潜江市| 乐平市| 博乐市| 洛川县| 华池县| 云阳县| 象州县| 东台市| 泊头市| 延安市| 唐河县| 婺源县| 西吉县| 耒阳市| 兴隆县| 博白县|