RxJS哪个运算符用于“ onIdle”?

问题描述

我的用例如下-我有一个针对不同元素的操作流,并且我只想在每个对象闲置了一定时间或接收到另一个元素时才对每个对象调用“提交”。 / p>

我正在尝试groupBy并进行反跳,但并没有涵盖所有情况-例如。

action.pipe(
  groupBy(item -> item.key),debounceTime(1000),mergeMap(item -> { 
           item.commit()})
)

解决方法

我不确定您的目标是什么

让我们以您的情况为例,其中 A => B => A 少于最小空闲时间

选项1 :每种元素类型都应具有各自的空闲状态-类型A的第二次发射将被忽略
选项2 。由于没有连续的序列,因此第二个A将不会被忽略

OPTION 1示例:

action.pipe(
    groupBy(item => item.key),mergeMap(group => group.pipe(debounceTime(1000))),mergeMap(item => item.commit())
)

可选:

const IDLE_TIME = XXXX;
action.pipe(
    groupBy(item => item.key),mergeMap(group => merge(
        group.pipe(first()),group.pipe(
            timeInterval(),filter(x => x.interval > IDLE_TIME),map(x => x.value)
        )
    )),mergeMap(item => item.commit())
)

OPTION 2示例:

 action.pipe(
     pairwise(),debounce(([previous,current]) => previous.key == current.key? timer(1000) : EMPTY),map(([pre,current]) => current),mergeMap(item => item.commit())
 )
,

您可以使用auditTimescanfilter

评估空闲状态
action.pipe(
  //add the idle property to the item
  map(item => ({ ...item,idle: false})),//audit the stream each second
  auditTime(1000),//then use scan to with previous emission at audit time
  scan(
     (prev,curr) => {
       //then if the key remains the same then we have an idle state
       if (prev.key === curr.key) {
          //return changed object to indicate we have an idle state
          return Object.assign({},curr,{idle: true});
       } else {
          //otherwise just return the non idle item
          return curr 
       }
    //seed with an object that cannot match the first emission key
    },{ key: null }
  ),//then filter out all emissions indicated as not idle
  filter(item => item.idle === true)
  //and commit
  mergeMap(item => item.commit())
)

然后您可以使用distinctUntilKeyChanged 达到第二个条件

action.pipe(
  distinctUntilKeyChanged('key'),mergeMap(item => item.commit())
)

我对redux-observable不熟悉,但是您通常会merge这两个可观察变量然后最后提交。