-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added batch actions and the new "shouldClearFilters" option (#391)
- Actions called one after another will now both be applied, and consolidated into a single API request - There is now a "shouldClearFilters" flag available on `setSearchTerm` and `SearchBox` which controls whether or not filters will be cleared when the search term is changed.
- Loading branch information
1 parent
c14cc70
commit 0ef3535
Showing
21 changed files
with
498 additions
and
87 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,30 +1,76 @@ | ||
import debounceFn from "debounce-fn"; | ||
|
||
export default class DebounceManager { | ||
class DebounceManager { | ||
debounceCache = {}; | ||
|
||
/* | ||
The purpose of this is to: | ||
Dynamically debounce and cache a debounced version of a function at the time of calling that function. This avoids | ||
managing debounced version of functions locally. | ||
Assumption: | ||
Functions are debounced on a combination of unique function and wait times. So debouncing won't work on | ||
subsequent calls with different wait times or different functions. That also means that the debounce manager | ||
can be used for different functions in parallel, and keep the two functions debounced separately. | ||
*/ | ||
runWithDebounce(wait, fn, ...parameters) { | ||
/** | ||
* Dynamically debounce and cache a debounced version of a function at the time of calling that function. This avoids | ||
* managing debounced version of functions locally. | ||
* | ||
* In other words, debounce usually works by debouncing based on | ||
* referential identity of a function. This works by comparing provided function names. | ||
* | ||
* This also has the ability to short-circuit a debounce all-together, if no wait | ||
* time is provided. | ||
* | ||
* Assumption: | ||
* Functions are debounced on a combination of unique function name and wait times. So debouncing won't work on | ||
* subsequent calls with different wait times or different functions. That also means that the debounce manager | ||
* can be used for different functions in parallel, and keep the two functions debounced separately. | ||
* | ||
* @param {number} wait Milliseconds to debounce. Executes immediately if falsey. | ||
* @param {function} fn Function to debounce | ||
* @param {function} functionName Name of function to debounce, used to create a unique key | ||
* @param {...any} parameters Parameters to pass to function | ||
*/ | ||
runWithDebounce(wait, functionName, fn, ...parameters) { | ||
if (!wait) { | ||
return fn(...parameters); | ||
} | ||
|
||
const key = fn.toString() + wait.toString(); | ||
const key = `${functionName}|${wait.toString()}`; | ||
let debounced = this.debounceCache[key]; | ||
if (!debounced) { | ||
this.debounceCache[key] = debounceFn(fn, { wait }); | ||
debounced = this.debounceCache[key]; | ||
} | ||
debounced(...parameters); | ||
} | ||
|
||
/** | ||
* Cancels existing debounced function calls. | ||
* | ||
* This will cancel any debounced function call, regardless of the debounce length that was provided. | ||
* | ||
* For example, making the following series of calls will create multiple debounced functions, because | ||
* they are cached by a combination of unique name and debounce length. | ||
* | ||
* runWithDebounce(1000, "_updateSearchResults", this._updateSearchResults) | ||
* runWithDebounce(500, "_updateSearchResults", this._updateSearchResults) | ||
* runWithDebounce(1000, "_updateSearchResults", this._updateSearchResults) | ||
* | ||
* Calling the following will cancel all of those, if they have not yet executed: | ||
* | ||
* cancelByName("_updateSearchResults") | ||
* | ||
* @param {string} functionName The name of the function that was debounced. This needs to match exactly what was provided | ||
* when runWithDebounce was called originally. | ||
*/ | ||
cancelByName(functionName) { | ||
Object.entries(this.debounceCache) | ||
.filter(([cachedKey]) => cachedKey.startsWith(`${functionName}|`)) | ||
// eslint-disable-next-line no-unused-vars | ||
.forEach(([_, cachedValue]) => cachedValue.cancel()); | ||
} | ||
} | ||
/** | ||
* Perform a standard debounce | ||
* | ||
* @param {number} wait Milliseconds to debounce. Executes immediately if falsey. | ||
* @param {function} fn Function to debounce | ||
*/ | ||
DebounceManager.debounce = (wait, fn) => { | ||
return debounceFn(fn, { wait }); | ||
}; | ||
|
||
export default DebounceManager; |
Oops, something went wrong.