Restructure project as Worldshaper
This commit is contained in:
parent
ab891a315c
commit
b4dbd4ee8e
583 changed files with 279 additions and 189269 deletions
47
src/worldshaperStudio/debounce.ts
Normal file
47
src/worldshaperStudio/debounce.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
export function createDebouncedCallback<T extends unknown[]>(
|
||||
callback: (...args: T) => void,
|
||||
delayMs = 120,
|
||||
) {
|
||||
let timerId = 0;
|
||||
let lastArgs: T | null = null;
|
||||
|
||||
const run = () => {
|
||||
timerId = 0;
|
||||
if (!lastArgs) {
|
||||
return;
|
||||
}
|
||||
const args = lastArgs;
|
||||
lastArgs = null;
|
||||
callback(...args);
|
||||
};
|
||||
|
||||
const debounced = (...args: T) => {
|
||||
lastArgs = args;
|
||||
if (timerId) {
|
||||
window.clearTimeout(timerId);
|
||||
}
|
||||
timerId = window.setTimeout(run, Math.max(0, Number(delayMs) || 0));
|
||||
};
|
||||
|
||||
debounced.flush = () => {
|
||||
if (timerId) {
|
||||
window.clearTimeout(timerId);
|
||||
run();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
debounced.cancel = () => {
|
||||
if (!timerId) {
|
||||
lastArgs = null;
|
||||
return false;
|
||||
}
|
||||
window.clearTimeout(timerId);
|
||||
timerId = 0;
|
||||
lastArgs = null;
|
||||
return true;
|
||||
};
|
||||
|
||||
return debounced;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue