[英]RxJS: Difference between auditTime and sampleTime?
我找不到任何相关的帖子,也无法从文档中找出细微差别,auditTime 和 sampleTime 运算符有什么区别?
auditTime(ms)
将继续存储ms
毫秒的最新值。 在ms
过去后,如果存在任何值,它将作为next notification
传递。
auditTime(ms) === audit(() => timer(ms, scheduler?))
。
u - units of time
1--3--5----------6-7-- values$
----|-----|----!-----| auditTime(5u)
----3-----5----------7 result
^ ^ ^
! - since the timer did not started, because there was no value after `5`, there won't be any value emitted further in the stream
^ - when the timer starts
还值得注意的是,此计时器仅在至少一个值到达时才启动。
也许可视化源代码会有所帮助:
_next(value: T): void {
this.value = value; // Keep track of the oldest value
this.hasValue = true;
if (!this.throttled) { // If the timer didn't started yet, start it
let duration;
try {
const { durationSelector } = this;
duration = durationSelector(value); // Create observable; if `auditTime(d)`, it will be `() => timer(ms)`
} catch (err) {
return this.destination.error(err);
}
const innerSubscription = subscribeToResult(this, duration); // Subscribe to the inner observable
/* ... */
this.throttled = innerSubscription // Store the subscription
}
}
当计时器到期时(即当内部 observable 已发出/完成时),该值将被传递:
// Called when the inner obs completes/emits a value
clearThrottle() {
const { value, hasValue, throttled } = this;
if (throttled) { // Did we have a timer(a subscription)? If yes, unsubscribe
this.remove(throttled);
this.throttled = null;
throttled.unsubscribe();
}
if (hasValue) { // If we have a value, send it do its destination
this.value = null;
this.hasValue = false;
this.destination.next(value);
}
}
sampleTime(ms)
,如auditTime(ms)
将跟踪最新到达的值,并将在链中进一步发出它,除了在sampleTime
中timer
(决定何时emit
值)始终处于活动状态。 这意味着无论自上次发射以来是否有任何值到达,计时器都会运行。 现在,如果没有新值到达,它就不会传递该值。
让我们探索它的源代码:
_next(value: T) { // Keep track of the oldest value
this.lastValue = value;
this.hasValue = true;
}
notifyNext() { // When time is up, check if any `new` value came in since the last 'sample'
if (this.hasValue) { // If we have a value, then send it further
this.hasValue = false;
this.destination.next(this.lastValue);
}
}
请注意,该值可以与先前发出的值相同,但它必须在当前计时器处于活动状态时到达。
sampleTime
默认使用由AsyncAction
管理的AsyncScheduler
。 换句话说,在这种情况下, timer
是通过setInterval
实现的。
sample(notifier)
遵循相同的逻辑,只是没有调度程序,并且timer
由notifier
定义,它是一个Observable
。
与auditTime
相比:
u - units of time
1--3--5-------6-7-- values$
----|---|---|---|-- auditTime(5u)
----3---5-------7-- result
^ ^ ^ ^ ^
^ - when the timer starts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.