Merge pull request #2728 from woocommerce/PCP-3809-settings-data-store-welcome-screen

Settings data store welcome screen (3809)
This commit is contained in:
Emili Castells 2024-10-25 11:06:42 +02:00 committed by GitHub
commit d0723fe513
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 823 additions and 119 deletions

View file

@ -1,9 +1,44 @@
export const debounce = ( callback, delayMs ) => {
let timeoutId = null;
return ( ...args ) => {
window.clearTimeout( timeoutId );
timeoutId = window.setTimeout( () => {
callback.apply( null, args );
}, delayMs );
const state = {
timeoutId: null,
args: null,
};
/**
* Cancels any pending debounced execution.
*/
const cancel = () => {
if ( state.timeoutId ) {
window.clearTimeout( state.timeoutId );
}
state.timeoutId = null;
state.args = null;
};
/**
* Immediately executes the debounced function if there's a pending execution.
* @return {void}
*/
const flush = () => {
// If there's nothing pending, return early.
if ( ! state.timeoutId ) {
return;
}
callback.apply( null, state.args || [] );
cancel();
};
const debouncedFunc = ( ...args ) => {
cancel();
state.args = args;
state.timeoutId = window.setTimeout( flush, delayMs );
};
// Attach utility methods
debouncedFunc.cancel = cancel;
debouncedFunc.flush = flush;
return debouncedFunc;
};