如何解决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())
)
,
评估空闲状态
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
这两个可观察变量然后最后提交。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。