@usefy/use-throttle-callback 0.0.17 → 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 +60 -60
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -51,11 +51,11 @@
51
51
 
52
52
  ### Throttle vs Debounce Callbacks
53
53
 
54
- | Feature | Throttle Callback | Debounce Callback |
55
- |---------|-------------------|-------------------|
56
- | First invocation | Immediate (leading: true) | After delay |
57
- | During rapid calls | Regular intervals | Waits for pause |
58
- | Best for | Scroll, resize, mouse move | Search, form validation |
54
+ | Feature | Throttle Callback | Debounce Callback |
55
+ | ------------------ | -------------------------- | ----------------------- |
56
+ | First invocation | Immediate (leading: true) | After delay |
57
+ | During rapid calls | Regular intervals | Waits for pause |
58
+ | Best for | Scroll, resize, mouse move | Search, form validation |
59
59
 
60
60
  ---
61
61
 
@@ -93,18 +93,18 @@ This package uses [@usefy/use-debounce-callback](https://www.npmjs.com/package/@
93
93
  ## Quick Start
94
94
 
95
95
  ```tsx
96
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
96
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
97
97
 
98
98
  function ScrollTracker() {
99
99
  const handleScroll = useThrottleCallback(() => {
100
- console.log('Scroll position:', window.scrollY);
100
+ console.log("Scroll position:", window.scrollY);
101
101
  }, 100);
102
102
 
103
103
  useEffect(() => {
104
- window.addEventListener('scroll', handleScroll);
104
+ window.addEventListener("scroll", handleScroll);
105
105
  return () => {
106
106
  handleScroll.cancel();
107
- window.removeEventListener('scroll', handleScroll);
107
+ window.removeEventListener("scroll", handleScroll);
108
108
  };
109
109
  }, [handleScroll]);
110
110
 
@@ -122,27 +122,27 @@ A hook that returns a throttled version of the provided callback function.
122
122
 
123
123
  #### Parameters
124
124
 
125
- | Parameter | Type | Default | Description |
126
- |-----------|------|---------|-------------|
127
- | `callback` | `T extends (...args: any[]) => any` | — | The callback function to throttle |
128
- | `delay` | `number` | `500` | The throttle interval in milliseconds |
129
- | `options` | `UseThrottleCallbackOptions` | `{}` | Additional configuration options |
125
+ | Parameter | Type | Default | Description |
126
+ | ---------- | ----------------------------------- | ------- | ------------------------------------- |
127
+ | `callback` | `T extends (...args: any[]) => any` | — | The callback function to throttle |
128
+ | `delay` | `number` | `500` | The throttle interval in milliseconds |
129
+ | `options` | `UseThrottleCallbackOptions` | `{}` | Additional configuration options |
130
130
 
131
131
  #### Options
132
132
 
133
- | Option | Type | Default | Description |
134
- |--------|------|---------|-------------|
135
- | `leading` | `boolean` | `true` | Invoke on the leading edge (first call) |
136
- | `trailing` | `boolean` | `true` | Invoke on the trailing edge (after interval) |
133
+ | Option | Type | Default | Description |
134
+ | ---------- | --------- | ------- | -------------------------------------------- |
135
+ | `leading` | `boolean` | `true` | Invoke on the leading edge (first call) |
136
+ | `trailing` | `boolean` | `true` | Invoke on the trailing edge (after interval) |
137
137
 
138
138
  #### Returns `ThrottledFunction<T>`
139
139
 
140
- | Property | Type | Description |
141
- |----------|------|-------------|
140
+ | Property | Type | Description |
141
+ | ----------- | --------------- | --------------------------------------------------- |
142
142
  | `(...args)` | `ReturnType<T>` | The throttled function (same signature as original) |
143
- | `cancel` | `() => void` | Cancels any pending invocation |
144
- | `flush` | `() => void` | Immediately invokes any pending invocation |
145
- | `pending` | `() => boolean` | Returns `true` if there's a pending invocation |
143
+ | `cancel` | `() => void` | Cancels any pending invocation |
144
+ | `flush` | `() => void` | Immediately invokes any pending invocation |
145
+ | `pending` | `() => boolean` | Returns `true` if there's a pending invocation |
146
146
 
147
147
  ---
148
148
 
@@ -151,7 +151,7 @@ A hook that returns a throttled version of the provided callback function.
151
151
  ### Scroll Event Handler
152
152
 
153
153
  ```tsx
154
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
154
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
155
155
 
156
156
  function InfiniteScroll() {
157
157
  const [items, setItems] = useState([]);
@@ -167,16 +167,18 @@ function InfiniteScroll() {
167
167
  }, 200);
168
168
 
169
169
  useEffect(() => {
170
- window.addEventListener('scroll', handleScroll, { passive: true });
170
+ window.addEventListener("scroll", handleScroll, { passive: true });
171
171
  return () => {
172
172
  handleScroll.cancel();
173
- window.removeEventListener('scroll', handleScroll);
173
+ window.removeEventListener("scroll", handleScroll);
174
174
  };
175
175
  }, [handleScroll]);
176
176
 
177
177
  return (
178
178
  <div>
179
- {items.map(item => <ItemCard key={item.id} item={item} />)}
179
+ {items.map((item) => (
180
+ <ItemCard key={item.id} item={item} />
181
+ ))}
180
182
  </div>
181
183
  );
182
184
  }
@@ -185,7 +187,7 @@ function InfiniteScroll() {
185
187
  ### Mouse Movement Tracking
186
188
 
187
189
  ```tsx
188
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
190
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
189
191
 
190
192
  function HeatmapTracker() {
191
193
  const recordPosition = useThrottleCallback((x: number, y: number) => {
@@ -197,10 +199,10 @@ function HeatmapTracker() {
197
199
  recordPosition(e.clientX, e.clientY);
198
200
  };
199
201
 
200
- document.addEventListener('mousemove', handleMouseMove);
202
+ document.addEventListener("mousemove", handleMouseMove);
201
203
  return () => {
202
204
  recordPosition.cancel();
203
- document.removeEventListener('mousemove', handleMouseMove);
205
+ document.removeEventListener("mousemove", handleMouseMove);
204
206
  };
205
207
  }, [recordPosition]);
206
208
 
@@ -211,13 +213,13 @@ function HeatmapTracker() {
211
213
  ### Window Resize Handler
212
214
 
213
215
  ```tsx
214
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
216
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
215
217
 
216
218
  function ResponsiveChart() {
217
219
  const [dimensions, setDimensions] = useState({ width: 800, height: 400 });
218
220
 
219
221
  const handleResize = useThrottleCallback(() => {
220
- const container = document.getElementById('chart-container');
222
+ const container = document.getElementById("chart-container");
221
223
  if (container) {
222
224
  setDimensions({
223
225
  width: container.clientWidth,
@@ -227,10 +229,10 @@ function ResponsiveChart() {
227
229
  }, 150);
228
230
 
229
231
  useEffect(() => {
230
- window.addEventListener('resize', handleResize);
232
+ window.addEventListener("resize", handleResize);
231
233
  return () => {
232
234
  handleResize.cancel();
233
- window.removeEventListener('resize', handleResize);
235
+ window.removeEventListener("resize", handleResize);
234
236
  };
235
237
  }, [handleResize]);
236
238
 
@@ -245,7 +247,7 @@ function ResponsiveChart() {
245
247
  ### Drag Handler with Flush
246
248
 
247
249
  ```tsx
248
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
250
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
249
251
 
250
252
  function Draggable() {
251
253
  const [position, setPosition] = useState({ x: 0, y: 0 });
@@ -280,13 +282,13 @@ function Draggable() {
280
282
  ### Real-time Input Sync
281
283
 
282
284
  ```tsx
283
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
285
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
284
286
 
285
287
  function CollaborativeEditor() {
286
- const [content, setContent] = useState('');
288
+ const [content, setContent] = useState("");
287
289
 
288
290
  const syncToServer = useThrottleCallback((text: string) => {
289
- webSocket.send(JSON.stringify({ type: 'update', content: text }));
291
+ webSocket.send(JSON.stringify({ type: "update", content: text }));
290
292
  }, 200);
291
293
 
292
294
  const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
@@ -315,7 +317,7 @@ function CollaborativeEditor() {
315
317
  ### Leading Edge Only (Immediate Response)
316
318
 
317
319
  ```tsx
318
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
320
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
319
321
 
320
322
  function ButtonWithCooldown() {
321
323
  const [clickCount, setClickCount] = useState(0);
@@ -323,7 +325,7 @@ function ButtonWithCooldown() {
323
325
  // First click is immediate, then 1 second cooldown
324
326
  const handleClick = useThrottleCallback(
325
327
  () => {
326
- setClickCount(c => c + 1);
328
+ setClickCount((c) => c + 1);
327
329
  },
328
330
  1000,
329
331
  { leading: true, trailing: false }
@@ -340,23 +342,21 @@ function ButtonWithCooldown() {
340
342
  ### Pending State Indicator
341
343
 
342
344
  ```tsx
343
- import { useThrottleCallback } from '@usefy/use-throttle-callback';
345
+ import { useThrottleCallback } from "@usefy/use-throttle-callback";
344
346
 
345
347
  function SaveIndicator() {
346
348
  const [data, setData] = useState({});
347
349
 
348
350
  const save = useThrottleCallback((newData: object) => {
349
- fetch('/api/save', {
350
- method: 'POST',
351
+ fetch("/api/save", {
352
+ method: "POST",
351
353
  body: JSON.stringify(newData),
352
354
  });
353
355
  }, 500);
354
356
 
355
357
  return (
356
358
  <div>
357
- <button onClick={() => save(data)}>
358
- Save
359
- </button>
359
+ <button onClick={() => save(data)}>Save</button>
360
360
  {save.pending() && <span className="saving-indicator">Saving...</span>}
361
361
  </div>
362
362
  );
@@ -374,7 +374,7 @@ import {
374
374
  useThrottleCallback,
375
375
  type UseThrottleCallbackOptions,
376
376
  type ThrottledFunction,
377
- } from '@usefy/use-throttle-callback';
377
+ } from "@usefy/use-throttle-callback";
378
378
 
379
379
  // Type inference from callback
380
380
  const throttledFn = useThrottleCallback((x: number, y: number) => {
@@ -395,12 +395,12 @@ This package maintains comprehensive test coverage to ensure reliability and sta
395
395
 
396
396
  ### Test Coverage
397
397
 
398
- | Category | Coverage |
399
- |----------|----------|
398
+ | Category | Coverage |
399
+ | ---------- | ---------- |
400
400
  | Statements | 100% (2/2) |
401
- | Branches | 100% (4/4) |
402
- | Functions | 100% (1/1) |
403
- | Lines | 100% (2/2) |
401
+ | Branches | 100% (4/4) |
402
+ | Functions | 100% (1/1) |
403
+ | Lines | 100% (2/2) |
404
404
 
405
405
  ### Running Tests
406
406
 
@@ -421,14 +421,14 @@ pnpm test --coverage
421
421
 
422
422
  Explore other hooks in the **@usefy** collection:
423
423
 
424
- | Package | Description |
425
- |---------|-------------|
426
- | [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle) | Value throttling |
427
- | [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce) | Value debouncing |
428
- | [@usefy/use-debounce-callback](https://www.npmjs.com/package/@usefy/use-debounce-callback) | Debounced callbacks |
429
- | [@usefy/use-toggle](https://www.npmjs.com/package/@usefy/use-toggle) | Boolean state management |
430
- | [@usefy/use-counter](https://www.npmjs.com/package/@usefy/use-counter) | Counter state management |
431
- | [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where) | Global click detection |
424
+ | Package | Description |
425
+ | ------------------------------------------------------------------------------------------ | ------------------------ |
426
+ | [@usefy/use-throttle](https://www.npmjs.com/package/@usefy/use-throttle) | Value throttling |
427
+ | [@usefy/use-debounce](https://www.npmjs.com/package/@usefy/use-debounce) | Value debouncing |
428
+ | [@usefy/use-debounce-callback](https://www.npmjs.com/package/@usefy/use-debounce-callback) | Debounced callbacks |
429
+ | [@usefy/use-toggle](https://www.npmjs.com/package/@usefy/use-toggle) | Boolean state management |
430
+ | [@usefy/use-counter](https://www.npmjs.com/package/@usefy/use-counter) | Counter state management |
431
+ | [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where) | Global click detection |
432
432
 
433
433
  ---
434
434
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usefy/use-throttle-callback",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "description": "A React hook for throttling callback functions",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "sideEffects": false,
19
19
  "dependencies": {
20
- "@usefy/use-debounce-callback": "0.0.17"
20
+ "@usefy/use-debounce-callback": "0.0.18"
21
21
  },
22
22
  "peerDependencies": {
23
23
  "react": "^18.0.0 || ^19.0.0"