@usefy/use-throttle-callback 0.0.7 → 0.0.8
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.
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/useThrottleCallback.ts"],"sourcesContent":["export {\
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/useThrottleCallback.ts"],"sourcesContent":["export {\n useThrottleCallback,\n type UseThrottleCallbackOptions,\n type ThrottledFunction,\n} from \"./useThrottleCallback\";\n","import {\n useDebounceCallback,\n type DebouncedFunction,\n} from \"@usefy/use-debounce-callback\";\n\n/**\n * Options for useThrottleCallback hook\n */\nexport interface UseThrottleCallbackOptions {\n /**\n * Whether to invoke on the leading edge\n * @default true\n */\n leading?: boolean;\n /**\n * Whether to invoke on the trailing edge\n * @default true\n */\n trailing?: boolean;\n}\n\n/**\n * Throttled function interface with control methods\n */\nexport type ThrottledFunction<T extends (...args: any[]) => any> =\n DebouncedFunction<T>;\n\n/**\n * Creates a throttled version of the provided callback function.\n * The throttled function limits invocations to at most once per specified interval.\n * This is implemented using useDebounceCallback with maxWait set to the interval.\n *\n * @template T - The type of the callback function\n * @param callback - The function to throttle\n * @param delay - The interval in milliseconds (default: 500ms)\n * @param options - Additional options for controlling throttle behavior\n * @returns A throttled version of the callback with cancel, flush, and pending methods\n *\n * @example\n * ```tsx\n * function ScrollTracker() {\n * const throttledScroll = useThrottleCallback(\n * () => {\n * console.log('Scroll position:', window.scrollY);\n * },\n * 100\n * );\n *\n * useEffect(() => {\n * window.addEventListener('scroll', throttledScroll);\n * return () => {\n * throttledScroll.cancel();\n * window.removeEventListener('scroll', throttledScroll);\n * };\n * }, [throttledScroll]);\n *\n * return <div>Scroll to see throttled logs</div>;\n * }\n * ```\n *\n * @example\n * ```tsx\n * // With leading edge only\n * const throttledFn = useThrottleCallback(callback, 300, { leading: true, trailing: false });\n * ```\n *\n * @example\n * ```tsx\n * // With trailing edge only\n * const throttledFn = useThrottleCallback(callback, 300, { leading: false, trailing: true });\n *\n * // Cancel pending invocation\n * throttledFn.cancel();\n *\n * // Immediately invoke pending invocation\n * throttledFn.flush();\n *\n * // Check if there's a pending invocation\n * if (throttledFn.pending()) {\n * console.log('There is a pending call');\n * }\n * ```\n */\nexport function useThrottleCallback<T extends (...args: any[]) => any>(\n callback: T,\n delay: number = 500,\n options: UseThrottleCallbackOptions = {}\n): ThrottledFunction<T> {\n const { leading = true, trailing = true } = options;\n\n // Throttle is implemented using debounce with maxWait set to delay\n return useDebounceCallback(callback, delay, {\n leading,\n maxWait: delay,\n trailing,\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mCAGO;AAgFA,SAAS,oBACd,UACA,QAAgB,KAChB,UAAsC,CAAC,GACjB;AACtB,QAAM,EAAE,UAAU,MAAM,WAAW,KAAK,IAAI;AAG5C,aAAO,kDAAoB,UAAU,OAAO;AAAA,IAC1C;AAAA,IACA,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useThrottleCallback.ts"],"sourcesContent":["import {\
|
|
1
|
+
{"version":3,"sources":["../src/useThrottleCallback.ts"],"sourcesContent":["import {\n useDebounceCallback,\n type DebouncedFunction,\n} from \"@usefy/use-debounce-callback\";\n\n/**\n * Options for useThrottleCallback hook\n */\nexport interface UseThrottleCallbackOptions {\n /**\n * Whether to invoke on the leading edge\n * @default true\n */\n leading?: boolean;\n /**\n * Whether to invoke on the trailing edge\n * @default true\n */\n trailing?: boolean;\n}\n\n/**\n * Throttled function interface with control methods\n */\nexport type ThrottledFunction<T extends (...args: any[]) => any> =\n DebouncedFunction<T>;\n\n/**\n * Creates a throttled version of the provided callback function.\n * The throttled function limits invocations to at most once per specified interval.\n * This is implemented using useDebounceCallback with maxWait set to the interval.\n *\n * @template T - The type of the callback function\n * @param callback - The function to throttle\n * @param delay - The interval in milliseconds (default: 500ms)\n * @param options - Additional options for controlling throttle behavior\n * @returns A throttled version of the callback with cancel, flush, and pending methods\n *\n * @example\n * ```tsx\n * function ScrollTracker() {\n * const throttledScroll = useThrottleCallback(\n * () => {\n * console.log('Scroll position:', window.scrollY);\n * },\n * 100\n * );\n *\n * useEffect(() => {\n * window.addEventListener('scroll', throttledScroll);\n * return () => {\n * throttledScroll.cancel();\n * window.removeEventListener('scroll', throttledScroll);\n * };\n * }, [throttledScroll]);\n *\n * return <div>Scroll to see throttled logs</div>;\n * }\n * ```\n *\n * @example\n * ```tsx\n * // With leading edge only\n * const throttledFn = useThrottleCallback(callback, 300, { leading: true, trailing: false });\n * ```\n *\n * @example\n * ```tsx\n * // With trailing edge only\n * const throttledFn = useThrottleCallback(callback, 300, { leading: false, trailing: true });\n *\n * // Cancel pending invocation\n * throttledFn.cancel();\n *\n * // Immediately invoke pending invocation\n * throttledFn.flush();\n *\n * // Check if there's a pending invocation\n * if (throttledFn.pending()) {\n * console.log('There is a pending call');\n * }\n * ```\n */\nexport function useThrottleCallback<T extends (...args: any[]) => any>(\n callback: T,\n delay: number = 500,\n options: UseThrottleCallbackOptions = {}\n): ThrottledFunction<T> {\n const { leading = true, trailing = true } = options;\n\n // Throttle is implemented using debounce with maxWait set to delay\n return useDebounceCallback(callback, delay, {\n leading,\n maxWait: delay,\n trailing,\n });\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,OAEK;AAgFA,SAAS,oBACd,UACA,QAAgB,KAChB,UAAsC,CAAC,GACjB;AACtB,QAAM,EAAE,UAAU,MAAM,WAAW,KAAK,IAAI;AAG5C,SAAO,oBAAoB,UAAU,OAAO;AAAA,IAC1C;AAAA,IACA,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usefy/use-throttle-callback",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
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.
|
|
20
|
+
"@usefy/use-debounce-callback": "0.0.8"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"react": "^18.0.0 || ^19.0.0"
|