@1money/hooks 0.1.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.
Files changed (46) hide show
  1. package/README.md +53 -0
  2. package/es/index.d.ts +11 -0
  3. package/es/index.js +11 -0
  4. package/es/useControlledState/index.d.ts +14 -0
  5. package/es/useControlledState/index.js +36 -0
  6. package/es/useEventCallback/index.d.ts +11 -0
  7. package/es/useEventCallback/index.js +16 -0
  8. package/es/useLatest/index.d.ts +9 -0
  9. package/es/useLatest/index.js +15 -0
  10. package/es/useLayoutEffect/index.d.ts +11 -0
  11. package/es/useLayoutEffect/index.js +16 -0
  12. package/es/useLayoutState/index.d.ts +13 -0
  13. package/es/useLayoutState/index.js +76 -0
  14. package/es/useMemoizedFn/index.d.ts +12 -0
  15. package/es/useMemoizedFn/index.js +26 -0
  16. package/es/usePrevious/index.d.ts +11 -0
  17. package/es/usePrevious/index.js +19 -0
  18. package/es/useSafeState/index.d.ts +12 -0
  19. package/es/useSafeState/index.js +40 -0
  20. package/es/useSyncState/index.d.ts +14 -0
  21. package/es/useSyncState/index.js +43 -0
  22. package/es/useUpdateEffect/index.d.ts +9 -0
  23. package/es/useUpdateEffect/index.js +19 -0
  24. package/lib/index.d.ts +11 -0
  25. package/lib/index.js +68 -0
  26. package/lib/useControlledState/index.d.ts +14 -0
  27. package/lib/useControlledState/index.js +54 -0
  28. package/lib/useEventCallback/index.d.ts +11 -0
  29. package/lib/useEventCallback/index.js +42 -0
  30. package/lib/useLatest/index.d.ts +9 -0
  31. package/lib/useLatest/index.js +30 -0
  32. package/lib/useLayoutEffect/index.d.ts +11 -0
  33. package/lib/useLayoutEffect/index.js +33 -0
  34. package/lib/useLayoutState/index.d.ts +13 -0
  35. package/lib/useLayoutState/index.js +85 -0
  36. package/lib/useMemoizedFn/index.d.ts +12 -0
  37. package/lib/useMemoizedFn/index.js +36 -0
  38. package/lib/usePrevious/index.d.ts +11 -0
  39. package/lib/usePrevious/index.js +32 -0
  40. package/lib/useSafeState/index.d.ts +12 -0
  41. package/lib/useSafeState/index.js +42 -0
  42. package/lib/useSyncState/index.d.ts +14 -0
  43. package/lib/useSyncState/index.js +54 -0
  44. package/lib/useUpdateEffect/index.d.ts +9 -0
  45. package/lib/useUpdateEffect/index.js +35 -0
  46. package/package.json +117 -0
@@ -0,0 +1,54 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/useControlledState/index.ts
30
+ var useControlledState_exports = {};
31
+ __export(useControlledState_exports, {
32
+ default: () => useControlledState
33
+ });
34
+ module.exports = __toCommonJS(useControlledState_exports);
35
+ var import_react = require("react");
36
+ var import_useLayoutEffect = __toESM(require("../useLayoutEffect"));
37
+ function useControlledState(defaultStateValue, value) {
38
+ const [innerValue, setInnerValue] = (0, import_react.useState)(defaultStateValue);
39
+ const mergedValue = value !== void 0 ? value : innerValue;
40
+ (0, import_useLayoutEffect.default)(
41
+ (mount) => {
42
+ if (!mount && value !== void 0) {
43
+ setInnerValue(value);
44
+ }
45
+ },
46
+ [value]
47
+ );
48
+ return [
49
+ // Value
50
+ mergedValue,
51
+ // Update function
52
+ setInnerValue
53
+ ];
54
+ }
@@ -0,0 +1,11 @@
1
+ type Fn<T extends unknown[], R> = (...args: T) => R;
2
+ /**
3
+ * A hook that returns a stable callback reference that always invokes
4
+ * the latest version of the provided function. Similar to the React
5
+ * useEvent RFC proposal.
6
+ *
7
+ * @param fn The function to wrap
8
+ * @returns A stable callback that always calls the latest fn
9
+ */
10
+ export default function useEventCallback<T extends unknown[], R>(fn: Fn<T, R>): Fn<T, R>;
11
+ export {};
@@ -0,0 +1,42 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/useEventCallback/index.ts
30
+ var useEventCallback_exports = {};
31
+ __export(useEventCallback_exports, {
32
+ default: () => useEventCallback
33
+ });
34
+ module.exports = __toCommonJS(useEventCallback_exports);
35
+ var import_react = require("react");
36
+ var import_useLatest = __toESM(require("../useLatest"));
37
+ function useEventCallback(fn) {
38
+ const fnRef = (0, import_useLatest.default)(fn);
39
+ return (0, import_react.useCallback)((...args) => {
40
+ return fnRef.current(...args);
41
+ }, []);
42
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * A hook that returns a ref object whose `.current` property is always
3
+ * updated to the latest value. Useful for accessing the latest value
4
+ * in callbacks without causing re-renders or stale closures.
5
+ *
6
+ * @param value The value to keep up-to-date
7
+ * @returns A ref object containing the latest value
8
+ */
9
+ export default function useLatest<T>(value: T): import("react").RefObject<T>;
@@ -0,0 +1,30 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/useLatest/index.ts
20
+ var useLatest_exports = {};
21
+ __export(useLatest_exports, {
22
+ default: () => useLatest
23
+ });
24
+ module.exports = __toCommonJS(useLatest_exports);
25
+ var import_react = require("react");
26
+ function useLatest(value) {
27
+ const ref = (0, import_react.useRef)(value);
28
+ ref.current = value;
29
+ return ref;
30
+ }
@@ -0,0 +1,11 @@
1
+ import { type DependencyList } from 'react';
2
+ type EffectCallback = (mount: boolean) => void | (() => void);
3
+ /**
4
+ * A custom `useLayoutEffect` that passes a boolean indicating whether
5
+ * it's the initial mount (`true`) or a subsequent update (`false`).
6
+ *
7
+ * @param effect The effect callback that receives a `mount` parameter
8
+ * @param deps The dependency array
9
+ */
10
+ export default function useLayoutEffect(effect: EffectCallback, deps?: DependencyList): void;
11
+ export {};
@@ -0,0 +1,33 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/useLayoutEffect/index.ts
20
+ var useLayoutEffect_exports = {};
21
+ __export(useLayoutEffect_exports, {
22
+ default: () => useLayoutEffect
23
+ });
24
+ module.exports = __toCommonJS(useLayoutEffect_exports);
25
+ var import_react = require("react");
26
+ function useLayoutEffect(effect, deps) {
27
+ const isMounted = (0, import_react.useRef)(false);
28
+ (0, import_react.useLayoutEffect)(() => {
29
+ const mount = !isMounted.current;
30
+ isMounted.current = true;
31
+ return effect(mount);
32
+ }, deps);
33
+ }
@@ -0,0 +1,13 @@
1
+ export type Updater<State> = (prev: State) => State;
2
+ /**
3
+ * Execute code before next frame but async.
4
+ *
5
+ * Batches multiple state updates into a single microtask (via `Promise.resolve`),
6
+ * reducing unnecessary re-renders when multiple updates happen synchronously.
7
+ *
8
+ * @param defaultState The initial state value
9
+ * @returns A tuple of `[state, setFrameState]`
10
+ */
11
+ export default function useLayoutState<State>(defaultState: State): [State, (updater: Updater<State>) => void];
12
+ /** Lock frame, when frame pass reset the lock. */
13
+ export declare function useTimeoutLock<State>(defaultState?: State): [(state: State) => void, () => State | null];
@@ -0,0 +1,85 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/useLayoutState/index.ts
20
+ var useLayoutState_exports = {};
21
+ __export(useLayoutState_exports, {
22
+ default: () => useLayoutState,
23
+ useTimeoutLock: () => useTimeoutLock
24
+ });
25
+ module.exports = __toCommonJS(useLayoutState_exports);
26
+ var import_react = require("react");
27
+ function useLayoutState(defaultState) {
28
+ const stateRef = (0, import_react.useRef)(defaultState);
29
+ const [, forceUpdate] = (0, import_react.useState)({});
30
+ const lastPromiseRef = (0, import_react.useRef)(null);
31
+ const updateBatchRef = (0, import_react.useRef)([]);
32
+ const setFrameState = (0, import_react.useCallback)((updater) => {
33
+ updateBatchRef.current.push(updater);
34
+ const promise = Promise.resolve();
35
+ lastPromiseRef.current = promise;
36
+ promise.then(() => {
37
+ if (lastPromiseRef.current === promise) {
38
+ const prevBatch = updateBatchRef.current;
39
+ const prevState = stateRef.current;
40
+ updateBatchRef.current = [];
41
+ prevBatch.forEach((batchUpdater) => {
42
+ stateRef.current = batchUpdater(stateRef.current);
43
+ });
44
+ lastPromiseRef.current = null;
45
+ if (prevState !== stateRef.current) {
46
+ forceUpdate({});
47
+ }
48
+ }
49
+ });
50
+ }, []);
51
+ (0, import_react.useEffect)(
52
+ () => () => {
53
+ lastPromiseRef.current = null;
54
+ },
55
+ []
56
+ );
57
+ return [stateRef.current, setFrameState];
58
+ }
59
+ function useTimeoutLock(defaultState) {
60
+ const frameRef = (0, import_react.useRef)(defaultState ?? null);
61
+ const timeoutRef = (0, import_react.useRef)(null);
62
+ function cleanUp() {
63
+ if (timeoutRef.current != null) {
64
+ clearTimeout(timeoutRef.current);
65
+ timeoutRef.current = null;
66
+ }
67
+ }
68
+ function setState(newState) {
69
+ frameRef.current = newState;
70
+ cleanUp();
71
+ timeoutRef.current = setTimeout(() => {
72
+ frameRef.current = null;
73
+ timeoutRef.current = null;
74
+ }, 100);
75
+ }
76
+ function getState() {
77
+ return frameRef.current;
78
+ }
79
+ (0, import_react.useEffect)(() => cleanUp, []);
80
+ return [setState, getState];
81
+ }
82
+ // Annotate the CommonJS export names for ESM import in node:
83
+ 0 && (module.exports = {
84
+ useTimeoutLock
85
+ });
@@ -0,0 +1,12 @@
1
+ type Fn<T extends unknown[], R> = (...args: T) => R;
2
+ /**
3
+ * A hook that returns a memoized version of a callback function.
4
+ * Unlike `useCallback`, it maintains referential equality across renders
5
+ * without requiring a dependency array, while always calling the latest
6
+ * version of the function.
7
+ *
8
+ * @param fn The function to memoize
9
+ * @returns A memoized function with stable reference
10
+ */
11
+ export default function useMemoizedFn<T extends unknown[], R>(fn: Fn<T, R>): Fn<T, R>;
12
+ export {};
@@ -0,0 +1,36 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/useMemoizedFn/index.ts
20
+ var useMemoizedFn_exports = {};
21
+ __export(useMemoizedFn_exports, {
22
+ default: () => useMemoizedFn
23
+ });
24
+ module.exports = __toCommonJS(useMemoizedFn_exports);
25
+ var import_react = require("react");
26
+ function useMemoizedFn(fn) {
27
+ const fnRef = (0, import_react.useRef)(fn);
28
+ fnRef.current = (0, import_react.useMemo)(() => fn, [fn]);
29
+ const memoizedFn = (0, import_react.useRef)(void 0);
30
+ if (!memoizedFn.current) {
31
+ memoizedFn.current = function(...args) {
32
+ return fnRef.current.apply(this, args);
33
+ };
34
+ }
35
+ return memoizedFn.current;
36
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Returns the last distinct value observed before the current `value`.
3
+ *
4
+ * Because the internal effect depends on `[value]`, the ref is only updated
5
+ * when `value` changes. That means this hook tracks the previous value on
6
+ * value-change boundaries (not every render).
7
+ *
8
+ * @param value The value to track
9
+ * @returns The previous distinct value, or `undefined` until one exists
10
+ */
11
+ export default function usePrevious<T>(value: T): T | undefined;
@@ -0,0 +1,32 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/usePrevious/index.ts
20
+ var usePrevious_exports = {};
21
+ __export(usePrevious_exports, {
22
+ default: () => usePrevious
23
+ });
24
+ module.exports = __toCommonJS(usePrevious_exports);
25
+ var import_react = require("react");
26
+ function usePrevious(value) {
27
+ const ref = (0, import_react.useRef)(void 0);
28
+ (0, import_react.useEffect)(() => {
29
+ ref.current = value;
30
+ }, [value]);
31
+ return ref.current;
32
+ }
@@ -0,0 +1,12 @@
1
+ import { type Dispatch, type SetStateAction } from 'react';
2
+ /**
3
+ * It is exactly the same as `React.useState`, but after the component is unmounted,
4
+ * the setState in the asynchronous callback will no longer be executed to avoid
5
+ * memory leakage caused by updating the state after the component is unmounted.
6
+ *
7
+ * @param initialState The initial state value or initializer function
8
+ * @returns A tuple of [state, setSafeState] similar to useState
9
+ */
10
+ declare function useSafeState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
11
+ declare function useSafeState<S = undefined>(): [S | undefined, Dispatch<SetStateAction<S | undefined>>];
12
+ export default useSafeState;
@@ -0,0 +1,42 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/useSafeState/index.ts
20
+ var useSafeState_exports = {};
21
+ __export(useSafeState_exports, {
22
+ default: () => useSafeState_default
23
+ });
24
+ module.exports = __toCommonJS(useSafeState_exports);
25
+ var import_react = require("react");
26
+ function useSafeState(initialState) {
27
+ const isMountedRef = (0, import_react.useRef)(true);
28
+ const [state, setState] = (0, import_react.useState)(initialState);
29
+ (0, import_react.useEffect)(() => {
30
+ isMountedRef.current = true;
31
+ return () => {
32
+ isMountedRef.current = false;
33
+ };
34
+ }, []);
35
+ const setSafeState = (0, import_react.useCallback)((value) => {
36
+ if (isMountedRef.current) {
37
+ setState(value);
38
+ }
39
+ }, []);
40
+ return [state, setSafeState];
41
+ }
42
+ var useSafeState_default = useSafeState;
@@ -0,0 +1,14 @@
1
+ import { type SetStateAction } from 'react';
2
+ type GetState<T> = () => T;
3
+ type SetState<T> = (value: SetStateAction<T>) => void;
4
+ /**
5
+ * Same as `React.useState` but will always get the latest state.
6
+ * This is useful when React merges multiple state updates into one.
7
+ * e.g. `onTransitionEnd` triggers multiple events at once that will be merged
8
+ * into a single state update in React.
9
+ *
10
+ * @param initialState The initial state value or initializer function
11
+ * @returns A tuple of [getState, setState] where getState always returns the latest value
12
+ */
13
+ export default function useSyncState<T>(initialState: T | (() => T)): [GetState<T>, SetState<T>];
14
+ export {};
@@ -0,0 +1,54 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // src/useSyncState/index.ts
30
+ var useSyncState_exports = {};
31
+ __export(useSyncState_exports, {
32
+ default: () => useSyncState
33
+ });
34
+ module.exports = __toCommonJS(useSyncState_exports);
35
+ var import_react = require("react");
36
+ var import_useEventCallback = __toESM(require("../useEventCallback"));
37
+ var UNINITIALIZED = Symbol("useSyncState.uninitialized");
38
+ function useSyncState(initialState) {
39
+ const stateRef = (0, import_react.useRef)(UNINITIALIZED);
40
+ if (stateRef.current === UNINITIALIZED) {
41
+ stateRef.current = typeof initialState === "function" ? initialState() : initialState;
42
+ }
43
+ const [, forceUpdate] = (0, import_react.useState)({});
44
+ const getState = (0, import_useEventCallback.default)(() => stateRef.current);
45
+ const setState = (0, import_useEventCallback.default)((value) => {
46
+ const prevState = stateRef.current;
47
+ const nextState = typeof value === "function" ? value(prevState) : value;
48
+ if (!Object.is(prevState, nextState)) {
49
+ stateRef.current = nextState;
50
+ forceUpdate({});
51
+ }
52
+ });
53
+ return [getState, setState];
54
+ }
@@ -0,0 +1,9 @@
1
+ import { type DependencyList, type EffectCallback } from 'react';
2
+ /**
3
+ * A hook identical to `useEffect`, but it skips the effect on the initial mount
4
+ * and only runs on subsequent updates.
5
+ *
6
+ * @param effect The effect callback to run on updates
7
+ * @param deps The dependency array
8
+ */
9
+ export default function useUpdateEffect(effect: EffectCallback, deps?: DependencyList): void;
@@ -0,0 +1,35 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/useUpdateEffect/index.ts
20
+ var useUpdateEffect_exports = {};
21
+ __export(useUpdateEffect_exports, {
22
+ default: () => useUpdateEffect
23
+ });
24
+ module.exports = __toCommonJS(useUpdateEffect_exports);
25
+ var import_react = require("react");
26
+ function useUpdateEffect(effect, deps) {
27
+ const isMounted = (0, import_react.useRef)(false);
28
+ (0, import_react.useEffect)(() => {
29
+ if (!isMounted.current) {
30
+ isMounted.current = true;
31
+ return;
32
+ }
33
+ return effect();
34
+ }, deps);
35
+ }