简体   繁体   English

如何在 rxjs 中合并父子可观察对象

[英]How can I merge parent and child observable in rxjs

I have created a parent observable with a child observable.我创建了一个带有子可观察对象的父可观察对象。 When the child observable completes it completes the parent observable.当子 Observable 完成时,它完成父 Observable。

Is there a nicer way to do this without having to use subscripe() twice.有没有更好的方法可以做到这一点而不必使用subscripe()两次。

Code:代码:

const parentObservable = new Observable((observer) => {
  
  if (condition) {
     observer.complete();
     return;
  }

  this.childObservable().subscribe(() => observer.complete());
});

parentObservable.subscribe();

EDIT: We want to be able to not subscribe to the child if a condition is met编辑:如果满足条件,我们希望能够不订阅孩子

The merge operator allows you to merge multiple observables into a single observable. merge 运算符允许您将多个 observable 合并为一个 observable。 You can use this operator to merge the parent observable and the child observable.您可以使用此运算符合并父可观察对象和子可观察对象。

const parentObservable = new Observable((observer) => {
  // random sync code
});

const childObservable = this.childObservable();

merge(parentObservable, childObservable).subscribe();

If you want to complete the parent observable when the child observable completes, you can use the concat operator instead.如果你想在子 Observable 完成时完成父 Observable,你可以使用 concat 运算符。 The concat operator will emit the values from the first observable, and then emit the values from the second observable once the first observable completes. concat 运算符将从第一个可观察对象发出值,然后在第一个可观察对象完成后从第二个可观察对象发出值。

import { concat } from 'rxjs';

const combinedObservable = concat(parentObservable, childObservable);
combinedObservable.subscribe();

This will complete the parent observable when the child observable completes.这将在子可观察对象完成时完成父可观察对象。

merge operator combines all the observables into a single stream and emits value as they come. merge运算符将所有 observables 组合成一个 stream 并在它们出现时发出值。

concat operator concatenate the observables and emits value only after the first observable completes. concat运算符连接可观察对象并仅在第一个可观察对象完成后才发出值。

According to your description根据你的描述

When the child observable completes it completes the parent observable.当子 Observable 完成时,它完成父 Observable。

I believe you could use takeUntil .我相信你可以使用takeUntil

const parentObservable = new Observable((observer) => { ... });

parentObservable.pipe(takeUntil(childObservable)).subscribe();

Whenever childObservable emits a value the pipe will complete and the teardown logic of parentObservable (if there is one) will be run.每当childObservable发出一个值时,pipe 将完成, parentObservable的拆卸逻辑(如果有的话)将运行。


Your example's new Observable constructor never emits any values, so it's going to be a pretty useless observable.您示例的new Observable构造函数从不发出任何值,因此它将是一个非常无用的可观察对象。 You can just drop it and not worry about completing a useless observable.你可以放弃它而不用担心完成一个无用的可观察对象。

Taking this into account, your example code can be re-written like this:考虑到这一点,您的示例代码可以这样重写:

const parentObservable = defer(() => {
  if(condition){
    return EMPTY;
  }
  return this.childObservable();
});
parentObservable.subscribe();

This is conditionally subscribing to childObservable .这是有条件地订阅childObservable
This can be simplified further as:这可以进一步简化为:

const parentObservable = defer(
  () => condition? EMPTY: this.childObservable()
);
parentObservable.subscribe();

or:或者:

defer(() => condition? EMPTY: this.childObservable())
  .subscribe();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM