简体   繁体   English

如何组合可观察对象,但在每个对象发生时发出一个值

[英]How to combine observables but emit a value for each as it occurs

I would like to get each emitted value from the combined observable.我想从组合的 observable 中获取每个发出的值。 I do not need the final value for all which is achieved by operators like combineLatest, forkJoin etc.我不需要通过 combineLatest、forkJoin 等运算符实现的所有最终值。

I am looking for an operator that will make the below code shorter but keep the same behaivor.我正在寻找一个可以使以下代码更短但保持相同行为的运算符。

this.keyPress$.pipe(debounceTime(1000))
.subscribe(
  (d) => {
    console.log('keypress');
    resetInterval();
  }
);

this.mouseMove$.pipe(debounceTime(1000))
.subscribe(
  (d) => {
    console.log('mouseMove');
  }
);


this.click$.pipe(debounceTime(1000))
.subscribe(
  (d) => {
    console.log('click');
  }
);

Is there a way or an operator I can use to achieve this?有没有办法或操作员可以用来实现这一目标?

If I understand the problem right, you may try something along these lines如果我理解正确的问题,你可以尝试这些方面的东西

k$ = this.keyPress$.pipe(
  debounceTime(1000),
  tasp(() => {
    console.log('keypress');
    resetInterval();
  })

m$ = this.mouseMove$.pipe(
  debounceTime(1000),
  tap(() => console.log('mouseMove'))
  );


c$ = this.click$.pipe(
  debounceTime(1000),
  tap(() => console.log('click'))
  )


merge(k$, m$, c$).subscribe()

It does not reduces your code, probably, but requires just one explicit subscription, which may be an advantage in itself since, if for instance you need to unsubscribe at some point, you have only one subscription to deal with.它可能不会减少您的代码,但只需要一个显式订阅,这本身可能是一个优势,因为例如,如果您需要在某个时候取消订阅,那么您只有一个订阅需要处理。

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

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