@jobber/hooks 2.16.0 → 2.17.0
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.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/useDebounce/index.d.ts +1 -0
- package/dist/useDebounce/index.js +5 -0
- package/dist/useDebounce/useDebounce.d.ts +11 -0
- package/dist/useDebounce/useDebounce.js +29 -0
- package/dist/useDebounce/useDebounce.test.d.ts +1 -0
- package/dist/useDebounce/useDebounce.test.js +190 -0
- package/package.json +3 -2
- package/useDebounce.d.ts +1 -0
- package/useDebounce.js +17 -0
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export * from "./useBool";
|
|
|
2
2
|
export * from "./useBreakpoints";
|
|
3
3
|
export * from "./useCallbackRef";
|
|
4
4
|
export * from "./useCollectionQuery";
|
|
5
|
+
export * from "./useDebounce";
|
|
5
6
|
export * from "./useFocusTrap";
|
|
6
7
|
export * from "./useFormState";
|
|
7
8
|
export * from "./useInView";
|
package/dist/index.js
CHANGED
|
@@ -18,6 +18,7 @@ __exportStar(require("./useBool"), exports);
|
|
|
18
18
|
__exportStar(require("./useBreakpoints"), exports);
|
|
19
19
|
__exportStar(require("./useCallbackRef"), exports);
|
|
20
20
|
__exportStar(require("./useCollectionQuery"), exports);
|
|
21
|
+
__exportStar(require("./useDebounce"), exports);
|
|
21
22
|
__exportStar(require("./useFocusTrap"), exports);
|
|
22
23
|
__exportStar(require("./useFormState"), exports);
|
|
23
24
|
__exportStar(require("./useInView"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useDebounce } from "./useDebounce";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDebounce = void 0;
|
|
4
|
+
var useDebounce_1 = require("./useDebounce");
|
|
5
|
+
Object.defineProperty(exports, "useDebounce", { enumerable: true, get: function () { return useDebounce_1.useDebounce; } });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { debounce } from "es-toolkit";
|
|
2
|
+
type AnyFunction = (...args: any[]) => any;
|
|
3
|
+
/**
|
|
4
|
+
* A hook to easily manage debounced functions, including their cleanup.
|
|
5
|
+
* @param func The function to debounce.
|
|
6
|
+
* @param wait The number of milliseconds to delay.
|
|
7
|
+
* @param options Optional debounce settings.
|
|
8
|
+
* @returns The debounced function.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useDebounce<T extends AnyFunction>(func: T, wait: number, options?: Parameters<typeof debounce>[2]): import("es-toolkit").DebouncedFunction<(...args: Parameters<T>) => any>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useDebounce = useDebounce;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const es_toolkit_1 = require("es-toolkit");
|
|
6
|
+
const useCallbackRef_1 = require("../useCallbackRef");
|
|
7
|
+
/**
|
|
8
|
+
* A hook to easily manage debounced functions, including their cleanup.
|
|
9
|
+
* @param func The function to debounce.
|
|
10
|
+
* @param wait The number of milliseconds to delay.
|
|
11
|
+
* @param options Optional debounce settings.
|
|
12
|
+
* @returns The debounced function.
|
|
13
|
+
*/
|
|
14
|
+
function useDebounce(func, wait, options) {
|
|
15
|
+
const funcRef = (0, useCallbackRef_1.useCallbackRef)(func);
|
|
16
|
+
// Note: do not add additional dependencies! There is currently no use case where we would change
|
|
17
|
+
// the wait delay or options between renders. By not tracking as dependencies, this allows
|
|
18
|
+
// consumers of useDebounce to provide a raw object for options rather than forcing them to
|
|
19
|
+
// memoize that param. Same with the func they provide, we're using a ref to keep that up
|
|
20
|
+
// to date and to avoid forcing the consumer to memoize it.
|
|
21
|
+
const debounced = (0, react_1.useMemo)(() => {
|
|
22
|
+
return (0, es_toolkit_1.debounce)((...args) => funcRef(...args), wait, options);
|
|
23
|
+
}, [funcRef]);
|
|
24
|
+
(0, react_1.useEffect)(() => {
|
|
25
|
+
// If the debounced function is recreated (or unmounted), cancel the last call.
|
|
26
|
+
return () => debounced.cancel();
|
|
27
|
+
}, [debounced]);
|
|
28
|
+
return debounced;
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
const react_1 = __importStar(require("react"));
|
|
39
|
+
const react_2 = require("@testing-library/react");
|
|
40
|
+
const user_event_1 = __importDefault(require("@testing-library/user-event"));
|
|
41
|
+
const useDebounce_1 = require("./useDebounce");
|
|
42
|
+
const DEBOUNCE_WAIT = 300;
|
|
43
|
+
describe("useDebounce", () => {
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
jest.useFakeTimers();
|
|
46
|
+
});
|
|
47
|
+
afterEach(() => {
|
|
48
|
+
jest.useRealTimers();
|
|
49
|
+
});
|
|
50
|
+
it("should debounce the function call", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
+
const mockFn = jest.fn();
|
|
52
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
53
|
+
result.current("test");
|
|
54
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
55
|
+
(0, react_2.act)(() => {
|
|
56
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
57
|
+
});
|
|
58
|
+
expect(mockFn).toHaveBeenCalledWith("test");
|
|
59
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
60
|
+
}));
|
|
61
|
+
it("should cancel pending debounced calls on unmount", () => {
|
|
62
|
+
const mockFn = jest.fn();
|
|
63
|
+
const { result, unmount } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
64
|
+
result.current("test");
|
|
65
|
+
unmount();
|
|
66
|
+
(0, react_2.act)(() => {
|
|
67
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
68
|
+
});
|
|
69
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
it("should handle multiple calls within the debounce period", () => {
|
|
72
|
+
const mockFn = jest.fn();
|
|
73
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
74
|
+
result.current("first");
|
|
75
|
+
(0, react_2.act)(() => {
|
|
76
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT / 2);
|
|
77
|
+
});
|
|
78
|
+
result.current("second");
|
|
79
|
+
(0, react_2.act)(() => {
|
|
80
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
81
|
+
});
|
|
82
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
83
|
+
expect(mockFn).toHaveBeenCalledWith("second");
|
|
84
|
+
});
|
|
85
|
+
it("should not recreate debounced function when options object reference changes", () => {
|
|
86
|
+
const mockFn = jest.fn();
|
|
87
|
+
const debounceEgdesOption = ["trailing"];
|
|
88
|
+
// Use a function that returns a new options object each time
|
|
89
|
+
const { result, rerender } = (0, react_2.renderHook)(({ options }) => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT, options), { initialProps: { options: { edges: debounceEgdesOption } } });
|
|
90
|
+
const debounceRef = result.current;
|
|
91
|
+
rerender({ options: { edges: debounceEgdesOption } });
|
|
92
|
+
expect(debounceRef).toBe(result.current);
|
|
93
|
+
});
|
|
94
|
+
it("should not recreate debounced function when options config changes", () => {
|
|
95
|
+
const mockFn = jest.fn();
|
|
96
|
+
// Largely arbitrary, this value x 2 must simply be less than the debounce wait
|
|
97
|
+
const TIME_INCREMENT_LESSER_THAN_DEBOUNCE_WAIT = 1;
|
|
98
|
+
// Start with trailing edge
|
|
99
|
+
const debounceEgdesOption = ["trailing"];
|
|
100
|
+
// Use a function that returns a new options object each time
|
|
101
|
+
const { result, rerender } = (0, react_2.renderHook)(({ options }) => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT, options), { initialProps: { options: { edges: debounceEgdesOption } } });
|
|
102
|
+
result.current("first");
|
|
103
|
+
(0, react_2.act)(() => {
|
|
104
|
+
jest.advanceTimersByTime(TIME_INCREMENT_LESSER_THAN_DEBOUNCE_WAIT);
|
|
105
|
+
});
|
|
106
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
107
|
+
// This means it calls immediately at the leading edge of the timeout.
|
|
108
|
+
rerender({ options: { edges: ["leading"] } });
|
|
109
|
+
result.current("second");
|
|
110
|
+
(0, react_2.act)(() => {
|
|
111
|
+
jest.advanceTimersByTime(TIME_INCREMENT_LESSER_THAN_DEBOUNCE_WAIT);
|
|
112
|
+
});
|
|
113
|
+
// The config change should be ignored, options are hardcoded
|
|
114
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
115
|
+
});
|
|
116
|
+
it("should work with React components", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
|
+
function DebouncedComponent() {
|
|
118
|
+
const [value, setValue] = (0, react_1.useState)("");
|
|
119
|
+
const [debouncedValue, setDebouncedValue] = (0, react_1.useState)("");
|
|
120
|
+
const debouncedSetValue = (0, useDebounce_1.useDebounce)((newValue) => {
|
|
121
|
+
setDebouncedValue(newValue);
|
|
122
|
+
}, DEBOUNCE_WAIT);
|
|
123
|
+
(0, react_1.useEffect)(() => {
|
|
124
|
+
debouncedSetValue(value);
|
|
125
|
+
}, [value, debouncedSetValue]);
|
|
126
|
+
return (react_1.default.createElement("div", null,
|
|
127
|
+
react_1.default.createElement("input", { "data-testid": "input", value: value, onChange: e => setValue(e.target.value) }),
|
|
128
|
+
react_1.default.createElement("div", { "data-testid": "debounced-value" }, debouncedValue)));
|
|
129
|
+
}
|
|
130
|
+
(0, react_2.render)(react_1.default.createElement(DebouncedComponent, null));
|
|
131
|
+
const input = react_2.screen.getByTestId("input");
|
|
132
|
+
const debouncedValue = react_2.screen.getByTestId("debounced-value");
|
|
133
|
+
const user = user_event_1.default.setup({ advanceTimers: jest.advanceTimersByTime });
|
|
134
|
+
yield user.type(input, "test");
|
|
135
|
+
expect(debouncedValue.textContent).toBe("");
|
|
136
|
+
(0, react_2.act)(() => {
|
|
137
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT + 100);
|
|
138
|
+
});
|
|
139
|
+
expect(debouncedValue.textContent).toBe("test");
|
|
140
|
+
}), 10000);
|
|
141
|
+
it("should properly handle options object", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
142
|
+
function DebouncedComponent() {
|
|
143
|
+
const [count, setCount] = (0, react_1.useState)(0);
|
|
144
|
+
const [debouncedCount, setDebouncedCount] = (0, react_1.useState)(0);
|
|
145
|
+
const options = {
|
|
146
|
+
edges: ["leading", "trailing"],
|
|
147
|
+
};
|
|
148
|
+
const debouncedSetCount = (0, useDebounce_1.useDebounce)((value) => {
|
|
149
|
+
setDebouncedCount(value);
|
|
150
|
+
}, DEBOUNCE_WAIT, options);
|
|
151
|
+
return (react_1.default.createElement("div", null,
|
|
152
|
+
react_1.default.createElement("button", { "data-testid": "increment", onClick: () => {
|
|
153
|
+
const newCount = count + 1;
|
|
154
|
+
setCount(newCount);
|
|
155
|
+
debouncedSetCount(newCount);
|
|
156
|
+
}, type: "button" }, "Increment"),
|
|
157
|
+
react_1.default.createElement("div", { "data-testid": "count" }, count),
|
|
158
|
+
react_1.default.createElement("div", { "data-testid": "debounced-count" }, debouncedCount)));
|
|
159
|
+
}
|
|
160
|
+
(0, react_2.render)(react_1.default.createElement(DebouncedComponent, null));
|
|
161
|
+
const incrementButton = react_2.screen.getByTestId("increment");
|
|
162
|
+
const debouncedCount = react_2.screen.getByTestId("debounced-count");
|
|
163
|
+
const user = user_event_1.default.setup({ advanceTimers: jest.advanceTimersByTime });
|
|
164
|
+
yield user.click(incrementButton);
|
|
165
|
+
// With leading edge, the value should be updated immediately
|
|
166
|
+
expect(debouncedCount.textContent).toBe("1");
|
|
167
|
+
yield user.click(incrementButton);
|
|
168
|
+
yield user.click(incrementButton);
|
|
169
|
+
// Additional clicks shouldn't update immediately (debounced)
|
|
170
|
+
expect(debouncedCount.textContent).toBe("1");
|
|
171
|
+
// After the debounce period, the trailing edge should update with the latest value
|
|
172
|
+
(0, react_2.act)(() => {
|
|
173
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
174
|
+
});
|
|
175
|
+
expect(debouncedCount.textContent).toBe("3");
|
|
176
|
+
}));
|
|
177
|
+
it("should abort debounced function when signal is aborted", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
178
|
+
const mockFn = jest.fn();
|
|
179
|
+
const controller = new AbortController();
|
|
180
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT, { signal: controller.signal }));
|
|
181
|
+
result.current("test");
|
|
182
|
+
(0, react_2.act)(() => {
|
|
183
|
+
controller.abort();
|
|
184
|
+
});
|
|
185
|
+
(0, react_2.act)(() => {
|
|
186
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT + 100);
|
|
187
|
+
});
|
|
188
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
189
|
+
}));
|
|
190
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jobber/hooks",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.17.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.js",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"uuid": "^8.3.2"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
+
"es-toolkit": "^1.39.7",
|
|
32
33
|
"lodash": "^4.17.20",
|
|
33
34
|
"resize-observer-polyfill": "^1.5.1",
|
|
34
35
|
"tiny-invariant": "^1.3.3",
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"@apollo/client": "^3.0.0",
|
|
40
41
|
"react": "^18.2.0"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "5b2a570f0d657e82d04a8d60964242f67da3fab0"
|
|
43
44
|
}
|
package/useDebounce.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./dist/useDebounce";
|
package/useDebounce.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true,
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var useDebounce = require("./dist/useDebounce");
|
|
8
|
+
|
|
9
|
+
Object.keys(useDebounce).forEach(function(key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
Object.defineProperty(exports, key, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function get() {
|
|
14
|
+
return useDebounce[key];
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
});
|