RxJS:auditTime 和 sampleTime 之间的区别? [英] RxJS: Difference between auditTime and sampleTime?

查看:108
本文介绍了RxJS:auditTime 和 sampleTime 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can't find any relevant posts about this and I can't figure out the nuance from the documentation, what is the difference between auditTime and sampleTime operators?

推荐答案

auditTime

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

auditTime

auditTime(ms) will keep storing the latest value for ms milliseconds. After ms have passed, if any value exists, it will pass along as a 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

同样值得注意的是,这个计时器仅在至少有一个值到达时才开始.

It's also worth noticing that this timer starts only when at least one value has arrived.

也许可视化 source/a> 会有所帮助:

Maybe visualizing the source code would help:

_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 已发出/完成时),该值将被传递:

When the timer expires(i.e when the inner observable has emitted/completed), the value will be passed along:

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


sampleTime

sampleTime(ms), like auditTime(ms) will keep track of the latest arrived value and will emit it further in the chain, with the exception that in sampleTime the timer(which decides when to emit the value) is always active. This means that no matter if any value as arrived since the last emission, the timer will run. Now, if no new value arrived, it will simply not pass along the value.

让我们探索它的源代码/a>:

Let's explore its source code:

_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);
  }
}

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

Note that the value can be the same as the one which was previously emitted, but it must arrive while the current timer is active.

sampleTime 默认使用 AsyncAction,它们由 AsyncScheduler管理.换句话说,在这种情况下,timer 是通过 setInterval 实现的.

sampleTime by default uses AsyncActions, which are managed by the AsyncScheduler. Put differently, the timer, in this case, is achieved with setInterval.

sample(notifier) 遵循相同的逻辑,只是没有调度器,timernotifier 定义,它是一个 Observable.

sample(notifier) follows the same logic, except that there is no scheduler, and the timer is defined by the notifier, which is an Observable.

auditTime相比:

u - units of time

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

^   ^   ^   ^   ^

^ - when the timer starts

这篇关于RxJS:auditTime 和 sampleTime 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆