Javascript RxJS:auditTime和sampleTime之间的差异?

Javascript RxJS:auditTime和sampleTime之间的差异?,javascript,rxjs,reactive,Javascript,Rxjs,Reactive,我找不到任何与此相关的帖子,也无法从文档中找出细微差别,auditTime和sampleTime操作符之间有什么区别?auditTime auditTime(ms)将持续存储ms毫秒的最新值。传递ms后,如果存在任何值,它将作为下一个通知传递 auditTime(ms)==audit(()=>定时器(ms,调度器?) 还值得注意的是,该计时器仅在至少一个值到达时启动 也许将这些数据可视化将有助于: _next(value: T): void { this.value = value; //

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

auditTime
auditTime(ms)
将持续存储
ms
毫秒的最新值。传递
ms
后,如果存在任何值,它将作为
下一个通知传递

auditTime(ms)==audit(()=>定时器(ms,调度器?)

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

也许将这些数据可视化将有助于:

_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
  }
}
当计时器过期时(即当内部可观察对象已发出/完成时),将传递该值:

// 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
中的
计时器(决定何时发出该值)始终处于活动状态。这意味着,无论自上次发射以来是否有任何值到达,计时器都将运行。现在,如果没有新值到达,它将不会传递该值

让我们探讨一下它的:

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

sampleTime
默认情况下使用
AsyncAction
s,由
AsyncScheduler
管理。换言之,
定时器
,在这种情况下,是通过
设置间隔
实现的

sample(notifier)
遵循相同的逻辑,只是没有调度程序,
计时器由
通知程序定义,它是一个可观察的

与审核时间相比

u - units of time

1--3--5-------6-7-- values$
----|---|---|---|-- auditTime(5u)
----3---5-------7-- result

^   ^   ^   ^   ^

^ - when the timer starts

你想在上一张图表中使用
sampleTime(5u)
,是吗?@AlexeyGrinko是的!
_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);
  }
}
u - units of time

1--3--5-------6-7-- values$
----|---|---|---|-- auditTime(5u)
----3---5-------7-- result

^   ^   ^   ^   ^

^ - when the timer starts