How does throttleTime operator's config parameter work? (ThrottleConfig)

醉酒当歌 提交于 2020-06-08 18:24:05

问题


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:

  1. { leading: true, trailing: false }:
    default option, after receiving event skips other events for specified duration and then repeat.
  2. { leading: false, trailing: true }:
    ???
  3. { leading: false, trailing: false }:
    Tested this and the Observable doesn't emit anything at all.
  4. { leading: true, trailing: true }:
    ???

回答1:


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.

leading and trailing specify whether an item should be emitted at the beginning or end of a throttle interval.

leading: Emit the item that starts a new throttle interval at the beginning of the throttle interval.

trailing: Emit the last item received from the source at the end of a throttle interval.

Visualisation

https://rxviz.com/v/xOvKPypJ

{ leading: true, trailing: false }

source:               --0----1----2----3----4----5----6----7----8-- 
throttle interval:    --[~~~~~~~~~~~~~~~~~]-[~~~~~~~~~~~~~~~~~]-[~~ 
output:               --0-------------------4-------------------8--
{ leading: false, trailing: true }

source:               --0----1----2----3----4----5----6----7----8-- 
throttle interval:    --[~~~~~~~~~~~~~~~~~]-[~~~~~~~~~~~~~~~~~]-[~~ 
output:               --------------------3-------------------7----
{ leading: true, trailing: true }

source:               --0----1----2----3----4----5----6----7----8-- 
throttle interval:    --[~~~~~~~~~~~~~~~~~]-[~~~~~~~~~~~~~~~~~]-[~~  
output:               --0-----------------3-4-----------------7-8--
{ leading: false, trailing: false }

source:               --0----1----2----3----4----5----6----7----8--
throttle interval:    --[~~~~~~~~~~~~~~~~~]-[~~~~~~~~~~~~~~~~~]-[~~
output:               ---------------------------------------------


来源:https://stackoverflow.com/questions/57059666/how-does-throttletime-operators-config-parameter-work-throttleconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!