@kopexa/use-debounced-callback 0.0.0-canary-20250721165623

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/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @kopexa/use-debounced-callback
2
+
3
+ A Quick description of the component
4
+
5
+ > This is an internal utility, not intended for public usage.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ yarn add @kopexa/use-debounced-callback
11
+ # or
12
+ npm i @kopexa/use-debounced-callback
13
+ ```
14
+
15
+ ## Contribution
16
+
17
+ Yes please! See the
18
+ [contributing guidelines](https://github.com/kopexa-grc/sight/blob/master/CONTRIBUTING.md)
19
+ for details.
20
+
21
+ ## License
22
+
23
+ This project is licensed under the terms of the
24
+ [MIT license](https://github.com/kopexa-grc/sight/blob/master/LICENSE).
@@ -0,0 +1,62 @@
1
+ /** Configuration options for controlling the behavior of the debounced function. */
2
+ type DebounceOptions = {
3
+ /**
4
+ * Determines whether the function should be invoked on the leading edge of the timeout.
5
+ * @default false
6
+ */
7
+ leading?: boolean;
8
+ /**
9
+ * Determines whether the function should be invoked on the trailing edge of the timeout.
10
+ * @default false
11
+ */
12
+ trailing?: boolean;
13
+ /**
14
+ * The maximum time the specified function is allowed to be delayed before it is invoked.
15
+ */
16
+ maxWait?: number;
17
+ };
18
+ /** Functions to manage a debounced callback. */
19
+ type ControlFunctions = {
20
+ /** Cancels pending function invocations. */
21
+ cancel: () => void;
22
+ /** Immediately invokes pending function invocations. */
23
+ flush: () => void;
24
+ /**
25
+ * Checks if there are any pending function invocations.
26
+ * @returns `true` if there are pending invocations, otherwise `false`.
27
+ */
28
+ isPending: () => boolean;
29
+ };
30
+ /**
31
+ * Represents the state and control functions of a debounced callback.
32
+ * Subsequent calls to the debounced function return the result of the last invocation.
33
+ * Note: If there are no previous invocations, the result will be undefined.
34
+ * Ensure proper handling in your code.
35
+ */
36
+ type DebouncedState<T extends (...args: any) => ReturnType<T>> = ((...args: Parameters<T>) => ReturnType<T> | undefined) & ControlFunctions;
37
+ /**
38
+ * Custom hook that creates a debounced version of a callback function.
39
+ * @template T - Type of the original callback function.
40
+ * @param {T} func - The callback function to be debounced.
41
+ * @param {number} delay - The delay in milliseconds before the callback is invoked (default is `500` milliseconds).
42
+ * @param {DebounceOptions} [options] - Options to control the behavior of the debounced function.
43
+ * @returns {DebouncedState<T>} A debounced version of the original callback along with control functions.
44
+ * @public
45
+ * @see [Documentation](https://usehooks-ts.com/react-hook/use-debounce-callback)
46
+ * @example
47
+ * ```tsx
48
+ * const debouncedCallback = useDebounceCallback(
49
+ * (searchTerm) => {
50
+ * // Perform search after user stops typing for 500 milliseconds
51
+ * searchApi(searchTerm);
52
+ * },
53
+ * 500
54
+ * );
55
+ *
56
+ * // Later in the component
57
+ * debouncedCallback('react hooks'); // Will invoke the callback after 500 milliseconds of inactivity.
58
+ * ```
59
+ */
60
+ declare function useDebounceCallback<T extends (...args: any) => ReturnType<T>>(func: T, delay?: number, options?: DebounceOptions): DebouncedState<T>;
61
+
62
+ export { type DebouncedState, useDebounceCallback };
@@ -0,0 +1,62 @@
1
+ /** Configuration options for controlling the behavior of the debounced function. */
2
+ type DebounceOptions = {
3
+ /**
4
+ * Determines whether the function should be invoked on the leading edge of the timeout.
5
+ * @default false
6
+ */
7
+ leading?: boolean;
8
+ /**
9
+ * Determines whether the function should be invoked on the trailing edge of the timeout.
10
+ * @default false
11
+ */
12
+ trailing?: boolean;
13
+ /**
14
+ * The maximum time the specified function is allowed to be delayed before it is invoked.
15
+ */
16
+ maxWait?: number;
17
+ };
18
+ /** Functions to manage a debounced callback. */
19
+ type ControlFunctions = {
20
+ /** Cancels pending function invocations. */
21
+ cancel: () => void;
22
+ /** Immediately invokes pending function invocations. */
23
+ flush: () => void;
24
+ /**
25
+ * Checks if there are any pending function invocations.
26
+ * @returns `true` if there are pending invocations, otherwise `false`.
27
+ */
28
+ isPending: () => boolean;
29
+ };
30
+ /**
31
+ * Represents the state and control functions of a debounced callback.
32
+ * Subsequent calls to the debounced function return the result of the last invocation.
33
+ * Note: If there are no previous invocations, the result will be undefined.
34
+ * Ensure proper handling in your code.
35
+ */
36
+ type DebouncedState<T extends (...args: any) => ReturnType<T>> = ((...args: Parameters<T>) => ReturnType<T> | undefined) & ControlFunctions;
37
+ /**
38
+ * Custom hook that creates a debounced version of a callback function.
39
+ * @template T - Type of the original callback function.
40
+ * @param {T} func - The callback function to be debounced.
41
+ * @param {number} delay - The delay in milliseconds before the callback is invoked (default is `500` milliseconds).
42
+ * @param {DebounceOptions} [options] - Options to control the behavior of the debounced function.
43
+ * @returns {DebouncedState<T>} A debounced version of the original callback along with control functions.
44
+ * @public
45
+ * @see [Documentation](https://usehooks-ts.com/react-hook/use-debounce-callback)
46
+ * @example
47
+ * ```tsx
48
+ * const debouncedCallback = useDebounceCallback(
49
+ * (searchTerm) => {
50
+ * // Perform search after user stops typing for 500 milliseconds
51
+ * searchApi(searchTerm);
52
+ * },
53
+ * 500
54
+ * );
55
+ *
56
+ * // Later in the component
57
+ * debouncedCallback('react hooks'); // Will invoke the callback after 500 milliseconds of inactivity.
58
+ * ```
59
+ */
60
+ declare function useDebounceCallback<T extends (...args: any) => ReturnType<T>>(func: T, delay?: number, options?: DebounceOptions): DebouncedState<T>;
61
+
62
+ export { type DebouncedState, useDebounceCallback };
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ useDebounceCallback: () => useDebounceCallback
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_use_unmount = require("@kopexa/use-unmount");
37
+ var import_lodash = __toESM(require("lodash.debounce"));
38
+ var import_react = require("react");
39
+ function useDebounceCallback(func, delay = 500, options) {
40
+ const debouncedFunc = (0, import_react.useRef)(null);
41
+ (0, import_use_unmount.useUnmount)(() => {
42
+ if (debouncedFunc.current) {
43
+ debouncedFunc.current.cancel();
44
+ }
45
+ });
46
+ const debounced = (0, import_react.useMemo)(() => {
47
+ const debouncedFuncInstance = (0, import_lodash.default)(func, delay, options);
48
+ const wrappedFunc = (...args) => {
49
+ return debouncedFuncInstance(...args);
50
+ };
51
+ wrappedFunc.cancel = () => {
52
+ debouncedFuncInstance.cancel();
53
+ };
54
+ wrappedFunc.isPending = () => {
55
+ return !!debouncedFunc.current;
56
+ };
57
+ wrappedFunc.flush = () => {
58
+ return debouncedFuncInstance.flush();
59
+ };
60
+ return wrappedFunc;
61
+ }, [func, delay, options]);
62
+ (0, import_react.useEffect)(() => {
63
+ debouncedFunc.current = (0, import_lodash.default)(func, delay, options);
64
+ }, [func, delay, options]);
65
+ return debounced;
66
+ }
67
+ // Annotate the CommonJS export names for ESM import in node:
68
+ 0 && (module.exports = {
69
+ useDebounceCallback
70
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,35 @@
1
+ // src/index.ts
2
+ import { useUnmount } from "@kopexa/use-unmount";
3
+ import debounce from "lodash.debounce";
4
+ import { useEffect, useMemo, useRef } from "react";
5
+ function useDebounceCallback(func, delay = 500, options) {
6
+ const debouncedFunc = useRef(null);
7
+ useUnmount(() => {
8
+ if (debouncedFunc.current) {
9
+ debouncedFunc.current.cancel();
10
+ }
11
+ });
12
+ const debounced = useMemo(() => {
13
+ const debouncedFuncInstance = debounce(func, delay, options);
14
+ const wrappedFunc = (...args) => {
15
+ return debouncedFuncInstance(...args);
16
+ };
17
+ wrappedFunc.cancel = () => {
18
+ debouncedFuncInstance.cancel();
19
+ };
20
+ wrappedFunc.isPending = () => {
21
+ return !!debouncedFunc.current;
22
+ };
23
+ wrappedFunc.flush = () => {
24
+ return debouncedFuncInstance.flush();
25
+ };
26
+ return wrappedFunc;
27
+ }, [func, delay, options]);
28
+ useEffect(() => {
29
+ debouncedFunc.current = debounce(func, delay, options);
30
+ }, [func, delay, options]);
31
+ return debounced;
32
+ }
33
+ export {
34
+ useDebounceCallback
35
+ };
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@kopexa/use-debounced-callback",
3
+ "version": "0.0.0-canary-20250721165623",
4
+ "description": "",
5
+ "keywords": [
6
+ "use-debounced-callback"
7
+ ],
8
+ "author": "Kopexa <hello@kopexa.com>",
9
+ "homepage": "https://kopexa.com",
10
+ "license": "MIT",
11
+ "main": "dist/index.js",
12
+ "sideEffects": false,
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/kopexa-grc/sight.git",
22
+ "directory": "packages/hooks/use-debounced-callback"
23
+ },
24
+ "bugs": {
25
+ "url": "https://github.com/kopexa-grc/sight/issues"
26
+ },
27
+ "peerDependencies": {
28
+ "react": ">=19.0.0-rc.0"
29
+ },
30
+ "dependencies": {
31
+ "lodash.debounce": "^4.0.8",
32
+ "@kopexa/use-unmount": "0.0.0-canary-20250721165623"
33
+ },
34
+ "clean-package": "../../../clean-package.config.json",
35
+ "tsup": {
36
+ "clean": true,
37
+ "target": "es2019",
38
+ "format": [
39
+ "cjs",
40
+ "esm"
41
+ ]
42
+ },
43
+ "module": "dist/index.mjs",
44
+ "types": "dist/index.d.ts",
45
+ "exports": {
46
+ ".": {
47
+ "types": "./dist/index.d.ts",
48
+ "import": "./dist/index.mjs",
49
+ "require": "./dist/index.js"
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "scripts": {
54
+ "build": "tsup src --dts",
55
+ "build:fast": "tsup src",
56
+ "dev": "pnpm build:fast --watch",
57
+ "clean": "rimraf dist .turbo",
58
+ "typecheck": "tsc --noEmit"
59
+ }
60
+ }