@jobber/hooks 2.14.0 → 2.15.1-export-use-7a742d1.85
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 +0 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/useDebounce/index.d.ts +1 -0
- package/dist/useDebounce/index.js +17 -0
- package/dist/useDebounce/useDebounce.d.ts +11 -0
- package/dist/useDebounce/useDebounce.js +37 -0
- package/dist/useDebounce/useDebounce.test.d.ts +1 -0
- package/dist/useDebounce/useDebounce.test.js +111 -0
- package/package.json +2 -2
- package/useDebounce.d.ts +1 -0
- package/{useAssert.js → useDebounce.js} +3 -3
- package/dist/useAssert/index.d.ts +0 -1
- package/dist/useAssert/index.js +0 -5
- package/dist/useAssert/useAssert.d.ts +0 -5
- package/dist/useAssert/useAssert.js +0 -18
- package/useAssert.d.ts +0 -1
package/README.md
CHANGED
|
@@ -4,7 +4,6 @@ Shared hooks for components in Atlantis.
|
|
|
4
4
|
|
|
5
5
|
## Hooks
|
|
6
6
|
|
|
7
|
-
- [useAssert](../?path=/docs/hooks-useassert--docs)
|
|
8
7
|
- [useCallbackRef](../?path=/docs/hooks-usecallbackref--docs)
|
|
9
8
|
- [useBool](../?path=/docs/hooks-usebool--docs)
|
|
10
9
|
- [useCollectionQuery](../?path=/docs/hooks-usecollectionquery--docs)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from "./useAssert";
|
|
2
1
|
export * from "./useBool";
|
|
3
2
|
export * from "./useBreakpoints";
|
|
4
3
|
export * from "./useCallbackRef";
|
|
5
4
|
export * from "./useCollectionQuery";
|
|
5
|
+
export * from "./useDebounce";
|
|
6
6
|
export * from "./useFocusTrap";
|
|
7
7
|
export * from "./useFormState";
|
|
8
8
|
export * from "./useInView";
|
package/dist/index.js
CHANGED
|
@@ -14,11 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./useAssert"), exports);
|
|
18
17
|
__exportStar(require("./useBool"), exports);
|
|
19
18
|
__exportStar(require("./useBreakpoints"), exports);
|
|
20
19
|
__exportStar(require("./useCallbackRef"), exports);
|
|
21
20
|
__exportStar(require("./useCollectionQuery"), exports);
|
|
21
|
+
__exportStar(require("./useDebounce"), exports);
|
|
22
22
|
__exportStar(require("./useFocusTrap"), exports);
|
|
23
23
|
__exportStar(require("./useFormState"), exports);
|
|
24
24
|
__exportStar(require("./useInView"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useDebounce";
|
|
@@ -0,0 +1,17 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./useDebounce"), exports);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import debounce from "lodash/debounce";
|
|
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]): ((...args: Parameters<T>) => any) & import("lodash").Cancelable;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useDebounce = useDebounce;
|
|
7
|
+
const react_1 = require("react");
|
|
8
|
+
const debounce_1 = __importDefault(require("lodash/debounce"));
|
|
9
|
+
/**
|
|
10
|
+
* A hook to easily manage debounced functions, including their cleanup.
|
|
11
|
+
* @param func The function to debounce.
|
|
12
|
+
* @param wait The number of milliseconds to delay.
|
|
13
|
+
* @param options Optional debounce settings.
|
|
14
|
+
* @returns The debounced function.
|
|
15
|
+
*/
|
|
16
|
+
function useDebounce(func, wait, options) {
|
|
17
|
+
const funcRef = (0, react_1.useRef)(func);
|
|
18
|
+
// We're keeping the provided func wrapped in a ref to avoid forcing the consumer to memoize it.
|
|
19
|
+
// This is a defense against consumers who aren't memoizing their functions.. or if they are
|
|
20
|
+
// memoized but invalidating too frequently due to possible bugs.
|
|
21
|
+
if (funcRef.current !== func) {
|
|
22
|
+
funcRef.current = func;
|
|
23
|
+
}
|
|
24
|
+
const debounced = (0, react_1.useMemo)(() => {
|
|
25
|
+
return (0, debounce_1.default)((...args) => funcRef.current(...args), wait, options);
|
|
26
|
+
// Note: do not add any dependencies! There is currently no use case where we would change
|
|
27
|
+
// the wait delay or options between renders. By not tracking as dependencies, this allows
|
|
28
|
+
// consumers of useDebounce to provide a raw object for options rather than forcing them to
|
|
29
|
+
// memoize that param. Same with the func they provide, we're using a ref to keep that up
|
|
30
|
+
// to date and to avoid forcing the consumer to memoize it.
|
|
31
|
+
}, []);
|
|
32
|
+
(0, react_1.useEffect)(() => {
|
|
33
|
+
// If the debounced function is recreated (or unmounted), cancel the last call.
|
|
34
|
+
return () => debounced.cancel();
|
|
35
|
+
}, [debounced]);
|
|
36
|
+
return debounced;
|
|
37
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
const react_1 = __importStar(require("react"));
|
|
36
|
+
const react_2 = require("@testing-library/react");
|
|
37
|
+
const useDebounce_1 = require("./useDebounce");
|
|
38
|
+
const DEBOUNCE_WAIT = 300;
|
|
39
|
+
describe("useDebounce", () => {
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
jest.useFakeTimers();
|
|
42
|
+
});
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
jest.useRealTimers();
|
|
45
|
+
});
|
|
46
|
+
it("should debounce the function call", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
+
const mockFn = jest.fn();
|
|
48
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
49
|
+
result.current("test");
|
|
50
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
51
|
+
// Fast-forward time
|
|
52
|
+
(0, react_2.act)(() => {
|
|
53
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
54
|
+
});
|
|
55
|
+
expect(mockFn).toHaveBeenCalledWith("test");
|
|
56
|
+
expect(mockFn).toHaveBeenCalledTimes(1);
|
|
57
|
+
}));
|
|
58
|
+
it("should cancel pending debounced calls on unmount", () => {
|
|
59
|
+
const mockFn = jest.fn();
|
|
60
|
+
const { result, unmount } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
61
|
+
result.current("test");
|
|
62
|
+
unmount();
|
|
63
|
+
// Fast-forward time
|
|
64
|
+
(0, react_2.act)(() => {
|
|
65
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT);
|
|
66
|
+
});
|
|
67
|
+
expect(mockFn).not.toHaveBeenCalled();
|
|
68
|
+
});
|
|
69
|
+
it("should handle multiple calls within the debounce period", () => {
|
|
70
|
+
const mockFn = jest.fn();
|
|
71
|
+
const { result } = (0, react_2.renderHook)(() => (0, useDebounce_1.useDebounce)(mockFn, DEBOUNCE_WAIT));
|
|
72
|
+
result.current("first");
|
|
73
|
+
// Fast-forward half the debounce time
|
|
74
|
+
(0, react_2.act)(() => {
|
|
75
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT / 2);
|
|
76
|
+
});
|
|
77
|
+
result.current("second");
|
|
78
|
+
// Fast-forward remaining debounce time
|
|
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 work with React components", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
86
|
+
function DebouncedComponent() {
|
|
87
|
+
const [value, setValue] = (0, react_1.useState)("");
|
|
88
|
+
const [debouncedValue, setDebouncedValue] = (0, react_1.useState)("");
|
|
89
|
+
const debouncedSetValue = (0, useDebounce_1.useDebounce)((newValue) => {
|
|
90
|
+
setDebouncedValue(newValue);
|
|
91
|
+
}, DEBOUNCE_WAIT);
|
|
92
|
+
(0, react_1.useEffect)(() => {
|
|
93
|
+
debouncedSetValue(value);
|
|
94
|
+
}, [value, debouncedSetValue]);
|
|
95
|
+
return (react_1.default.createElement("div", null,
|
|
96
|
+
react_1.default.createElement("input", { "data-testid": "input", value: value, onChange: e => setValue(e.target.value) }),
|
|
97
|
+
react_1.default.createElement("div", { "data-testid": "debounced-value" }, debouncedValue)));
|
|
98
|
+
}
|
|
99
|
+
(0, react_2.render)(react_1.default.createElement(DebouncedComponent, null));
|
|
100
|
+
const input = react_2.screen.getByTestId("input");
|
|
101
|
+
const debouncedValue = react_2.screen.getByTestId("debounced-value");
|
|
102
|
+
// Using fireEvent instead of userEvent
|
|
103
|
+
react_2.fireEvent.change(input, { target: { value: "test" } });
|
|
104
|
+
expect(debouncedValue.textContent).toBe("");
|
|
105
|
+
// Fast-forward time
|
|
106
|
+
(0, react_2.act)(() => {
|
|
107
|
+
jest.advanceTimersByTime(DEBOUNCE_WAIT + 100);
|
|
108
|
+
});
|
|
109
|
+
expect(debouncedValue.textContent).toBe("test");
|
|
110
|
+
}), 10000); // 10 second timeout
|
|
111
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jobber/hooks",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.15.1-export-use-7a742d1.85+7a742d1e",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.js",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"@apollo/client": "^3.0.0",
|
|
39
39
|
"react": "^18.2.0"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "7a742d1e2dc479ba136d7ecf5a0bf5186e3361eb"
|
|
42
42
|
}
|
package/useDebounce.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./dist/useDebounce";
|
|
@@ -4,14 +4,14 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true,
|
|
5
5
|
});
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var useDebounce = require("./dist/useDebounce");
|
|
8
8
|
|
|
9
|
-
Object.keys(
|
|
9
|
+
Object.keys(useDebounce).forEach(function(key) {
|
|
10
10
|
if (key === "default" || key === "__esModule") return;
|
|
11
11
|
Object.defineProperty(exports, key, {
|
|
12
12
|
enumerable: true,
|
|
13
13
|
get: function get() {
|
|
14
|
-
return
|
|
14
|
+
return useDebounce[key];
|
|
15
15
|
},
|
|
16
16
|
});
|
|
17
17
|
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { useAssert } from "./useAssert";
|
package/dist/useAssert/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useAssert = void 0;
|
|
4
|
-
var useAssert_1 = require("./useAssert");
|
|
5
|
-
Object.defineProperty(exports, "useAssert", { enumerable: true, get: function () { return useAssert_1.useAssert; } });
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.useAssert = useAssert;
|
|
7
|
-
const process_1 = __importDefault(require("process"));
|
|
8
|
-
function useAssert(shouldShow, message, options) {
|
|
9
|
-
var _a;
|
|
10
|
-
if (((_a = process_1.default === null || process_1.default === void 0 ? void 0 : process_1.default.env) === null || _a === void 0 ? void 0 : _a.NODE_ENV) !== "production" && shouldShow) {
|
|
11
|
-
if (options === null || options === void 0 ? void 0 : options.warn) {
|
|
12
|
-
console.warn(message);
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
throw new Error(message);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
package/useAssert.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./dist/useAssert";
|