Skip to content

Commit

Permalink
introduce debounceOnServer param for UseDebouncedCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
xnimorz committed Nov 5, 2023
1 parent e69efea commit 3dfaf50
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
17 changes: 9 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
## 10.0.0

- _Major breaking change_: replaced `index.modern.js` with `index.mjs`. Might require a little change in your build pipeline
- _Major breaking change_: New option `debounceOnServer`: if you put the option to true, it will run debouncing on server (via `setTimeout`). The new option can break your current server behaviour, as v9.x, it runs all the time and might cause unnessesary server CPU utilisation. Now, by default, debounced callbacks do not happen on server.
- _Minor breaking change_: Replaced `useState` for `useDebounce` with `useReducer`. It might lead to reduced amount of re-renders, as useState is known to have excess re-renders in some corner: https://stackoverflow.com/questions/57652176/react-hooks-usestate-setvalue-still-rerender-one-more-time-when-value-is-equal
- _Minor breaking change_: `useDebouncedCallback` now updates function to call asap. Meaning, if you re-called the hook and it should trigger immediately, it will trigger the newest function all the time.
- Lib size:
883 B: index.js.gz
814 B: index.js.br
850 B: index.mjs.gz
792 B: index.mjs.br
903 B: index.module.js.gz
847 B: index.module.js.br
958 B: index.umd.js.gz
888 B: index.umd.js.br
914 B: index.js.gz
851 B: index.js.br
883 B: index.mjs.gz
826 B: index.mjs.br
938 B: index.module.js.gz
873 B: index.module.js.br
989 B: index.umd.js.gz
919 B: index.umd.js.br
- [Internal] Replaced Enzyme with @testing-library
- [Internal] yarn classic => npm
- [Internal] Updated devDependencies
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,30 @@
"size-limit": [
{
"path": "dist/index.js",
"limit": "1.1 KB"
"limit": "1.2 KB"
},
{
"path": "dist/index.module.js",
"limit": "1.1 KB"
"limit": "1.2 KB"
},
{
"path": "dist/index.umd.js",
"limit": "1.1 KB"
"limit": "1.2 KB"
},
{
"path": "dist/index.js",
"import": "{ useDebounce }",
"limit": "1.1 KB"
"limit": "1.2 KB"
},
{
"path": "dist/index.js",
"import": "{ useDebouncedCallback }",
"limit": "1.1 KB"
"limit": "1.2 KB"
},
{
"path": "dist/index.js",
"import": "{ useThrottledCallback }",
"limit": "1.1 KB"
"limit": "1.2 KB"
}
]
}
23 changes: 21 additions & 2 deletions src/useDebouncedCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface Options extends CallOptions {
* The maximum time the given function is allowed to be delayed before it's invoked.
*/
maxWait?: number;
/**
* If the setting is set to true, all debouncing and timers will happen on the server side as well
*/
debounceOnServer?: boolean;
}

export interface ControlFunctions {
Expand Down Expand Up @@ -122,8 +126,9 @@ export default function useDebouncedCallback<
// Always keep the latest version of debounce callback, with no wait time.
funcRef.current = func;

const isClientSize = typeof window !== 'undefined';
// Bypass `requestAnimationFrame` by explicitly setting `wait=0`.
const useRAF = !wait && wait !== 0 && typeof window !== 'undefined';
const useRAF = !wait && wait !== 0 && isClientSize;

if (typeof func !== 'function') {
throw new TypeError('Expected a function');
Expand All @@ -135,6 +140,8 @@ export default function useDebouncedCallback<
const leading = !!options.leading;
const trailing = 'trailing' in options ? !!options.trailing : true; // `true` by default
const maxing = 'maxWait' in options;
const debounceOnServer =
'debounceOnServer' in options ? !!options.debounceOnServer : false; // `false` by default
const maxWait = maxing ? Math.max(+options.maxWait || 0, wait) : null;

useEffect(() => {
Expand Down Expand Up @@ -222,6 +229,9 @@ export default function useDebouncedCallback<
};

const func: DebouncedState<T> = (...args: Parameters<T>): ReturnType<T> => {
if (!isClientSize && !debounceOnServer) {
return;
}
const time = Date.now();
const isInvoking = shouldInvoke(time);

Expand Down Expand Up @@ -273,7 +283,16 @@ export default function useDebouncedCallback<
};

return func;
}, [leading, maxing, wait, maxWait, trailing, useRAF]);
}, [
leading,
maxing,
wait,
maxWait,
trailing,
useRAF,
isClientSize,
debounceOnServer,
]);

return debounced;
}

0 comments on commit 3dfaf50

Please sign in to comment.