分组和去抖动可观察? [英] Group and debounce observable?

查看:48
本文介绍了分组和去抖动可观察?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 observable,它发出一个包含一些参数的对象.在对象中,其中一个参数(称为 optionId)明确标识了一个选项.我想消除该发射的所有实例.但是,如果出现新的 optionId,我想开始一个新的时钟,并开始一个新的去抖动.

I have an observable that emits an object which contains a few params. In the object, one of the params (called optionId) distinctly identifies an option. I'd like to debounce all instances of that emission. However, if a new optionId shows up, I'd like to start a new clock, and start a new debounce.

这是我正在寻找的示例大理石图:

Here's a sample marble diagram for what i'm looking for:

-----1----1-----1----3----3----3----3-----1---3---1---3---1------>
(magic operators for which I'm unclear)
-------------------1-------------------3-----1---3---1---3---1--->

我有我喜欢的去抖动功能,但它是这样做的:

I have debounce, which I like, but it does this:

-----1----1-----1----3----3----3----3-----1---3---1---3---1------>
debounce
-------------------1-------------------3--------------------1---->

它会在最后跳过所有那些中间的 3... 这有意义吗?

It would skip all those intermediate 3s at the end... does this make sense?

推荐答案

我认为您正在寻找这个:

I think you're looking for this:

source$.pipe(
  groupBy(item => item.optionId),
  map(group => group.pipe(
    debounceTime(1000),
  )),
  mergeAll(),
).subscribe(console.log);

它的作用是将源分组为基于 optionId 的更高阶的 observable,这为您提供每个组的 observable.然后每个组分别映射到一个去抖动版本,最后我们将它们全部合并在一起.

What this does is that it groups the source into a higher order observable based on optionId, which gives you an observable per group. Each group is then separately mapped to a debounced version and in the end we merge it all back together.

使用 map 返回一个 observable 实际上是我们想要的,这是极少数情况之一.:-)

It's one of those rare cases where using map to return an observable is actually what we want. :-)

这篇关于分组和去抖动可观察?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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