是否有类似于 throttleTime 的 RXJS 运算符,但不会忽略中间事件?
Is there an RXJS operator similar to throttleTime, but one that doesn't ignore inermediate events?
我有这个示例代码:
interval(500).pipe(
throttleTime(1000)
).subscribe(arg => {
console.log(arg);
});
发出:
0
3
6
...
我知道它每 1000 毫秒发出一次最新值。我的问题是它忽略了不是最新的值。是否有一个类似于 throttleTime 的运算符,但它保存了这些被忽略的值?我希望它发出:
[0]
[1,2,3]
[4,5,6]
...
编辑:理想情况下,我想要听按钮点击的东西。当第一次点击发生时,代码会启动一个 N 毫秒的计时器。用户可以在这段时间内一直点击。一旦 N 毫秒结束,运算符将触发一个数组,其中包含在这 N 毫秒内发生的所有事件。
超级理想,我希望每次用户单击按钮时重置计时器。
您可以使用 bufferToggle
。它根据您的要求在数组中收集值和 returns:
const click$ = fromEvent(document, 'click').pipe(
// count emitions
scan(acc => acc += 1, 0)
)
const timerInterval = () => timer(5000);
// buffer emitions to an array. When first click happens 'turn on' buffer
// and when timer ends turn it off.
// throttle used to avoid turning on buffer on every click
click$.pipe(
bufferToggle(
click$.pipe(throttle(timerInterval)),
timerInterval
),
)
.subscribe(console.log)
但要注意 - 点击间隔之间没有明确的分隔。例如,用户点击的时间可能超过 5 秒,结果会发生两次发射。
但这更像是一个架构任务,需要您解决。
我有这个示例代码:
interval(500).pipe(
throttleTime(1000)
).subscribe(arg => {
console.log(arg);
});
发出:
0
3
6
...
我知道它每 1000 毫秒发出一次最新值。我的问题是它忽略了不是最新的值。是否有一个类似于 throttleTime 的运算符,但它保存了这些被忽略的值?我希望它发出:
[0]
[1,2,3]
[4,5,6]
...
编辑:理想情况下,我想要听按钮点击的东西。当第一次点击发生时,代码会启动一个 N 毫秒的计时器。用户可以在这段时间内一直点击。一旦 N 毫秒结束,运算符将触发一个数组,其中包含在这 N 毫秒内发生的所有事件。
超级理想,我希望每次用户单击按钮时重置计时器。
您可以使用 bufferToggle
。它根据您的要求在数组中收集值和 returns:
const click$ = fromEvent(document, 'click').pipe(
// count emitions
scan(acc => acc += 1, 0)
)
const timerInterval = () => timer(5000);
// buffer emitions to an array. When first click happens 'turn on' buffer
// and when timer ends turn it off.
// throttle used to avoid turning on buffer on every click
click$.pipe(
bufferToggle(
click$.pipe(throttle(timerInterval)),
timerInterval
),
)
.subscribe(console.log)
但要注意 - 点击间隔之间没有明确的分隔。例如,用户点击的时间可能超过 5 秒,结果会发生两次发射。
但这更像是一个架构任务,需要您解决。