RxJS dynamic source of observables

前提是你 提交于 2020-02-04 05:30:48

问题


I want to create an Observable of many observables (merge them). This could be achieved with merge(...arrayOfObservables). The problem is that some time this array will be changed and the observable should subscribe to the new observables, too.


回答1:


You can push new Observables to an array and then emit the array and subscribe to them with switchMap.

import { of, merge, BehaviorSubject } from 'rxjs'; 
import { switchMap } from 'rxjs/operators';

const s = new BehaviorSubject([of(1), of(2), of(3)]);

s.pipe(
  switchMap(array => merge(...array)),
).subscribe(x => console.log(x));

s.next([...s.getValue(), of(4)]);

Live demo: https://stackblitz.com/edit/rxjs-vmcqs9



来源:https://stackoverflow.com/questions/59916768/rxjs-dynamic-source-of-observables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!