-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'debounce-sync-and-save'
- Loading branch information
Showing
6 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** throttle( callback, rate ) | ||
* Returns a throttle function with a build in debounce timer that runs after `wait` ms. | ||
* | ||
* Note that the args go inside the parameter and you should be careful not to | ||
* recreate the function on each usage. (In React, see useMemo().) | ||
* | ||
* | ||
* Example usage: | ||
* const callback = throttle((ev) => { doSomethingExpensiveOrOccasional() }, 100) | ||
* target.addEventListener('frequent-event', callback); | ||
* | ||
*/ | ||
|
||
export const throttle = <F extends (...args: Parameters<F>) => ReturnType<F>>( | ||
fn: F, | ||
rate: number | ||
) => { | ||
let timeout: ReturnType<typeof setTimeout> | ||
return function (...args: Parameters<F>) { | ||
clearTimeout(timeout) | ||
timeout = setTimeout(() => { | ||
fn.apply(null, args) | ||
}, rate) | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** Throttle | ||
* Returns a function with a built in throttle timer that runs after `delay` ms. | ||
* | ||
* This function differs from a conventional `throttle` in that it ensures the final | ||
* call will also execute and delays sending the first one until `delay` ms to allow | ||
* additional work to accumulate. | ||
* | ||
* Here's a diagram: | ||
* | ||
* calls +----++++++-----++---- | ||
* dlay ^--v ^--v^--v ^--v | ||
* execs ---+----+---+------+-- | ||
* | ||
* The goal in this design is to create batches of changes without flooding | ||
* communication or storage systems while still feeling responsive. | ||
* (By default we communicate at 10hz / every 100ms.) | ||
* | ||
* Note that the args go inside the parameter and you should be careful not to | ||
* recreate the function on each usage. (In React, see useMemo().) | ||
* | ||
* | ||
* Example usage: | ||
* const callback = debounce((ev) => { doSomethingExpensiveOrOccasional() }, 100) | ||
* target.addEventListener('frequent-event', callback); | ||
* | ||
*/ | ||
|
||
export const throttle = <F extends (...args: Parameters<F>) => ReturnType<F>>( | ||
fn: F, | ||
delay: number | ||
) => { | ||
let lastCall = Date.now() | ||
let wait | ||
let timeout: ReturnType<typeof setTimeout> | ||
return function (...args: Parameters<F>) { | ||
wait = lastCall + delay - Date.now() | ||
clearTimeout(timeout) | ||
timeout = setTimeout(() => { | ||
fn.apply(null, args) | ||
lastCall = Date.now() | ||
}, wait) | ||
} | ||
} |
This file contains 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 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