@usefy/use-throttle-callback 0.0.2
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.d.mts +80 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +16 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +61 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { DebouncedFunction } from '@usefy/use-debounce-callback';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options for useThrottleCallback hook
|
|
5
|
+
*/
|
|
6
|
+
interface UseThrottleCallbackOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Whether to invoke on the leading edge
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
leading?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to invoke on the trailing edge
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
trailing?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Throttled function interface with control methods
|
|
20
|
+
*/
|
|
21
|
+
type ThrottledFunction<T extends (...args: any[]) => any> = DebouncedFunction<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a throttled version of the provided callback function.
|
|
24
|
+
* The throttled function limits invocations to at most once per specified interval.
|
|
25
|
+
* This is implemented using useDebounceCallback with maxWait set to the interval.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The type of the callback function
|
|
28
|
+
* @param callback - The function to throttle
|
|
29
|
+
* @param delay - The interval in milliseconds (default: 500ms)
|
|
30
|
+
* @param options - Additional options for controlling throttle behavior
|
|
31
|
+
* @returns A throttled version of the callback with cancel, flush, and pending methods
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* function ScrollTracker() {
|
|
36
|
+
* const throttledScroll = useThrottleCallback(
|
|
37
|
+
* () => {
|
|
38
|
+
* console.log('Scroll position:', window.scrollY);
|
|
39
|
+
* },
|
|
40
|
+
* 100
|
|
41
|
+
* );
|
|
42
|
+
*
|
|
43
|
+
* useEffect(() => {
|
|
44
|
+
* window.addEventListener('scroll', throttledScroll);
|
|
45
|
+
* return () => {
|
|
46
|
+
* throttledScroll.cancel();
|
|
47
|
+
* window.removeEventListener('scroll', throttledScroll);
|
|
48
|
+
* };
|
|
49
|
+
* }, [throttledScroll]);
|
|
50
|
+
*
|
|
51
|
+
* return <div>Scroll to see throttled logs</div>;
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* // With leading edge only
|
|
58
|
+
* const throttledFn = useThrottleCallback(callback, 300, { leading: true, trailing: false });
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* // With trailing edge only
|
|
64
|
+
* const throttledFn = useThrottleCallback(callback, 300, { leading: false, trailing: true });
|
|
65
|
+
*
|
|
66
|
+
* // Cancel pending invocation
|
|
67
|
+
* throttledFn.cancel();
|
|
68
|
+
*
|
|
69
|
+
* // Immediately invoke pending invocation
|
|
70
|
+
* throttledFn.flush();
|
|
71
|
+
*
|
|
72
|
+
* // Check if there's a pending invocation
|
|
73
|
+
* if (throttledFn.pending()) {
|
|
74
|
+
* console.log('There is a pending call');
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
declare function useThrottleCallback<T extends (...args: any[]) => any>(callback: T, delay?: number, options?: UseThrottleCallbackOptions): ThrottledFunction<T>;
|
|
79
|
+
|
|
80
|
+
export { type ThrottledFunction, type UseThrottleCallbackOptions, useThrottleCallback };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { DebouncedFunction } from '@usefy/use-debounce-callback';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options for useThrottleCallback hook
|
|
5
|
+
*/
|
|
6
|
+
interface UseThrottleCallbackOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Whether to invoke on the leading edge
|
|
9
|
+
* @default true
|
|
10
|
+
*/
|
|
11
|
+
leading?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to invoke on the trailing edge
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
trailing?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Throttled function interface with control methods
|
|
20
|
+
*/
|
|
21
|
+
type ThrottledFunction<T extends (...args: any[]) => any> = DebouncedFunction<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a throttled version of the provided callback function.
|
|
24
|
+
* The throttled function limits invocations to at most once per specified interval.
|
|
25
|
+
* This is implemented using useDebounceCallback with maxWait set to the interval.
|
|
26
|
+
*
|
|
27
|
+
* @template T - The type of the callback function
|
|
28
|
+
* @param callback - The function to throttle
|
|
29
|
+
* @param delay - The interval in milliseconds (default: 500ms)
|
|
30
|
+
* @param options - Additional options for controlling throttle behavior
|
|
31
|
+
* @returns A throttled version of the callback with cancel, flush, and pending methods
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* function ScrollTracker() {
|
|
36
|
+
* const throttledScroll = useThrottleCallback(
|
|
37
|
+
* () => {
|
|
38
|
+
* console.log('Scroll position:', window.scrollY);
|
|
39
|
+
* },
|
|
40
|
+
* 100
|
|
41
|
+
* );
|
|
42
|
+
*
|
|
43
|
+
* useEffect(() => {
|
|
44
|
+
* window.addEventListener('scroll', throttledScroll);
|
|
45
|
+
* return () => {
|
|
46
|
+
* throttledScroll.cancel();
|
|
47
|
+
* window.removeEventListener('scroll', throttledScroll);
|
|
48
|
+
* };
|
|
49
|
+
* }, [throttledScroll]);
|
|
50
|
+
*
|
|
51
|
+
* return <div>Scroll to see throttled logs</div>;
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```tsx
|
|
57
|
+
* // With leading edge only
|
|
58
|
+
* const throttledFn = useThrottleCallback(callback, 300, { leading: true, trailing: false });
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* // With trailing edge only
|
|
64
|
+
* const throttledFn = useThrottleCallback(callback, 300, { leading: false, trailing: true });
|
|
65
|
+
*
|
|
66
|
+
* // Cancel pending invocation
|
|
67
|
+
* throttledFn.cancel();
|
|
68
|
+
*
|
|
69
|
+
* // Immediately invoke pending invocation
|
|
70
|
+
* throttledFn.flush();
|
|
71
|
+
*
|
|
72
|
+
* // Check if there's a pending invocation
|
|
73
|
+
* if (throttledFn.pending()) {
|
|
74
|
+
* console.log('There is a pending call');
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
declare function useThrottleCallback<T extends (...args: any[]) => any>(callback: T, delay?: number, options?: UseThrottleCallbackOptions): ThrottledFunction<T>;
|
|
79
|
+
|
|
80
|
+
export { type ThrottledFunction, type UseThrottleCallbackOptions, useThrottleCallback };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useThrottleCallback: () => useThrottleCallback
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/useThrottleCallback.ts
|
|
28
|
+
var import_use_debounce_callback = require("@usefy/use-debounce-callback");
|
|
29
|
+
function useThrottleCallback(callback, delay = 500, options = {}) {
|
|
30
|
+
const { leading = true, trailing = true } = options;
|
|
31
|
+
return (0, import_use_debounce_callback.useDebounceCallback)(callback, delay, {
|
|
32
|
+
leading,
|
|
33
|
+
maxWait: delay,
|
|
34
|
+
trailing
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
useThrottleCallback
|
|
40
|
+
});
|
|
41
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/useThrottleCallback.ts"],"sourcesContent":["export {\r\n useThrottleCallback,\r\n type UseThrottleCallbackOptions,\r\n type ThrottledFunction,\r\n} from \"./useThrottleCallback\";\r\n","import {\r\n useDebounceCallback,\r\n type DebouncedFunction,\r\n} from \"@usefy/use-debounce-callback\";\r\n\r\n/**\r\n * Options for useThrottleCallback hook\r\n */\r\nexport interface UseThrottleCallbackOptions {\r\n /**\r\n * Whether to invoke on the leading edge\r\n * @default true\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to invoke on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Throttled function interface with control methods\r\n */\r\nexport type ThrottledFunction<T extends (...args: any[]) => any> =\r\n DebouncedFunction<T>;\r\n\r\n/**\r\n * Creates a throttled version of the provided callback function.\r\n * The throttled function limits invocations to at most once per specified interval.\r\n * This is implemented using useDebounceCallback with maxWait set to the interval.\r\n *\r\n * @template T - The type of the callback function\r\n * @param callback - The function to throttle\r\n * @param delay - The interval in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling throttle behavior\r\n * @returns A throttled version of the callback with cancel, flush, and pending methods\r\n *\r\n * @example\r\n * ```tsx\r\n * function ScrollTracker() {\r\n * const throttledScroll = useThrottleCallback(\r\n * () => {\r\n * console.log('Scroll position:', window.scrollY);\r\n * },\r\n * 100\r\n * );\r\n *\r\n * useEffect(() => {\r\n * window.addEventListener('scroll', throttledScroll);\r\n * return () => {\r\n * throttledScroll.cancel();\r\n * window.removeEventListener('scroll', throttledScroll);\r\n * };\r\n * }, [throttledScroll]);\r\n *\r\n * return <div>Scroll to see throttled logs</div>;\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge only\r\n * const throttledFn = useThrottleCallback(callback, 300, { leading: true, trailing: false });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With trailing edge only\r\n * const throttledFn = useThrottleCallback(callback, 300, { leading: false, trailing: true });\r\n *\r\n * // Cancel pending invocation\r\n * throttledFn.cancel();\r\n *\r\n * // Immediately invoke pending invocation\r\n * throttledFn.flush();\r\n *\r\n * // Check if there's a pending invocation\r\n * if (throttledFn.pending()) {\r\n * console.log('There is a pending call');\r\n * }\r\n * ```\r\n */\r\nexport function useThrottleCallback<T extends (...args: any[]) => any>(\r\n callback: T,\r\n delay: number = 500,\r\n options: UseThrottleCallbackOptions = {}\r\n): ThrottledFunction<T> {\r\n const { leading = true, trailing = true } = options;\r\n\r\n // Throttle is implemented using debounce with maxWait set to delay\r\n return useDebounceCallback(callback, delay, {\r\n leading,\r\n maxWait: delay,\r\n trailing,\r\n });\r\n}\r\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
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/useThrottleCallback.ts
|
|
2
|
+
import {
|
|
3
|
+
useDebounceCallback
|
|
4
|
+
} from "@usefy/use-debounce-callback";
|
|
5
|
+
function useThrottleCallback(callback, delay = 500, options = {}) {
|
|
6
|
+
const { leading = true, trailing = true } = options;
|
|
7
|
+
return useDebounceCallback(callback, delay, {
|
|
8
|
+
leading,
|
|
9
|
+
maxWait: delay,
|
|
10
|
+
trailing
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
useThrottleCallback
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useThrottleCallback.ts"],"sourcesContent":["import {\r\n useDebounceCallback,\r\n type DebouncedFunction,\r\n} from \"@usefy/use-debounce-callback\";\r\n\r\n/**\r\n * Options for useThrottleCallback hook\r\n */\r\nexport interface UseThrottleCallbackOptions {\r\n /**\r\n * Whether to invoke on the leading edge\r\n * @default true\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to invoke on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Throttled function interface with control methods\r\n */\r\nexport type ThrottledFunction<T extends (...args: any[]) => any> =\r\n DebouncedFunction<T>;\r\n\r\n/**\r\n * Creates a throttled version of the provided callback function.\r\n * The throttled function limits invocations to at most once per specified interval.\r\n * This is implemented using useDebounceCallback with maxWait set to the interval.\r\n *\r\n * @template T - The type of the callback function\r\n * @param callback - The function to throttle\r\n * @param delay - The interval in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling throttle behavior\r\n * @returns A throttled version of the callback with cancel, flush, and pending methods\r\n *\r\n * @example\r\n * ```tsx\r\n * function ScrollTracker() {\r\n * const throttledScroll = useThrottleCallback(\r\n * () => {\r\n * console.log('Scroll position:', window.scrollY);\r\n * },\r\n * 100\r\n * );\r\n *\r\n * useEffect(() => {\r\n * window.addEventListener('scroll', throttledScroll);\r\n * return () => {\r\n * throttledScroll.cancel();\r\n * window.removeEventListener('scroll', throttledScroll);\r\n * };\r\n * }, [throttledScroll]);\r\n *\r\n * return <div>Scroll to see throttled logs</div>;\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge only\r\n * const throttledFn = useThrottleCallback(callback, 300, { leading: true, trailing: false });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With trailing edge only\r\n * const throttledFn = useThrottleCallback(callback, 300, { leading: false, trailing: true });\r\n *\r\n * // Cancel pending invocation\r\n * throttledFn.cancel();\r\n *\r\n * // Immediately invoke pending invocation\r\n * throttledFn.flush();\r\n *\r\n * // Check if there's a pending invocation\r\n * if (throttledFn.pending()) {\r\n * console.log('There is a pending call');\r\n * }\r\n * ```\r\n */\r\nexport function useThrottleCallback<T extends (...args: any[]) => any>(\r\n callback: T,\r\n delay: number = 500,\r\n options: UseThrottleCallbackOptions = {}\r\n): ThrottledFunction<T> {\r\n const { leading = true, trailing = true } = options;\r\n\r\n // Throttle is implemented using debounce with maxWait set to delay\r\n return useDebounceCallback(callback, delay, {\r\n leading,\r\n maxWait: delay,\r\n trailing,\r\n });\r\n}\r\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
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@usefy/use-throttle-callback",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "A React hook for throttling callback functions",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@usefy/use-debounce-callback": "0.0.2"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
27
|
+
"@testing-library/react": "^16.3.1",
|
|
28
|
+
"@testing-library/user-event": "^14.6.1",
|
|
29
|
+
"@types/react": "^19.0.0",
|
|
30
|
+
"jsdom": "^27.3.0",
|
|
31
|
+
"react": "^19.0.0",
|
|
32
|
+
"rimraf": "^6.0.1",
|
|
33
|
+
"tsup": "^8.0.0",
|
|
34
|
+
"typescript": "^5.0.0",
|
|
35
|
+
"vitest": "^4.0.16"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/geon0529/usefy.git",
|
|
43
|
+
"directory": "packages/use-throttle-callback"
|
|
44
|
+
},
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"keywords": [
|
|
47
|
+
"react",
|
|
48
|
+
"hooks",
|
|
49
|
+
"throttle",
|
|
50
|
+
"callback",
|
|
51
|
+
"rate-limit"
|
|
52
|
+
],
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"dev": "tsup --watch",
|
|
56
|
+
"test": "vitest run",
|
|
57
|
+
"test:watch": "vitest",
|
|
58
|
+
"typecheck": "tsc --noEmit",
|
|
59
|
+
"clean": "rimraf dist"
|
|
60
|
+
}
|
|
61
|
+
}
|