ThrottleTime 操作员的配置参数如何工作?(油门配置)

Gog*_*eli 12 javascript rxjs typescript rxjs6

我已经阅读了油门时间文档,但我没有完全了解操作员。

我知道如何throttleTime(1000)运作。事件到达后,它将跳过所有后续事件 1 秒,然后再次开始此过程。

我难以理解的是究竟是如何ThrottleConfig工作的,这是操作符的第三个参数。

throttleTime<T>(
  duration: number, 
  scheduler: SchedulerLike = async, 
  config: ThrottleConfig = defaultThrottleConfig): MonoTypeOperatorFunction<T>
Run Code Online (Sandbox Code Playgroud)

leadingtrailing属性如何改变源 Observable 的功能?

我已经阅读了很多文档,但他们没有清楚地解释这一点。

所以有四种选择:

  1. { leading: true, trailing: false }:
    默认选项,接收事件后跳过指定持续时间的其他事件,然后重复。
  2. { leading: false, trailing: true }
    ???
  3. { leading: false, trailing: false }
    对此进行了测试,并且 Observable 根本不发出任何内容。
  4. { leading: true, trailing: true }
    ???

fri*_*doo 21

throttleTime当它接收到一个新值并且尚未被限制时,将开始一个新的限制间隔(一个不会发出任何项目的时间段)。此节流间隔的长度取决于您提供的持续时间。

与RxJS 7新的油门间隔时的拖尾值在一个的端部发射也会启动油门间隔

leadingtrailing指定是否应在节流间隔的开始或结束时发出项目。

leading:在新的节流间隔开始时发出一个项目。

trailing:节流间隔结束时发出从源接收的最后一项。

可视化

RxJS 6 & 7 - trailing: false

throttleTime(12 ticks, { leading: true, trailing: false })

source_1:           --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval:  --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1:           --0----------------4---------------------8-----------------


source_2:           --0--------1------------------2--------------3---4---------
throttle interval:  --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2:           --0---------------------------2--------------3-------------
Run Code Online (Sandbox Code Playgroud)
throttleTime(12 ticks, { leading: false, trailing: false })

source_1:           --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval:  --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1:           -----------------------------------------------------------


source_2:           --0--------1------------------2--------------3---4---------
throttle interval:  --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2:           -----------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

RxJS 6 - trailing: true

throttleTime(12 ticks, { leading: true, trailing: true })

source_1:           --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval:  --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1:           --0-----------3----4-----------7---------8-----------9-----


source_2:           --0--------1------------------2--------------3---4---------
throttle interval:  --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2:           --0-----------1---------------2--------------3-----------4-
Run Code Online (Sandbox Code Playgroud)
throttleTime(12 ticks, { leading: false, trailing: true })

source_1:           --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval:  --[~~~~~~~~~~~]----[~~~~~~~~~~~]---------[~~~~~~~~~~~]-----
output_1:           --------------3----------------7---------------------9-----


source_2:           --0--------1------------------2--------------3---4---------
throttle interval:  --[~~~~~~~~~~~]---------------[~~~~~~~~~~~]--[~~~~~~~~~~~]-
output_2:           --------------1---------------------------2--------------4-
Run Code Online (Sandbox Code Playgroud)

RxJS 7 - trailing: true

throttleTime(12 ticks, { leading: true, trailing: true })

source_1:           --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval:  --[~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~
output:             --0-----------3-----------6-----------7-----------9--------


source_2:           --0--------1------------------2--------------3---4---------
throttle interval:  --[~~~~~~~~~~~I~~~~~~~~~~~]---[~~~~~~~~~~~]--[~~~~~~~~~~~I~
output_2:           --0-----------1---------------2--------------3-----------4-
Run Code Online (Sandbox Code Playgroud)
throttleTime(12 ticks, { leading: false, trailing: true })

source_1:           --0--1-----2--3----4--5-6---7------------8-------9---------
throttle interval:  --[~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~~~~I~~~~~~~~
output:             --------------3-----------6-----------7-----------9--------


source_2:           --0--------1------------------2--------------3---4---------
throttle interval:  --[~~~~~~~~~~~I~~~~~~~~~~~]---[~~~~~~~~~~~I~~~~~~~~~~~]----
output_2:           --------------1---------------------------2-----------4----
Run Code Online (Sandbox Code Playgroud)

  • 有人需要把这个答案放在官方文档中,前导、尾随...WTF?!谢谢。顺便说一句,你的可视化现在是一个死链接 (3认同)
  • Rxjs 7 中 {leading: true, Trailing: true} 情况的行为已更改。现在它将始终确保受限制数量的输出事件之间的间距。https://github.com/ReactiveX/rxjs/commit/ea84fc4dce84e32598701f79d9449be00a05352c (2认同)