首页 > 解决方案 > 我正在尝试构建一个动态元流对象,该对象在运行之前等待一些流完成

问题描述

问题:

我有许多流输入服务。当单击按钮时,当前订阅其服务流的所有活动组件,更新所述流。我需要遍历该服务中的所有流,并从不为空的流中构建一个 json 对象。

流服务

masterSubmit$: BehaviorSubject<string> = new BehaviorSubject(null);
addForm$: BehaviorSubject<model> = new BehaviorSubject(null);
addSubForm$: BehaviorSubject<otherModel> = new BehaviorSubject(null);

RxJSOperatorThatEmitsAllStreamsAtSameTime(
masterSubmit$,
addForm$,
addSubForm$,
).subscribe((data) => {
  if(data[0] == null){
    return;
  }
  if(data[1] != null){
    jsonObject.properties = {data[1]}
  }
  etc..

  if(data[0] == 'postNewThingy'){
    this.apiStuff.postThingy(jsonObject);
  }
})

这就是我想要的,任何建议都会很棒!谢谢!

标签: angularasynchronousreactive

解决方案


嗨,您可以使用 combineLatest RXJS 运算符等待所有调用完成

    combineLatest(masterSubmit$,
addForm$,
addSubForm$,).subscribe(
      ([masterSubmit, addForm, addSubForm]) => {

        console.log(
          `masterSubmit: ${masterSubmit},
         addForm: ${addForm},
         addSubForm: ${addSubForm}`
        );
      }
    );

推荐阅读