繁体   English   中英

RxJS 中是否有 publishOnce 类型的运算符?

[英]Is there a publishOnce sort of operator in RxJS?

const xSubject = new BehaviorSubject(undefined);
someSource$.subscribe(xSubject);
emitWhenSomeSourceEmits$.pipe(
  concatMap(
    (emission) => of(emission)
        .pipe(
          delayWhen(
            () => someSource$.pipe(
              filter(val => !!val),
              ...
              tap(() => xSubject.next(undefined)),
            ),
        );
  ),
);

期望:emitWhenSomeSourceEmits$ 的排放应该延迟到 someSource$ 发出新值。

如果我不使用 BehaviorSubject,那么如果 someSource$ 在 emitWhenSomeSourceEmits$ 之前发射,则后者的发射会错误地延迟到下一次发射。

如果我只使用 BehaviorSubject,emitWhenSomeSourceEmits 的第一次发射将与 someSource 计时。 但是来自 emitWhenSomeSourceEmits 的第二次发射将使用 Behaved 值并无论如何发射。

为了克服它,我一直在为 xSubject 敲一个undefined ,以便从 emitWhenSomeSourceEmits 发出的下一个发射等待来自 someSource 的新值。

有没有更好的和非hacky的方法来做到这一点?

zip运算符 ( https://rxjs-dev.firebaseapp.com/api/index/function/zip ) 听起来很适合这个,所以只需zip(someSource$, emitWhenSomeSourceEmits$)

暂无
暂无

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

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