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

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")
)
主站蜘蛛池模板: 靖远县| 内乡县| 金昌市| 凌源市| 虹口区| 临泽县| 类乌齐县| 犍为县| 济源市| 九龙坡区| 措美县| 文登市| 锡林浩特市| 晋宁县| 富源县| 岱山县| 建昌县| 浦北县| 碌曲县| 泽库县| 大洼县| 海伦市| 平武县| 呼和浩特市| 衡南县| 育儿| 怀化市| 泾源县| 江门市| 百色市| 桂东县| 菏泽市| 宜城市| 宿州市| 沐川县| 麻江县| 义马市| 陆川县| 浑源县| 迭部县| 句容市|