RxJS - 仅当其他人在延迟期间不发射时才发射 [英] RxJS - emit only if other does not emit during delay

查看:38
本文介绍了RxJS - 仅当其他人在延迟期间不发射时才发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个 observable.每次第一个发射时,我想等待 2 秒钟,看看在此期间另一个可观察对象是否发射了一些东西.如果是,不要发出任何东西.如果没有,则发出.

Suppose I have two observables. Every time the first one emits, I want to wait 2 seconds to see whether during that period the other observable emits something. If yes, do not emit anything. If no, emit.

示例:

const start$ = this.events.pipe(
  delay(1000),
  takeUntil(theSecondObservable) // WHAT SHOULD BE HERE?
).subscribe((value) => {
  console.log(value);
});

推荐答案

我能够在上述答案的帮助下解决它.但是,他们并没有完全回答我的问题,可能有更好的方法.我的解决方案:

I was able to solve it with the help of the answers above. However, they did not completely answer my question and there might be a better way. My solution:

const end$ = this.router.events.pipe(
  map(() => false),
);
const start$ = this.router.events.pipe(
  map(() => true),
);

merge(
  end$,
  start$.pipe(
    switchMap(() => {
      return race(end$, of(true).pipe(delay(2000)));
    }),
    filter((value) => value === true) // this really works only with booleans, not sure how I'd do it with more 
                                                            // complex objects
  )
).subscribe((value) => {
  console.log(value);
});

这篇关于RxJS - 仅当其他人在延迟期间不发射时才发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆