RxJS 限制相同的值但让新值通过 [英] RxJS throttle same value but let new values through
问题描述
给你",有人说,你得到了这个输入的值流,你有点想对它做 distinctUntilChanged() ......
"Here you have", someone says and you are given this input stream of values that you somewhat want to do distinctUntilChanged() upon...
Input: '1-1----11---2--1122----1---2---2-2-1-2---|'
Output: '1-----------2--1-2-----1---2-------1-2---|'
到目前为止没什么奇怪的,
但是现在有人说如果同样的值再次出现没关系",但前提是不要很快!".我希望至少 '----'
在相同的值之间打勾.好吧"你说,你加了一个油门
Nothing weird so far,
But now someone says "it's okey" if the same value comes again, "but only if it's not to soon!". I want at least '----'
ticks between the same value. "Okey" you say and you add a throttle
const source = new Subject<number>();
// mysterious cave troll is randomly source.next(oneOrTwo)
const example = source.pipe(throttle(val => interval(4000)));
Input: '1-1----11---2--1122----1---2---2-2-1-2-----|'
Output: '1------1----2----2-----1-------2-----2-----|'
这不是我想要的!看看你错过的所有价值",指的是你限制了所有正在传输的价值.
"That's not what I want! Look at all the value you missed", referring to that you throttle in regards to all values being streamed.
Input: '1-1----11---2--1122----1---2---2-2-1-2-----|'
Output: '1------1----2----2-----1-------2-----2-----|'
'-------------->1<--------->2<----->1<------|' <-- Missed values
来,让我给你看看"神秘人说并给了你这个
"Here, let me show show you" the mysterious man says and gives you this
Input: '1-1----11---2--1112----1---2---2-2-1-2-----|'
Output: '1------1----2--1--2----1---2-----2-1-------|'
我对此的回答是,感觉组合窗口不行.
My answer to this is that it feels like a combined window wouldn't do.
来自更有经验的人,
这是一个很难解决的问题吗?(或者我错过了一个明显的解决方案)
From someone more experienced,
is this a hard problem to solve? (or have I missed an obvious solution)
推荐答案
我找到了一个有效的解决方案,有人对此有任何看法吗?
I found a solution that works, does someone have any take on this?
source.pipe(
windowTime(4000),
concatMap(obs => obs.pipe(distinct()))
);
之前的示例,在 StackBlitz 示例
更新:这实际上并不是 100% 有效.它只考虑当前窗口.所以你可以例如有
UPDATE: this does not actually work 100%. It only take the current window into consideration. So you can for example have
`[1-12][2---]` which would give `1--22---|`
其中 [----]
表示时间窗口.换句话说,如果一个值在一个窗口中最后发出,而在下一个窗口中首先发出,则相同的值将依次通过.
where [----]
would represent the time window. In other words, if a value is first emitted last in one window and emitted first in the next window, the same value will pass through right after each other.
感谢@eric99 让我意识到这一点.
这篇关于RxJS 限制相同的值但让新值通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!