Repository navigation
Fix middle functionality, add end option, migrate tests to fake timers
#37
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
Open
iansan5653
wants to merge
14
commits into
github:main
Choose a base branch
from
iansan5653:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d471677
Update tests to use fake timers and run without browser
iansan5653 aab84f9
Refactor and fix bugs, update tests
iansan5653 88c060c
Add tests for new `end` option
iansan5653 aa50567
Update readme to document `end` and correct behavior
iansan5653 ab8d78a
Apply suggestions from code review
iansan5653 88b9a4e
Add more explicit marble tests for every config
iansan5653 2b54027
Fix middle behavior near start of sequence
iansan5653 983136f
Merge branch 'main' of https://github.com/iansan5653/mini-throttle
iansan5653 bd0ded6
Extract `queueCall` helper
iansan5653 ec9908c
Update behavior to work more like lodash, when possible
iansan5653 1764589
Remove unused flow types
iansan5653 519f5c2
Fix `this` type accuracy
iansan5653 53b6c6c
Update comments and error messages
iansan5653 449421e
Merge branch 'main' into main
iansan5653 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,68 +1,83 @@ | ||
| export interface ThrottleOptions { | ||
| /** | ||
| * Fire immediately on the first call. | ||
| * Fire at the start of a sequence of calls. | ||
| */ | ||
| start?: boolean | ||
| /** | ||
| * Fire as soon as `wait` has passed. | ||
| * Fire in the middle of a sequence of calls when `wait` has passed. | ||
| */ | ||
| middle?: boolean | ||
| /** | ||
| * Cancel after the first successful call. | ||
| * Fire at the end of a sequence of calls. | ||
| */ | ||
| end?: boolean | ||
| /** | ||
| * Cancel after the first successful call. Will never call again. | ||
| */ | ||
| once?: boolean | ||
| } | ||
|
|
||
| interface Throttler<T extends unknown[]> { | ||
| (...args: T): void | ||
| /** Cancel any pending calls and never call the function again. */ | ||
| cancel(): void | ||
| } | ||
|
|
||
| export function throttle<T extends unknown[]>( | ||
| callback: (...args: T) => unknown, | ||
| wait = 0, | ||
| {start = true, middle = true, once = false}: ThrottleOptions = {} | ||
| {start = true, middle = true, once = false, end = true}: ThrottleOptions = {} | ||
| ): Throttler<T> { | ||
| let innerStart = start | ||
| let last = 0 | ||
| let timer: ReturnType<typeof setTimeout> | ||
| let lastCall = 0 | ||
| let lastAttempt = 0 | ||
| let endTimer: ReturnType<typeof setTimeout> | ||
| let cancelled = false | ||
| function fn(this: unknown, ...args: T) { | ||
| if (cancelled) return | ||
| const delta = Date.now() - last | ||
| last = Date.now() | ||
|
|
||
| if (start && middle && delta >= wait) { | ||
| innerStart = true | ||
| } | ||
| function fn(this: Throttler<T>, ...args: T) { | ||
| if (cancelled) return | ||
|
|
||
| if (innerStart) { | ||
| innerStart = false | ||
| const makeCall = () => { | ||
| clearTimeout(endTimer) | ||
| lastCall = Date.now() | ||
| callback.apply(this, args) | ||
| if (once) fn.cancel() | ||
| } else if ((middle && delta < wait) || !middle) { | ||
| clearTimeout(timer) | ||
| timer = setTimeout( | ||
| () => { | ||
| last = Date.now() | ||
| callback.apply(this, args) | ||
| if (once) fn.cancel() | ||
| }, | ||
| !middle ? wait : wait - delta | ||
| ) | ||
| } | ||
|
|
||
| const isStartOfSequence = Date.now() - lastAttempt >= wait | ||
| lastAttempt = Date.now() | ||
|
|
||
| // start: If it has been a sufficient time since the last attempt, start a new sequence | ||
| if (start && isStartOfSequence) { | ||
| makeCall() | ||
| return | ||
| } | ||
|
|
||
| // middle: If it has been sufficient time since the last successful call, make another call | ||
| if (middle && Date.now() - lastCall >= wait && !isStartOfSequence) { | ||
| makeCall() | ||
| return | ||
| } | ||
|
|
||
| // end: Schedule a call for after the sequence ends, if it hasn't already been handled | ||
| if (end) { | ||
| clearTimeout(endTimer) | ||
| endTimer = setTimeout(() => makeCall(), wait) | ||
| } | ||
| } | ||
|
|
||
| fn.cancel = () => { | ||
| clearTimeout(timer) | ||
| clearTimeout(endTimer) | ||
| cancelled = true | ||
| } | ||
|
|
||
| return fn | ||
| } | ||
|
|
||
| // debounce is just throttle but with only an ending call | ||
| export function debounce<T extends unknown[]>( | ||
| callback: (...args: T) => unknown, | ||
| wait = 0, | ||
| {start = false, middle = false, once = false}: ThrottleOptions = {} | ||
| {start = false, middle = false, once = false, end = true}: ThrottleOptions = {} | ||
| ): Throttler<T> { | ||
| return throttle(callback, wait, {start, middle, once}) | ||
| return throttle(callback, wait, {start, middle, once, end}) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.