I have read the throttleTime documentation, but I don't get the operator fully.
I know how throttleTime(1000)
works. After an event arrives it will skip all subsequent events for 1 second and then start this process again.
What I have trouble to understand is how exactly ThrottleConfig
works, which is the third parameter of the operator.
throttleTime<T>(
duration: number,
scheduler: SchedulerLike = async,
config: ThrottleConfig = defaultThrottleConfig): MonoTypeOperatorFunction<T>
How do leading
and trailing
properties change the functionality of the source Observable?
I have read many documentations but they don't clearly explain this.
So there are four options:
{ leading: true, trailing: false }
:{ leading: false, trailing: true }
:{ leading: false, trailing: false }
:{ leading: true, trailing: true }
:throttleTime
will start a new throttle interval (a time period in which no items will be emitted) when it receives a new value and isn't already throttled. The length of this throttle interval is determined by the duration you supply.
With RxJS 7 a new throttle interval is also started when a trailing value is emitted at the end of a throttle interval.
leading
and trailing
specify whether an item should be emitted at the beginning or end of a throttle interval.
leading:
Emit an item at the beginning of a new throttle interval.
trailing:
Emit the last item received from the source at the end of a throttle interval.
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-------------
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: -----------------------------------------------------------
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-
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-
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-
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----