RxJS:auditTime 和 sampleTime 之间的区别?

mii*_*alo 4 javascript rxjs reactive

我找不到关于此的任何相关帖子,也无法从文档中找出细微差别,auditTime 和 sampleTime 运算符之间有什么区别?

And*_*tej 7

审核时间

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
Run Code Online (Sandbox Code Playgroud)

还值得注意的是,此计时器仅在至少有一个值到达时才启动。

也许可视化源代码会有所帮助:

_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
  }
}
Run Code Online (Sandbox Code Playgroud)

当计时器到期时(即当内部 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

采样时间

sampleTime(ms)auditTime(ms)将跟踪最新赶到价值的,将进一步发出它在链,与例外,在sampleTimetimer(这决定何时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);
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,该值可以与先前发出的值相同,但它必须在当前计时器处于活动状态时到达。

sampleTime默认情况下会AsyncActions,这是管理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
Run Code Online (Sandbox Code Playgroud)