Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add leading and trailing for throttle like in lodash #37

Open
Tracked by #175
yumauri opened this issue Jun 14, 2020 · 1 comment · May be fixed by #283
Open
Tracked by #175

Add leading and trailing for throttle like in lodash #37

yumauri opened this issue Jun 14, 2020 · 1 comment · May be fixed by #283
Labels
good first issue Good for newcomers RFC Some new feature that should be discussed
Milestone

Comments

@yumauri
Copy link
Contributor

yumauri commented Jun 14, 2020

Following this comment

To have more industry-like behaviour (lodash), throttle should provide options to indicate whether target should be triggered on the leading and/or trailing edge of the timeout.

leading [= true] (boolean): Specify triggering on the leading edge of the timeout
trailing [= true] (boolean): Specify triggering on the trailing edge of the timeout

If leading and trailing options are true, target is triggered on the trailing edge of the timeout only if the throttled source is triggered more than once during the timeout.

If timeout is 0 and leading is false, target triggering is deferred until to the next tick, similar to setTimeout with a timeout of 0.


const trigger = createEvent()

// by default `leading` is `true` and `trailing` is `true`
const throttled = throttle({ source: trigger, timeout: 100 })

trigger(1)

throttled should be triggered immediately, one time


const trigger = createEvent()

// by default `leading` is `true` and `trailing` is `true`
const throttled = throttle({ source: trigger, timeout: 100 })

trigger(1)
trigger(2)
trigger(3)

throttled should be triggered immediately with payload of 1 and second time after 100ms with payload of 3


const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 100, leading: false })
trigger(1)

throttled should be triggered after 100ms, one time


const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 100, leading: false })
trigger(1)
trigger(2)
trigger(3)

throttled should be triggered after 100ms, one time, with payload of 3 (just like current behaviour)


const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 0, leading: false })
trigger(1)

throttled should be triggered on the next tick, one time (just like current behaviour)


const trigger = createEvent()
const throttled = throttle({ source: trigger, timeout: 100, trailing: false })

trigger(1)
await wait(75)
trigger(2)
await wait(75)
trigger(3)

throttled should be triggered immediately with payload of 1 and second time after 150ms with payload of 3


With combination leading: false, trailing: falsethrottled should not be triggered at all


Playground with lodash's throttle

@sergeysova sergeysova added the RFC Some new feature that should be discussed label Jun 14, 2020
@sergeysova sergeysova changed the title RFC: throttle RFC: throttle({ leading, trailing }) Jun 14, 2020
@sergeysova sergeysova added this to the Next milestone Aug 11, 2020
@sergeysova sergeysova changed the title RFC: throttle({ leading, trailing }) Add leading and trailing for throttle like in lodash Oct 7, 2020
@sergeysova sergeysova removed this from the Next milestone Nov 10, 2020
@sergeysova sergeysova added this to the v1.2 milestone Feb 14, 2021
@GTOsss
Copy link

GTOsss commented Jul 11, 2021

I have a problem due to the lack of the trailing flag. We really need flags.

@sergeysova sergeysova modified the milestones: v1.2, v2.0 Nov 14, 2021
@sergeysova sergeysova mentioned this issue Nov 14, 2021
35 tasks
@sergeysova sergeysova added the good first issue Good for newcomers label Aug 3, 2022
@Dema Dema linked a pull request Apr 27, 2023 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers RFC Some new feature that should be discussed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants