@usefy/use-debounce-callback 0.0.16 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +61 -64
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -81,10 +81,10 @@ This package requires React 18 or 19:
81
81
  ## Quick Start
82
82
 
83
83
  ```tsx
84
- import { useDebounceCallback } from '@usefy/use-debounce-callback';
84
+ import { useDebounceCallback } from "@usefy/use-debounce-callback";
85
85
 
86
86
  function SearchInput() {
87
- const [query, setQuery] = useState('');
87
+ const [query, setQuery] = useState("");
88
88
 
89
89
  const debouncedSearch = useDebounceCallback((searchTerm: string) => {
90
90
  fetchSearchResults(searchTerm);
@@ -116,28 +116,28 @@ A hook that returns a debounced version of the provided callback function.
116
116
 
117
117
  #### Parameters
118
118
 
119
- | Parameter | Type | Default | Description |
120
- |-----------|------|---------|-------------|
121
- | `callback` | `T extends (...args: any[]) => any` | — | The callback function to debounce |
122
- | `delay` | `number` | `500` | The debounce delay in milliseconds |
123
- | `options` | `UseDebounceCallbackOptions` | `{}` | Additional configuration options |
119
+ | Parameter | Type | Default | Description |
120
+ | ---------- | ----------------------------------- | ------- | ---------------------------------- |
121
+ | `callback` | `T extends (...args: any[]) => any` | — | The callback function to debounce |
122
+ | `delay` | `number` | `500` | The debounce delay in milliseconds |
123
+ | `options` | `UseDebounceCallbackOptions` | `{}` | Additional configuration options |
124
124
 
125
125
  #### Options
126
126
 
127
- | Option | Type | Default | Description |
128
- |--------|------|---------|-------------|
129
- | `leading` | `boolean` | `false` | Invoke on the leading edge (first call) |
130
- | `trailing` | `boolean` | `true` | Invoke on the trailing edge (after delay) |
131
- | `maxWait` | `number` | — | Maximum time to wait before forcing invocation |
127
+ | Option | Type | Default | Description |
128
+ | ---------- | --------- | ------- | ---------------------------------------------- |
129
+ | `leading` | `boolean` | `false` | Invoke on the leading edge (first call) |
130
+ | `trailing` | `boolean` | `true` | Invoke on the trailing edge (after delay) |
131
+ | `maxWait` | `number` | — | Maximum time to wait before forcing invocation |
132
132
 
133
133
  #### Returns `DebouncedFunction<T>`
134
134
 
135
- | Property | Type | Description |
136
- |----------|------|-------------|
135
+ | Property | Type | Description |
136
+ | ----------- | --------------- | --------------------------------------------------- |
137
137
  | `(...args)` | `ReturnType<T>` | The debounced function (same signature as original) |
138
- | `cancel` | `() => void` | Cancels any pending invocation |
139
- | `flush` | `() => void` | Immediately invokes any pending invocation |
140
- | `pending` | `() => boolean` | Returns `true` if there's a pending invocation |
138
+ | `cancel` | `() => void` | Cancels any pending invocation |
139
+ | `flush` | `() => void` | Immediately invokes any pending invocation |
140
+ | `pending` | `() => boolean` | Returns `true` if there's a pending invocation |
141
141
 
142
142
  ---
143
143
 
@@ -146,14 +146,14 @@ A hook that returns a debounced version of the provided callback function.
146
146
  ### Auto-Save with Cancel
147
147
 
148
148
  ```tsx
149
- import { useDebounceCallback } from '@usefy/use-debounce-callback';
149
+ import { useDebounceCallback } from "@usefy/use-debounce-callback";
150
150
 
151
151
  function Editor() {
152
- const [content, setContent] = useState('');
152
+ const [content, setContent] = useState("");
153
153
 
154
154
  const debouncedSave = useDebounceCallback((text: string) => {
155
155
  saveToServer(text);
156
- console.log('Auto-saved');
156
+ console.log("Auto-saved");
157
157
  }, 1000);
158
158
 
159
159
  const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
@@ -169,7 +169,7 @@ function Editor() {
169
169
  const handleDiscard = () => {
170
170
  // Cancel pending save and reset content
171
171
  debouncedSave.cancel();
172
- setContent('');
172
+ setContent("");
173
173
  };
174
174
 
175
175
  return (
@@ -186,7 +186,7 @@ function Editor() {
186
186
  ### Search with Immediate First Call
187
187
 
188
188
  ```tsx
189
- import { useDebounceCallback } from '@usefy/use-debounce-callback';
189
+ import { useDebounceCallback } from "@usefy/use-debounce-callback";
190
190
 
191
191
  function SearchWithSuggestions() {
192
192
  const [results, setResults] = useState([]);
@@ -214,25 +214,25 @@ function SearchWithSuggestions() {
214
214
  ### Form Validation
215
215
 
216
216
  ```tsx
217
- import { useDebounceCallback } from '@usefy/use-debounce-callback';
217
+ import { useDebounceCallback } from "@usefy/use-debounce-callback";
218
218
 
219
219
  function RegistrationForm() {
220
- const [email, setEmail] = useState('');
221
- const [error, setError] = useState('');
220
+ const [email, setEmail] = useState("");
221
+ const [error, setError] = useState("");
222
222
 
223
223
  const validateEmail = useDebounceCallback(async (value: string) => {
224
- if (!value.includes('@')) {
225
- setError('Invalid email format');
224
+ if (!value.includes("@")) {
225
+ setError("Invalid email format");
226
226
  return;
227
227
  }
228
228
  const response = await fetch(`/api/check-email?e=${value}`);
229
229
  const { available } = await response.json();
230
- setError(available ? '' : 'Email already registered');
230
+ setError(available ? "" : "Email already registered");
231
231
  }, 500);
232
232
 
233
233
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
234
234
  setEmail(e.target.value);
235
- setError(''); // Clear error immediately
235
+ setError(""); // Clear error immediately
236
236
  validateEmail(e.target.value);
237
237
  };
238
238
 
@@ -253,7 +253,7 @@ function RegistrationForm() {
253
253
  ### Event Handler with maxWait
254
254
 
255
255
  ```tsx
256
- import { useDebounceCallback } from '@usefy/use-debounce-callback';
256
+ import { useDebounceCallback } from "@usefy/use-debounce-callback";
257
257
 
258
258
  function ResizeHandler() {
259
259
  const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
@@ -271,10 +271,10 @@ function ResizeHandler() {
271
271
  );
272
272
 
273
273
  useEffect(() => {
274
- window.addEventListener('resize', handleResize);
274
+ window.addEventListener("resize", handleResize);
275
275
  return () => {
276
276
  handleResize.cancel();
277
- window.removeEventListener('resize', handleResize);
277
+ window.removeEventListener("resize", handleResize);
278
278
  };
279
279
  }, [handleResize]);
280
280
 
@@ -289,32 +289,29 @@ function ResizeHandler() {
289
289
  ### API Request with Pending State
290
290
 
291
291
  ```tsx
292
- import { useDebounceCallback } from '@usefy/use-debounce-callback';
292
+ import { useDebounceCallback } from "@usefy/use-debounce-callback";
293
293
 
294
294
  function DataFetcher() {
295
295
  const [data, setData] = useState(null);
296
296
  const [loading, setLoading] = useState(false);
297
297
 
298
- const fetchData = useDebounceCallback(
299
- async (params: QueryParams) => {
300
- setLoading(true);
301
- try {
302
- const response = await fetch('/api/data', {
303
- method: 'POST',
304
- body: JSON.stringify(params),
305
- });
306
- setData(await response.json());
307
- } finally {
308
- setLoading(false);
309
- }
310
- },
311
- 500
312
- );
298
+ const fetchData = useDebounceCallback(async (params: QueryParams) => {
299
+ setLoading(true);
300
+ try {
301
+ const response = await fetch("/api/data", {
302
+ method: "POST",
303
+ body: JSON.stringify(params),
304
+ });
305
+ setData(await response.json());
306
+ } finally {
307
+ setLoading(false);
308
+ }
309
+ }, 500);
313
310
 
314
311
  return (
315
312
  <div>
316
313
  <button onClick={() => fetchData({ page: 1 })}>
317
- {fetchData.pending() ? 'Request pending...' : 'Fetch Data'}
314
+ {fetchData.pending() ? "Request pending..." : "Fetch Data"}
318
315
  </button>
319
316
  {loading && <Spinner />}
320
317
  </div>
@@ -325,7 +322,7 @@ function DataFetcher() {
325
322
  ### Cleanup on Unmount
326
323
 
327
324
  ```tsx
328
- import { useDebounceCallback } from '@usefy/use-debounce-callback';
325
+ import { useDebounceCallback } from "@usefy/use-debounce-callback";
329
326
 
330
327
  function Component() {
331
328
  const debouncedAction = useDebounceCallback(() => {
@@ -354,7 +351,7 @@ import {
354
351
  useDebounceCallback,
355
352
  type UseDebounceCallbackOptions,
356
353
  type DebouncedFunction,
357
- } from '@usefy/use-debounce-callback';
354
+ } from "@usefy/use-debounce-callback";
358
355
 
359
356
  // Type inference from callback
360
357
  const debouncedFn = useDebounceCallback((a: string, b: number) => {
@@ -375,12 +372,12 @@ This package maintains comprehensive test coverage to ensure reliability and sta
375
372
 
376
373
  ### Test Coverage
377
374
 
378
- | Category | Coverage |
379
- |----------|----------|
375
+ | Category | Coverage |
376
+ | ---------- | --------------- |
380
377
  | Statements | 94.11% (96/102) |
381
- | Branches | 82.6% (38/46) |
382
- | Functions | 93.75% (15/16) |
383
- | Lines | 94.05% (95/101) |
378
+ | Branches | 82.6% (38/46) |
379
+ | Functions | 93.75% (15/16) |
380
+ | Lines | 94.05% (95/101) |
384
381
 
385
382
  ### Test Categories
386
383
 
@@ -425,14 +422,14 @@ pnpm test --coverage
425
422
 
426
423
  Explore other hooks in the **@usefy** collection:
427
424
 
428
- | Package | Description |
429
- |---------|-------------|
430
- | [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce) | Value debouncing |
431
- | [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle) | Value throttling |
432
- | [@usefy/use-throttle-callback](https://www.npmjs.com/package/@usefy/use-throttle-callback) | Throttled callbacks |
433
- | [@usefy/use-toggle](https://www.npmjs.com/package/@usefy/use-toggle) | Boolean state management |
434
- | [@usefy/use-counter](https://www.npmjs.com/package/@usefy/use-counter) | Counter state management |
435
- | [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where) | Global click detection |
425
+ | Package | Description |
426
+ | ------------------------------------------------------------------------------------------ | ------------------------ |
427
+ | [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce) | Value debouncing |
428
+ | [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle) | Value throttling |
429
+ | [@usefy/use-throttle-callback](https://www.npmjs.com/package/@usefy/use-throttle-callback) | Throttled callbacks |
430
+ | [@usefy/use-toggle](https://www.npmjs.com/package/@usefy/use-toggle) | Boolean state management |
431
+ | [@usefy/use-counter](https://www.npmjs.com/package/@usefy/use-counter) | Counter state management |
432
+ | [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where) | Global click detection |
436
433
 
437
434
  ---
438
435
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usefy/use-debounce-callback",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "A React hook for debouncing callback functions",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",