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

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")
)
主站蜘蛛池模板: 蕲春县| 汉源县| 高邮市| 永平县| 高尔夫| 民勤县| 恩施市| 滁州市| 巴彦淖尔市| 汉沽区| 宜阳县| 交城县| 咸丰县| 伊宁市| 泰宁县| 金沙县| 永泰县| 新绛县| 高安市| 甘泉县| 霍林郭勒市| 加查县| 龙海市| 龙川县| 阿瓦提县| 滁州市| 精河县| 邯郸县| 南汇区| 高青县| 慈利县| 乐至县| 保靖县| 阳山县| 济宁市| 洪江市| 东平县| 浦东新区| 新安县| 定远县| 密云县|