@assistant-ui/tap 0.6.1 → 0.7.1
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 +9 -6
- package/dist/core/ResourceFiber.d.ts +5 -5
- package/dist/core/ResourceFiber.d.ts.map +1 -1
- package/dist/core/ResourceFiber.js +26 -18
- package/dist/core/ResourceFiber.js.map +1 -1
- package/dist/core/createTapRoot.d.ts +9 -0
- package/dist/core/createTapRoot.d.ts.map +1 -0
- package/dist/core/createTapRoot.js +27 -0
- package/dist/core/createTapRoot.js.map +1 -0
- package/dist/core/helpers/commit.d.ts +1 -1
- package/dist/core/helpers/commit.d.ts.map +1 -1
- package/dist/core/helpers/commit.js +6 -1
- package/dist/core/helpers/commit.js.map +1 -1
- package/dist/core/helpers/execution-context.d.ts +4 -5
- package/dist/core/helpers/execution-context.d.ts.map +1 -1
- package/dist/core/helpers/execution-context.js +1 -7
- package/dist/core/helpers/execution-context.js.map +1 -1
- package/dist/core/helpers/root.d.ts +3 -2
- package/dist/core/helpers/root.d.ts.map +1 -1
- package/dist/core/helpers/root.js +19 -15
- package/dist/core/helpers/root.js.map +1 -1
- package/dist/core/react-dispatcher.d.ts.map +1 -1
- package/dist/core/react-dispatcher.js +14 -14
- package/dist/core/react-dispatcher.js.map +1 -1
- package/dist/core/resource.d.ts +2 -4
- package/dist/core/resource.d.ts.map +1 -1
- package/dist/core/resource.js +5 -10
- package/dist/core/resource.js.map +1 -1
- package/dist/core/scheduler.d.ts +2 -2
- package/dist/core/scheduler.d.ts.map +1 -1
- package/dist/core/scheduler.js +2 -2
- package/dist/core/scheduler.js.map +1 -1
- package/dist/core/types.d.ts +27 -25
- package/dist/core/types.d.ts.map +1 -1
- package/dist/hooks/useResource.d.ts +2 -2
- package/dist/hooks/useResource.d.ts.map +1 -1
- package/dist/hooks/useResource.js +14 -20
- package/dist/hooks/useResource.js.map +1 -1
- package/dist/hooks/useResources.d.ts +1 -1
- package/dist/hooks/useResources.d.ts.map +1 -1
- package/dist/hooks/useResources.js +18 -27
- package/dist/hooks/useResources.js.map +1 -1
- package/dist/hooks/useTapHost.d.ts +21 -0
- package/dist/hooks/useTapHost.d.ts.map +1 -0
- package/dist/hooks/useTapHost.js +30 -0
- package/dist/hooks/useTapHost.js.map +1 -0
- package/dist/hooks/useTapRoot.d.ts +18 -0
- package/dist/hooks/useTapRoot.d.ts.map +1 -0
- package/dist/hooks/useTapRoot.js +77 -0
- package/dist/hooks/useTapRoot.js.map +1 -0
- package/dist/hooks/utils/depsShallowEqual.d.ts.map +1 -1
- package/dist/hooks/utils/depsShallowEqual.js +5 -2
- package/dist/hooks/utils/depsShallowEqual.js.map +1 -1
- package/dist/hooks/utils/useCell.d.ts +2 -2
- package/dist/hooks/utils/useCell.d.ts.map +1 -1
- package/dist/hooks/utils/useCell.js.map +1 -1
- package/dist/hooks/utils/useDevStrictMode.d.ts +5 -0
- package/dist/hooks/utils/useDevStrictMode.d.ts.map +1 -0
- package/dist/hooks/utils/useDevStrictMode.js +25 -0
- package/dist/hooks/utils/useDevStrictMode.js.map +1 -0
- package/dist/hooks/utils/useRenderMemo.d.ts +5 -0
- package/dist/hooks/utils/useRenderMemo.d.ts.map +1 -0
- package/dist/hooks/utils/useRenderMemo.js +25 -0
- package/dist/hooks/utils/useRenderMemo.js.map +1 -0
- package/dist/hooks/utils/useResourceFiberHostUtils.d.ts +10 -0
- package/dist/hooks/utils/useResourceFiberHostUtils.d.ts.map +1 -0
- package/dist/hooks/utils/useResourceFiberHostUtils.js +46 -0
- package/dist/hooks/utils/useResourceFiberHostUtils.js.map +1 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +7 -4
- package/dist/{hooks → react-hooks}/index.d.ts +6 -6
- package/dist/{hooks → react-hooks}/index.js +5 -5
- package/dist/{hooks → react-hooks}/use.d.ts +1 -1
- package/dist/{hooks → react-hooks}/use.d.ts.map +1 -1
- package/dist/{hooks → react-hooks}/use.js +1 -1
- package/dist/react-hooks/use.js.map +1 -0
- package/dist/{hooks → react-hooks}/useCallback.d.ts +1 -1
- package/dist/react-hooks/useCallback.d.ts.map +1 -0
- package/dist/{hooks → react-hooks}/useCallback.js +1 -1
- package/dist/react-hooks/useCallback.js.map +1 -0
- package/dist/{hooks → react-hooks}/useEffect.d.ts +1 -1
- package/dist/react-hooks/useEffect.d.ts.map +1 -0
- package/dist/react-hooks/useEffect.js +35 -0
- package/dist/react-hooks/useEffect.js.map +1 -0
- package/dist/{hooks → react-hooks}/useEffectEvent.d.ts +1 -1
- package/dist/react-hooks/useEffectEvent.d.ts.map +1 -0
- package/dist/{hooks → react-hooks}/useEffectEvent.js +2 -2
- package/dist/react-hooks/useEffectEvent.js.map +1 -0
- package/dist/{hooks → react-hooks}/useMemo.d.ts +1 -1
- package/dist/react-hooks/useMemo.d.ts.map +1 -0
- package/dist/{hooks → react-hooks}/useMemo.js +3 -3
- package/dist/react-hooks/useMemo.js.map +1 -0
- package/dist/{hooks → react-hooks}/useMemoCache.d.ts +1 -1
- package/dist/react-hooks/useMemoCache.d.ts.map +1 -0
- package/dist/{hooks → react-hooks}/useMemoCache.js +1 -1
- package/dist/react-hooks/useMemoCache.js.map +1 -0
- package/dist/react-hooks/useReducer.d.ts +9 -0
- package/dist/react-hooks/useReducer.d.ts.map +1 -0
- package/dist/react-hooks/useReducer.js +120 -0
- package/dist/react-hooks/useReducer.js.map +1 -0
- package/dist/{hooks → react-hooks}/useRef.d.ts +1 -1
- package/dist/react-hooks/useRef.d.ts.map +1 -0
- package/dist/{hooks → react-hooks}/useRef.js +1 -1
- package/dist/react-hooks/useRef.js.map +1 -0
- package/dist/{hooks → react-hooks}/useState.d.ts +1 -1
- package/dist/react-hooks/useState.d.ts.map +1 -0
- package/dist/{hooks → react-hooks}/useState.js +3 -3
- package/dist/react-hooks/useState.js.map +1 -0
- package/dist/react-shim/index.js +11 -11
- package/dist/react-shim/index.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/basic/resourceHandle.test.ts +32 -22
- package/src/__tests__/basic/tapEffect.basic.test.ts +8 -8
- package/src/__tests__/basic/tapReducer.basic.test.ts +16 -14
- package/src/__tests__/basic/tapResources.basic.test.ts +19 -16
- package/src/__tests__/basic/tapState.basic.test.ts +11 -11
- package/src/__tests__/bench/hosts.bench.tsx +124 -0
- package/src/__tests__/bench/tree.bench.tsx +166 -0
- package/src/__tests__/errors/errors.effect-errors.test.ts +12 -13
- package/src/__tests__/errors/errors.render-errors.test.ts +65 -22
- package/src/__tests__/lifecycle/lifecycle.dependencies.test.ts +19 -19
- package/src/__tests__/lifecycle/lifecycle.mount-unmount.test.ts +14 -14
- package/src/__tests__/parity/describeParity.tsx +217 -0
- package/src/__tests__/parity/parity.adversarial.test.tsx +375 -0
- package/src/__tests__/parity/parity.basics.test.tsx +281 -0
- package/src/__tests__/parity/parity.divergences.test.tsx +208 -0
- package/src/__tests__/parity/parity.smoke.test.tsx +43 -0
- package/src/__tests__/react/concurrent-mode.test.tsx +10 -6
- package/src/__tests__/react/concurrent-pending-updates.test.tsx +351 -0
- package/src/__tests__/react/concurrent-render-phase.test.tsx +350 -0
- package/src/__tests__/react/react-shim.test.tsx +1 -1
- package/src/__tests__/react/useResource.test.tsx +41 -26
- package/src/__tests__/react/useTapHost.test.tsx +233 -0
- package/src/__tests__/react-dispatcher.test.ts +4 -4
- package/src/__tests__/rules/rules.hook-count.test.ts +21 -21
- package/src/__tests__/rules/rules.hook-order.test.ts +17 -17
- package/src/__tests__/strictmode/strictmode-parity.test.tsx +420 -0
- package/src/__tests__/strictmode/strictmode.test.ts +39 -209
- package/src/__tests__/test-utils.ts +33 -23
- package/src/core/ResourceFiber.ts +43 -35
- package/src/core/createTapRoot.ts +45 -0
- package/src/core/helpers/commit.ts +12 -2
- package/src/core/helpers/execution-context.ts +4 -13
- package/src/core/helpers/root.ts +24 -12
- package/src/core/react-dispatcher.ts +10 -9
- package/src/core/resource.ts +5 -20
- package/src/core/scheduler.ts +1 -1
- package/src/core/types.ts +27 -21
- package/src/hooks/useResource.ts +18 -27
- package/src/hooks/useResources.ts +18 -42
- package/src/hooks/useTapHost.ts +60 -0
- package/src/hooks/useTapRoot.ts +135 -0
- package/src/hooks/utils/depsShallowEqual.ts +12 -2
- package/src/hooks/utils/useCell.ts +2 -2
- package/src/hooks/utils/useDevStrictMode.ts +34 -0
- package/src/hooks/utils/useRenderMemo.ts +27 -0
- package/src/hooks/utils/useResourceFiberHostUtils.ts +61 -0
- package/src/index.ts +6 -3
- package/src/{hooks → react-hooks}/index.ts +4 -4
- package/src/react-hooks/useEffect.ts +58 -0
- package/src/{hooks → react-hooks}/useMemo.ts +1 -1
- package/src/react-hooks/useReducer.ts +254 -0
- package/src/{hooks → react-hooks}/useState.ts +2 -2
- package/src/react-shim/index.ts +1 -1
- package/dist/core/createResourceRoot.d.ts +0 -11
- package/dist/core/createResourceRoot.d.ts.map +0 -1
- package/dist/core/createResourceRoot.js +0 -31
- package/dist/core/createResourceRoot.js.map +0 -1
- package/dist/core/helpers/callResourceFn.d.ts +0 -1
- package/dist/core/helpers/callResourceFn.js +0 -19
- package/dist/core/helpers/callResourceFn.js.map +0 -1
- package/dist/hooks/use.js.map +0 -1
- package/dist/hooks/useCallback.d.ts.map +0 -1
- package/dist/hooks/useCallback.js.map +0 -1
- package/dist/hooks/useEffect.d.ts.map +0 -1
- package/dist/hooks/useEffect.js +0 -40
- package/dist/hooks/useEffect.js.map +0 -1
- package/dist/hooks/useEffectEvent.d.ts.map +0 -1
- package/dist/hooks/useEffectEvent.js.map +0 -1
- package/dist/hooks/useMemo.d.ts.map +0 -1
- package/dist/hooks/useMemo.js.map +0 -1
- package/dist/hooks/useMemoCache.d.ts.map +0 -1
- package/dist/hooks/useMemoCache.js.map +0 -1
- package/dist/hooks/useReducer.d.ts +0 -21
- package/dist/hooks/useReducer.d.ts.map +0 -1
- package/dist/hooks/useReducer.js +0 -81
- package/dist/hooks/useReducer.js.map +0 -1
- package/dist/hooks/useRef.d.ts.map +0 -1
- package/dist/hooks/useRef.js.map +0 -1
- package/dist/hooks/useResourceRoot.d.ts +0 -20
- package/dist/hooks/useResourceRoot.d.ts.map +0 -1
- package/dist/hooks/useResourceRoot.js +0 -77
- package/dist/hooks/useResourceRoot.js.map +0 -1
- package/dist/hooks/useState.d.ts.map +0 -1
- package/dist/hooks/useState.js.map +0 -1
- package/dist/react/hooks.d.ts +0 -25
- package/dist/react/hooks.d.ts.map +0 -1
- package/dist/react/hooks.js +0 -69
- package/dist/react/hooks.js.map +0 -1
- package/src/__tests__/strictmode/react-strictmode-behavior.test.tsx +0 -920
- package/src/__tests__/strictmode/react-strictmode-rerender-sources.test.tsx +0 -488
- package/src/__tests__/strictmode/tap-strictmode-rerender-sources.test.ts +0 -687
- package/src/core/createResourceRoot.ts +0 -53
- package/src/core/helpers/callResourceFn.ts +0 -21
- package/src/hooks/useEffect.ts +0 -72
- package/src/hooks/useReducer.ts +0 -160
- package/src/hooks/useResourceRoot.ts +0 -130
- package/src/react/hooks.ts +0 -112
- /package/src/{hooks → react-hooks}/use.ts +0 -0
- /package/src/{hooks → react-hooks}/useCallback.ts +0 -0
- /package/src/{hooks → react-hooks}/useEffectEvent.ts +0 -0
- /package/src/{hooks → react-hooks}/useMemoCache.ts +0 -0
- /package/src/{hooks → react-hooks}/useRef.ts +0 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { depsShallowEqual } from "./depsShallowEqual.js";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
//#region src/hooks/utils/useRenderMemo.ts
|
|
4
|
+
const useRenderMemo = (callback, deps) => {
|
|
5
|
+
const [state] = useState(() => ({
|
|
6
|
+
wipDeps: null,
|
|
7
|
+
wip: null,
|
|
8
|
+
currentDeps: null,
|
|
9
|
+
current: null
|
|
10
|
+
}));
|
|
11
|
+
state.wipDeps = state.currentDeps;
|
|
12
|
+
state.wip = state.current;
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
state.currentDeps = state.wipDeps;
|
|
15
|
+
state.current = state.wip;
|
|
16
|
+
});
|
|
17
|
+
if (state.currentDeps && depsShallowEqual(state.currentDeps, deps)) return state.current;
|
|
18
|
+
state.wipDeps = deps;
|
|
19
|
+
state.wip = callback();
|
|
20
|
+
return state.wip;
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { useRenderMemo };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=useRenderMemo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRenderMemo.js","names":[],"sources":["../../../src/hooks/utils/useRenderMemo.ts"],"sourcesContent":["import { useState, useEffect } from \"react\";\nimport { depsShallowEqual } from \"./depsShallowEqual\";\n\nexport const useRenderMemo = <T>(callback: () => T, deps: unknown[]) => {\n const [state] = useState(() => ({\n wipDeps: null as unknown[] | null,\n wip: null as T,\n currentDeps: null as unknown[] | null,\n current: null as T,\n }));\n\n state.wipDeps = state.currentDeps;\n state.wip = state.current;\n\n useEffect(() => {\n state.currentDeps = state.wipDeps;\n state.current = state.wip;\n });\n\n if (state.currentDeps && depsShallowEqual(state.currentDeps, deps))\n return state.current;\n\n state.wipDeps = deps;\n state.wip = callback();\n\n return state.wip;\n};\n"],"mappings":";;;AAGA,MAAa,iBAAoB,UAAmB,SAAoB;CACtE,MAAM,CAAC,SAAS,gBAAgB;EAC9B,SAAS;EACT,KAAK;EACL,aAAa;EACb,SAAS;CACX,EAAE;CAEF,MAAM,UAAU,MAAM;CACtB,MAAM,MAAM,MAAM;CAElB,gBAAgB;EACd,MAAM,cAAc,MAAM;EAC1B,MAAM,UAAU,MAAM;CACxB,CAAC;CAED,IAAI,MAAM,eAAe,iBAAiB,MAAM,aAAa,IAAI,GAC/D,OAAO,MAAM;CAEf,MAAM,UAAU;CAChB,MAAM,MAAM,SAAS;CAErB,OAAO,MAAM;AACf"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ResourceFiber } from "../../core/types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/hooks/utils/useResourceFiberHostUtils.d.ts
|
|
4
|
+
declare const useResourceFiberHost: () => {
|
|
5
|
+
version: number;
|
|
6
|
+
createFiber: <R, A extends readonly any[]>(hook: (...props: A) => R, _key: string | number | undefined) => ResourceFiber<R, A>;
|
|
7
|
+
};
|
|
8
|
+
//#endregion
|
|
9
|
+
export { useResourceFiberHost };
|
|
10
|
+
//# sourceMappingURL=useResourceFiberHostUtils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useResourceFiberHostUtils.d.ts","names":[],"sources":["../../../src/hooks/utils/useResourceFiberHostUtils.ts"],"mappings":";;;cA0Ca,oBAAA;;6CASmB,IAAA,MAChB,KAAA,EAAO,CAAA,KAAM,CAAA,EAAC,IAAA,kCACS,aAAA,CAAA,CAAA,EAAA,CAAA;AAAA"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getCurrentResourceFiber, peekResourceFiber } from "../../core/helpers/execution-context.js";
|
|
2
|
+
import { createResourceFiberRoot, setRootVersion } from "../../core/helpers/root.js";
|
|
3
|
+
import { createResourceFiber } from "../../core/ResourceFiber.js";
|
|
4
|
+
import { useDevStrictMode } from "./useDevStrictMode.js";
|
|
5
|
+
import { useCallback, useMemo, useReducer, useRef } from "react";
|
|
6
|
+
//#region src/hooks/utils/useResourceFiberHostUtils.ts
|
|
7
|
+
const useResourceFiberHostUtilsTap = () => {
|
|
8
|
+
const versionRef = useRef(0);
|
|
9
|
+
const version = versionRef.current;
|
|
10
|
+
const parent = getCurrentResourceFiber();
|
|
11
|
+
return {
|
|
12
|
+
version,
|
|
13
|
+
markDirty: useMemo(() => () => {
|
|
14
|
+
versionRef.current++;
|
|
15
|
+
parent?.markDirty?.();
|
|
16
|
+
}, [parent]),
|
|
17
|
+
root: parent.root
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
const useResourceFiberHostUtilsReact = () => {
|
|
21
|
+
const root = useMemo(() => {
|
|
22
|
+
return createResourceFiberRoot((cb) => dispatch(cb));
|
|
23
|
+
}, []);
|
|
24
|
+
const [version, dispatch] = useReducer((v, cb) => {
|
|
25
|
+
setRootVersion(root, v);
|
|
26
|
+
return v + (cb() ? 1 : 0);
|
|
27
|
+
}, 0);
|
|
28
|
+
setRootVersion(root, version);
|
|
29
|
+
return {
|
|
30
|
+
root,
|
|
31
|
+
version,
|
|
32
|
+
markDirty: void 0
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
const useResourceFiberHost = () => {
|
|
36
|
+
const getDevMode = useDevStrictMode();
|
|
37
|
+
const { root, version, markDirty } = peekResourceFiber() ? useResourceFiberHostUtilsTap() : useResourceFiberHostUtilsReact();
|
|
38
|
+
return {
|
|
39
|
+
version,
|
|
40
|
+
createFiber: useCallback((hook, _key) => createResourceFiber(hook, root, markDirty, getDevMode()), [])
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
export { useResourceFiberHost };
|
|
45
|
+
|
|
46
|
+
//# sourceMappingURL=useResourceFiberHostUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useResourceFiberHostUtils.js","names":[],"sources":["../../../src/hooks/utils/useResourceFiberHostUtils.ts"],"sourcesContent":["import { useRef, useMemo, useReducer, useCallback } from \"react\";\nimport {\n getCurrentResourceFiber,\n peekResourceFiber,\n} from \"../../core/helpers/execution-context\";\nimport {\n createResourceFiberRoot,\n setRootVersion,\n} from \"../../core/helpers/root\";\nimport { createResourceFiber } from \"../../core/ResourceFiber\";\nimport type { ResourceFiberRoot } from \"../../core/types\";\nimport { useDevStrictMode } from \"./useDevStrictMode\";\n\nconst useResourceFiberHostUtilsTap = () => {\n const versionRef = useRef(0);\n const version = versionRef.current;\n const parent = getCurrentResourceFiber();\n const markDirty = useMemo(\n () => () => {\n versionRef.current++;\n parent?.markDirty?.();\n },\n [parent],\n );\n\n return { version, markDirty, root: parent.root };\n};\n\nconst useResourceFiberHostUtilsReact = () => {\n const root = useMemo<ResourceFiberRoot>(() => {\n return createResourceFiberRoot((cb) => dispatch(cb));\n }, []);\n\n const [version, dispatch] = useReducer((v: number, cb: () => boolean) => {\n setRootVersion(root!, v);\n return v + (cb() ? 1 : 0);\n }, 0);\n setRootVersion(root, version);\n\n return { root, version, markDirty: undefined };\n};\n\nexport const useResourceFiberHost = () => {\n const getDevMode = useDevStrictMode();\n const { root, version, markDirty } = peekResourceFiber()\n ? // oxlint-disable-next-line react-hooks/rules-of-hooks\n useResourceFiberHostUtilsTap()\n : // oxlint-disable-next-line react-hooks/rules-of-hooks\n useResourceFiberHostUtilsReact();\n\n const createFiber = useCallback(\n <R, A extends readonly any[]>(\n hook: (...props: A) => R,\n _key: string | number | undefined,\n ) => createResourceFiber(hook, root, markDirty, getDevMode()),\n // oxlint-disable-next-line react-hooks/exhaustive-deps\n [],\n );\n\n return { version, createFiber };\n};\n"],"mappings":";;;;;;AAaA,MAAM,qCAAqC;CACzC,MAAM,aAAa,OAAO,CAAC;CAC3B,MAAM,UAAU,WAAW;CAC3B,MAAM,SAAS,wBAAwB;CASvC,OAAO;EAAE;EAAS,WARA,oBACJ;GACV,WAAW;GACX,QAAQ,YAAY;EACtB,GACA,CAAC,MAAM,CAGiB;EAAG,MAAM,OAAO;CAAK;AACjD;AAEA,MAAM,uCAAuC;CAC3C,MAAM,OAAO,cAAiC;EAC5C,OAAO,yBAAyB,OAAO,SAAS,EAAE,CAAC;CACrD,GAAG,CAAC,CAAC;CAEL,MAAM,CAAC,SAAS,YAAY,YAAY,GAAW,OAAsB;EACvE,eAAe,MAAO,CAAC;EACvB,OAAO,KAAK,GAAG,IAAI,IAAI;CACzB,GAAG,CAAC;CACJ,eAAe,MAAM,OAAO;CAE5B,OAAO;EAAE;EAAM;EAAS,WAAW,KAAA;CAAU;AAC/C;AAEA,MAAa,6BAA6B;CACxC,MAAM,aAAa,iBAAiB;CACpC,MAAM,EAAE,MAAM,SAAS,cAAc,kBAAkB,IAEnD,6BAA6B,IAE7B,+BAA+B;CAWnC,OAAO;EAAE;EAAS,aATE,aAEhB,MACA,SACG,oBAAoB,MAAM,MAAM,WAAW,WAAW,CAAC,GAE5D,CAAC,CAGyB;CAAE;AAChC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { ContravariantResource, Resource, ResourceElement } from "./core/types.js";
|
|
2
2
|
import { createResourceContext, withContextProvider } from "./core/context.js";
|
|
3
|
-
import {
|
|
3
|
+
import { useTapRoot } from "./hooks/useTapRoot.js";
|
|
4
|
+
import { createTapRoot } from "./core/createTapRoot.js";
|
|
4
5
|
import { resource } from "./core/resource.js";
|
|
5
|
-
import {
|
|
6
|
+
import { flushTapSync } from "./core/scheduler.js";
|
|
6
7
|
import { withKey } from "./core/withKey.js";
|
|
7
|
-
import { useResource
|
|
8
|
-
|
|
8
|
+
import { useResource } from "./hooks/useResource.js";
|
|
9
|
+
import { useResources } from "./hooks/useResources.js";
|
|
10
|
+
import { useTapHost } from "./hooks/useTapHost.js";
|
|
11
|
+
export { type ContravariantResource, type Resource, type ResourceElement, createResourceContext, createTapRoot, flushTapSync, resource, useResource, useResources, useTapHost, useTapRoot, withContextProvider, withKey };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { resource } from "./core/resource.js";
|
|
2
2
|
import { withKey } from "./core/withKey.js";
|
|
3
3
|
import { createResourceContext, withContextProvider } from "./core/context.js";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
4
|
+
import { flushTapSync } from "./core/scheduler.js";
|
|
5
|
+
import { useTapRoot } from "./hooks/useTapRoot.js";
|
|
6
|
+
import { createTapRoot } from "./core/createTapRoot.js";
|
|
7
|
+
import { useResource } from "./hooks/useResource.js";
|
|
8
|
+
import { useResources } from "./hooks/useResources.js";
|
|
9
|
+
import { useTapHost } from "./hooks/useTapHost.js";
|
|
10
|
+
export { createResourceContext, createTapRoot, flushTapSync, resource, useResource, useResources, useTapHost, useTapRoot, withContextProvider, withKey };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useTapRoot } from "../hooks/useTapRoot.js";
|
|
2
|
+
import { useResource } from "../hooks/useResource.js";
|
|
3
|
+
import { useResources } from "../hooks/useResources.js";
|
|
3
4
|
import { useState } from "./useState.js";
|
|
4
|
-
import { useReducer
|
|
5
|
+
import { useReducer } from "./useReducer.js";
|
|
5
6
|
import { useRef } from "./useRef.js";
|
|
6
7
|
import { useMemo } from "./useMemo.js";
|
|
7
8
|
import { useCallback } from "./useCallback.js";
|
|
9
|
+
import { useEffect } from "./useEffect.js";
|
|
8
10
|
import { useEffectEvent } from "./useEffectEvent.js";
|
|
9
11
|
import { use } from "./use.js";
|
|
10
12
|
import { useMemoCache } from "./useMemoCache.js";
|
|
11
|
-
|
|
12
|
-
import { useResources } from "./useResources.js";
|
|
13
|
-
export { use, useCallback, useEffect, useEffectEvent, useMemo, useMemoCache, useReducer, useReducerWithDerivedState, useRef, useResource, useResourceRoot, useResources, useState };
|
|
13
|
+
export { use, useCallback, useEffect, useEffectEvent, useMemo, useMemoCache, useReducer, useRef, useResource, useResources, useState, useTapRoot };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useReducer
|
|
1
|
+
import { useReducer } from "./useReducer.js";
|
|
2
2
|
import { useState } from "./useState.js";
|
|
3
3
|
import { useRef } from "./useRef.js";
|
|
4
4
|
import { useMemo } from "./useMemo.js";
|
|
@@ -7,7 +7,7 @@ import { useEffect } from "./useEffect.js";
|
|
|
7
7
|
import { useEffectEvent } from "./useEffectEvent.js";
|
|
8
8
|
import { use } from "./use.js";
|
|
9
9
|
import { useMemoCache } from "./useMemoCache.js";
|
|
10
|
-
import {
|
|
11
|
-
import { useResource } from "
|
|
12
|
-
import { useResources } from "
|
|
13
|
-
export { use, useCallback, useEffect, useEffectEvent, useMemo, useMemoCache, useReducer,
|
|
10
|
+
import { useTapRoot } from "../hooks/useTapRoot.js";
|
|
11
|
+
import { useResource } from "../hooks/useResource.js";
|
|
12
|
+
import { useResources } from "../hooks/useResources.js";
|
|
13
|
+
export { use, useCallback, useEffect, useEffectEvent, useMemo, useMemoCache, useReducer, useRef, useResource, useResources, useState, useTapRoot };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use.d.ts","names":[],"sources":["../../src/hooks/use.ts"],"mappings":";;AAMA;;;cAAa,GAAA,GAAO,MAAe"}
|
|
1
|
+
{"version":3,"file":"use.d.ts","names":[],"sources":["../../src/react-hooks/use.ts"],"mappings":";;AAMA;;;cAAa,GAAA,GAAO,MAAe"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isResourceContext, useResourceContext } from "../core/context.js";
|
|
2
|
-
//#region src/hooks/use.ts
|
|
2
|
+
//#region src/react-hooks/use.ts
|
|
3
3
|
/**
|
|
4
4
|
* Reads a resource context from inside a resource render, the tap equivalent of
|
|
5
5
|
* React's `use(Context)`. Only resource contexts are supported.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use.js","names":[],"sources":["../../src/react-hooks/use.ts"],"sourcesContent":["import { isResourceContext, useResourceContext } from \"../core/context\";\n\n/**\n * Reads a resource context from inside a resource render, the tap equivalent of\n * React's `use(Context)`. Only resource contexts are supported.\n */\nexport const use = (usable: unknown): unknown => {\n if (!isResourceContext(usable))\n throw new Error(\n \"A tap resource's `use()` only accepts a resource context.\",\n );\n return useResourceContext(usable as never);\n};\n"],"mappings":";;;;;;AAMA,MAAa,OAAO,WAA6B;CAC/C,IAAI,CAAC,kBAAkB,MAAM,GAC3B,MAAM,IAAI,MACR,2DACF;CACF,OAAO,mBAAmB,MAAe;AAC3C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCallback.d.ts","names":[],"sources":["../../src/react-hooks/useCallback.ts"],"mappings":";cAEa,WAAA,iBAA6B,IAAA,iBACxC,EAAA,EAAI,CAAA,EACJ,IAAA,yBACC,CAGF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCallback.js","names":[],"sources":["../../src/react-hooks/useCallback.ts"],"sourcesContent":["import { useMemo } from \"./useMemo\";\n\nexport const useCallback = <T extends (...args: any[]) => any>(\n fn: T,\n deps: readonly unknown[],\n): T => {\n // oxlint-disable-next-line react/exhaustive-deps -- user-provided dep array forwarded verbatim\n return useMemo(() => fn, deps);\n};\n"],"mappings":";;AAEA,MAAa,eACX,IACA,SACM;CAEN,OAAO,cAAc,IAAI,IAAI;AAC/B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEffect.d.ts","names":[],"sources":["../../src/react-hooks/useEffect.ts"],"mappings":";kBAUiB,SAAA;EAAA,KACH,UAAA;EAAA,KACA,cAAA,SAAuB,UAAU;AAAA;AAAA,iBAG/B,SAAA,CAAU,MAAA,EAAQ,SAAA,CAAU,cAAc;AAAA,iBAC1C,SAAA,CACd,MAAA,EAAQ,SAAA,CAAU,cAAc,EAChC,IAAA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { depsShallowEqual } from "../hooks/utils/depsShallowEqual.js";
|
|
2
|
+
import { registerRenderMountTask, useCell } from "../hooks/utils/useCell.js";
|
|
3
|
+
//#region src/react-hooks/useEffect.ts
|
|
4
|
+
const newEffect = () => ({
|
|
5
|
+
type: "effect",
|
|
6
|
+
cleanup: void 0,
|
|
7
|
+
deps: null
|
|
8
|
+
});
|
|
9
|
+
function useEffect(effect, deps) {
|
|
10
|
+
const cell = useCell("effect", newEffect);
|
|
11
|
+
if (deps && cell.deps && depsShallowEqual(cell.deps, deps)) return;
|
|
12
|
+
if (cell.deps !== null && !!deps !== !!cell.deps) throw new Error("useEffect called with and without dependencies across re-renders");
|
|
13
|
+
registerRenderMountTask({
|
|
14
|
+
cleanup: () => {
|
|
15
|
+
try {
|
|
16
|
+
cell.cleanup?.();
|
|
17
|
+
} finally {
|
|
18
|
+
cell.cleanup = void 0;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
setup: () => {
|
|
22
|
+
try {
|
|
23
|
+
const cleanup = effect();
|
|
24
|
+
if (cleanup !== void 0 && typeof cleanup !== "function") throw new Error(`An effect function must either return a cleanup function or nothing. Received: ${typeof cleanup}`);
|
|
25
|
+
cell.cleanup = cleanup;
|
|
26
|
+
} finally {
|
|
27
|
+
cell.deps = deps;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useEffect };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=useEffect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEffect.js","names":[],"sources":["../../src/react-hooks/useEffect.ts"],"sourcesContent":["import type { Cell } from \"../core/types\";\nimport { depsShallowEqual } from \"../hooks/utils/depsShallowEqual\";\nimport { useCell, registerRenderMountTask } from \"../hooks/utils/useCell\";\n\nconst newEffect = (): Cell & { type: \"effect\" } => ({\n type: \"effect\",\n cleanup: undefined,\n deps: null, // null means the effect has never been run\n});\n\nexport namespace useEffect {\n export type Destructor = () => void;\n export type EffectCallback = () => Destructor | undefined;\n}\n\nexport function useEffect(effect: useEffect.EffectCallback): void;\nexport function useEffect(\n effect: useEffect.EffectCallback,\n deps: readonly unknown[],\n): void;\nexport function useEffect(\n effect: useEffect.EffectCallback,\n deps?: readonly unknown[],\n): void {\n const cell = useCell(\"effect\", newEffect);\n\n if (deps && cell.deps && depsShallowEqual(cell.deps, deps)) return;\n if (cell.deps !== null && !!deps !== !!cell.deps)\n throw new Error(\n \"useEffect called with and without dependencies across re-renders\",\n );\n\n registerRenderMountTask({\n cleanup: () => {\n try {\n cell.cleanup?.();\n } finally {\n cell.cleanup = undefined;\n }\n },\n setup: () => {\n try {\n const cleanup = effect();\n\n if (cleanup !== undefined && typeof cleanup !== \"function\") {\n throw new Error(\n \"An effect function must either return a cleanup function or nothing. \" +\n `Received: ${typeof cleanup}`,\n );\n }\n\n cell.cleanup = cleanup;\n } finally {\n cell.deps = deps;\n }\n },\n });\n}\n"],"mappings":";;;AAIA,MAAM,mBAA8C;CAClD,MAAM;CACN,SAAS,KAAA;CACT,MAAM;AACR;AAYA,SAAgB,UACd,QACA,MACM;CACN,MAAM,OAAO,QAAQ,UAAU,SAAS;CAExC,IAAI,QAAQ,KAAK,QAAQ,iBAAiB,KAAK,MAAM,IAAI,GAAG;CAC5D,IAAI,KAAK,SAAS,QAAQ,CAAC,CAAC,SAAS,CAAC,CAAC,KAAK,MAC1C,MAAM,IAAI,MACR,kEACF;CAEF,wBAAwB;EACtB,eAAe;GACb,IAAI;IACF,KAAK,UAAU;GACjB,UAAU;IACR,KAAK,UAAU,KAAA;GACjB;EACF;EACA,aAAa;GACX,IAAI;IACF,MAAM,UAAU,OAAO;IAEvB,IAAI,YAAY,KAAA,KAAa,OAAO,YAAY,YAC9C,MAAM,IAAI,MACR,kFACe,OAAO,SACxB;IAGF,KAAK,UAAU;GACjB,UAAU;IACR,KAAK,OAAO;GACd;EACF;CACF,CAAC;AACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEffectEvent.d.ts","names":[],"sources":["../../src/react-hooks/useEffectEvent.ts"],"mappings":";;AAqBA;;;;;;;;;;AAEI;;;;iBAFY,cAAA,eAA6B,IAAA,iBAC3C,QAAA,EAAU,CAAA,GACT,CAAC"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { isDevelopment } from "../core/helpers/env.js";
|
|
2
1
|
import { getCurrentResourceFiber } from "../core/helpers/execution-context.js";
|
|
2
|
+
import { isDevelopment } from "../core/helpers/env.js";
|
|
3
3
|
import { useRef } from "./useRef.js";
|
|
4
4
|
import { useCallback } from "./useCallback.js";
|
|
5
5
|
import { useEffect } from "./useEffect.js";
|
|
6
|
-
//#region src/hooks/useEffectEvent.ts
|
|
6
|
+
//#region src/react-hooks/useEffectEvent.ts
|
|
7
7
|
/**
|
|
8
8
|
* Creates a stable function reference that always calls the most recent version of the callback.
|
|
9
9
|
* Similar to React's useEffectEvent hook.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEffectEvent.js","names":[],"sources":["../../src/react-hooks/useEffectEvent.ts"],"sourcesContent":["import { useRef } from \"./useRef\";\nimport { useEffect } from \"./useEffect\";\nimport { isDevelopment } from \"../core/helpers/env\";\nimport { useCallback } from \"./useCallback\";\nimport { getCurrentResourceFiber } from \"../core/helpers/execution-context\";\n\n/**\n * Creates a stable function reference that always calls the most recent version of the callback.\n * Similar to React's useEffectEvent hook.\n *\n * @param callback - The callback function to wrap\n * @returns A stable function reference that always calls the latest callback\n *\n * @example\n * ```typescript\n * const handleClick = useEffectEvent((value: string) => {\n * console.log(value);\n * });\n * // handleClick reference is stable, but always calls the latest version\n * ```\n */\nexport function useEffectEvent<T extends (...args: any[]) => any>(\n callback: T,\n): T {\n const callbackRef = useRef(callback);\n\n // TODO this effect needs to run before all userland effects\n useEffect(() => {\n callbackRef.current = callback;\n });\n\n const fiber = getCurrentResourceFiber();\n return useCallback(\n ((...args: Parameters<T>) => {\n if (isDevelopment && fiber.renderContext)\n throw new Error(\"useEffectEvent cannot be called during render\");\n return callbackRef.current(...args);\n }) as T,\n [fiber],\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqBA,SAAgB,eACd,UACG;CACH,MAAM,cAAc,OAAO,QAAQ;CAGnC,gBAAgB;EACd,YAAY,UAAU;CACxB,CAAC;CAED,MAAM,QAAQ,wBAAwB;CACtC,OAAO,cACH,GAAG,SAAwB;EAC3B,IAAI,iBAAiB,MAAM,eACzB,MAAM,IAAI,MAAM,+CAA+C;EACjE,OAAO,YAAY,QAAQ,GAAG,IAAI;CACpC,IACA,CAAC,KAAK,CACR;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMemo.d.ts","names":[],"sources":["../../src/react-hooks/useMemo.ts"],"mappings":";cAWa,OAAA,MAAc,EAAA,QAAU,CAAA,EAAG,IAAA,yBAA2B,CAkBlE"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isDevelopment } from "../core/helpers/env.js";
|
|
2
1
|
import { getCurrentResourceFiber } from "../core/helpers/execution-context.js";
|
|
2
|
+
import { isDevelopment } from "../core/helpers/env.js";
|
|
3
3
|
import { useReducerWithDerivedState } from "./useReducer.js";
|
|
4
|
-
import { depsShallowEqual } from "
|
|
5
|
-
//#region src/hooks/useMemo.ts
|
|
4
|
+
import { depsShallowEqual } from "../hooks/utils/depsShallowEqual.js";
|
|
5
|
+
//#region src/react-hooks/useMemo.ts
|
|
6
6
|
const memoReducer = () => {
|
|
7
7
|
throw new Error("Memo reducer should not be called");
|
|
8
8
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMemo.js","names":[],"sources":["../../src/react-hooks/useMemo.ts"],"sourcesContent":["import { isDevelopment } from \"../core/helpers/env\";\nimport { getCurrentResourceFiber } from \"../core/helpers/execution-context\";\nimport { useReducerWithDerivedState } from \"./useReducer\";\nimport { depsShallowEqual } from \"../hooks/utils/depsShallowEqual\";\n\nconst memoReducer = () => {\n throw new Error(\"Memo reducer should not be called\");\n};\n\ntype MemoState<T> = { value: T; deps: readonly unknown[] };\n\nexport const useMemo = <T>(fn: () => T, deps: readonly unknown[]): T => {\n const fiber = getCurrentResourceFiber();\n const [state] = useReducerWithDerivedState(\n memoReducer,\n (state: MemoState<T> | null): MemoState<T> => {\n if (state && depsShallowEqual(state.deps, deps)) return state;\n\n const value = fn();\n\n if (isDevelopment && fiber.devStrictMode) {\n void fn();\n }\n\n return { value, deps };\n },\n null,\n );\n return state.value;\n};\n"],"mappings":";;;;;AAKA,MAAM,oBAAoB;CACxB,MAAM,IAAI,MAAM,mCAAmC;AACrD;AAIA,MAAa,WAAc,IAAa,SAAgC;CACtE,MAAM,QAAQ,wBAAwB;CACtC,MAAM,CAAC,SAAS,2BACd,cACC,UAA6C;EAC5C,IAAI,SAAS,iBAAiB,MAAM,MAAM,IAAI,GAAG,OAAO;EAExD,MAAM,QAAQ,GAAG;EAEjB,IAAI,iBAAiB,MAAM,eACzB,GAAQ;EAGV,OAAO;GAAE;GAAO;EAAK;CACvB,GACA,IACF;CACA,OAAO,MAAM;AACf"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMemoCache.d.ts","names":[],"sources":["../../src/react-hooks/useMemoCache.ts"],"mappings":";;AASA;;;;cAAa,YAAA,GAAgB,IAAY"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useReducerWithDerivedState } from "./useReducer.js";
|
|
2
|
-
//#region src/hooks/useMemoCache.ts
|
|
2
|
+
//#region src/react-hooks/useMemoCache.ts
|
|
3
3
|
const MEMO_CACHE_SENTINEL = Symbol.for("react.memo_cache_sentinel");
|
|
4
4
|
/**
|
|
5
5
|
* Backs React Compiler's memo cache. Compiled output allocates it through
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMemoCache.js","names":[],"sources":["../../src/react-hooks/useMemoCache.ts"],"sourcesContent":["import { useReducerWithDerivedState } from \"./useReducer\";\n\nconst MEMO_CACHE_SENTINEL = Symbol.for(\"react.memo_cache_sentinel\");\n\n/**\n * Backs React Compiler's memo cache. Compiled output allocates it through\n * `react/compiler-runtime`'s `c(size)` (`ReactSharedInternals.H.useMemoCache(size)`);\n * a tap ref persists the array so compiled resources run without `\"use no memo\"`.\n */\nexport const useMemoCache = (size: number): unknown[] => {\n // clone the memo value once per render\n let cloned = false;\n const [cache] = useReducerWithDerivedState(\n () => [] as unknown[],\n (arr) => {\n if (cloned) return arr;\n cloned = true;\n return [...arr];\n },\n size,\n (length) => Array.from({ length }).fill(MEMO_CACHE_SENTINEL),\n );\n\n return cache;\n};\n"],"mappings":";;AAEA,MAAM,sBAAsB,OAAO,IAAI,2BAA2B;;;;;;AAOlE,MAAa,gBAAgB,SAA4B;CAEvD,IAAI,SAAS;CACb,MAAM,CAAC,SAAS,iCACR,CAAC,IACN,QAAQ;EACP,IAAI,QAAQ,OAAO;EACnB,SAAS;EACT,OAAO,CAAC,GAAG,GAAG;CAChB,GACA,OACC,WAAW,MAAM,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,mBAAmB,CAC7D;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/react-hooks/useReducer.d.ts
|
|
2
|
+
type Dispatch<A> = (action: A) => void;
|
|
3
|
+
declare function useReducer<S, A>(reducer: (state: S, action: A) => S, initialState: S): [S, Dispatch<A>];
|
|
4
|
+
declare function useReducer<S, A, I>(reducer: (state: S, action: A) => S, initialArg: I, init: (arg: I) => S): [S, Dispatch<A>];
|
|
5
|
+
declare function useEagerReducer<S, A, I>(reducer: (state: S, action: A) => S, initialArg: I, init: (arg: I) => S): [S, Dispatch<A>];
|
|
6
|
+
declare function useReducerWithDerivedState<S, A, I, R extends S>(reducer: (state: S, action: A) => S, getDerivedState: (state: S) => R, initialArg: I, init: (arg: I) => S): [R, Dispatch<A>];
|
|
7
|
+
//#endregion
|
|
8
|
+
export { useEagerReducer, useReducer, useReducerWithDerivedState };
|
|
9
|
+
//# sourceMappingURL=useReducer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useReducer.d.ts","names":[],"sources":["../../src/react-hooks/useReducer.ts"],"mappings":";KAQK,QAAA,OAAe,MAAA,EAAQ,CAAC;AAAA,iBA8Kb,UAAA,OACd,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,YAAA,EAAc,CAAA,IACZ,CAAA,EAAG,QAAA,CAAS,CAAA;AAAA,iBACA,UAAA,UACd,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,UAAA,EAAY,CAAA,EACZ,IAAA,GAAO,GAAA,EAAK,CAAA,KAAM,CAAA,IAChB,CAAA,EAAG,QAAA,CAAS,CAAA;AAAA,iBAoBA,eAAA,UACd,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,UAAA,EAAY,CAAA,EACZ,IAAA,GAAO,GAAA,EAAK,CAAA,KAAM,CAAA,IAChB,CAAA,EAAG,QAAA,CAAS,CAAA;AAAA,iBA0BA,0BAAA,oBAA8C,CAAA,EAC5D,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,eAAA,GAAkB,KAAA,EAAO,CAAA,KAAM,CAAA,EAC/B,UAAA,EAAY,CAAA,EACZ,IAAA,GAAO,GAAA,EAAK,CAAA,KAAM,CAAA,IAChB,CAAA,EAAG,QAAA,CAAS,CAAA"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { getCurrentResourceFiber, peekResourceFiber } from "../core/helpers/execution-context.js";
|
|
2
|
+
import { isDevelopment } from "../core/helpers/env.js";
|
|
3
|
+
import { applyChangelogRecord, markCellDirty } from "../core/helpers/root.js";
|
|
4
|
+
//#region src/react-hooks/useReducer.ts
|
|
5
|
+
const dispatchOnFiber = (fiber, callback) => {
|
|
6
|
+
if (fiber.isNeverMounted) throw new Error("Resource updated before mount");
|
|
7
|
+
fiber.root.dispatchUpdate(() => {
|
|
8
|
+
const record = callback();
|
|
9
|
+
if (record) {
|
|
10
|
+
applyChangelogRecord(record);
|
|
11
|
+
fiber.root.changelog.push(record);
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
const createReducerCell = (fiber, reducer, initialArg, initFn, eagerDispatch) => {
|
|
18
|
+
const initialState = initFn ? initFn(initialArg) : initialArg;
|
|
19
|
+
if (isDevelopment && fiber.devStrictMode && initFn) initFn(initialArg);
|
|
20
|
+
const cell = {
|
|
21
|
+
type: "reducer",
|
|
22
|
+
queue: null,
|
|
23
|
+
renderQueue: null,
|
|
24
|
+
workInProgress: initialState,
|
|
25
|
+
current: initialState,
|
|
26
|
+
reducer,
|
|
27
|
+
dispatch: (action) => {
|
|
28
|
+
const currentFiber = peekResourceFiber();
|
|
29
|
+
if (currentFiber !== null) {
|
|
30
|
+
if (currentFiber !== fiber) throw new Error("Cannot update a resource while rendering a different resource.");
|
|
31
|
+
(fiber.renderPendingCells ??= /* @__PURE__ */ new Set()).add(cell);
|
|
32
|
+
(cell.renderQueue ??= []).push(action);
|
|
33
|
+
} else {
|
|
34
|
+
const record = {
|
|
35
|
+
fiber,
|
|
36
|
+
cell,
|
|
37
|
+
action,
|
|
38
|
+
hasEagerState: false,
|
|
39
|
+
eagerState: void 0,
|
|
40
|
+
queued: false
|
|
41
|
+
};
|
|
42
|
+
dispatchOnFiber(fiber, () => {
|
|
43
|
+
if (eagerDispatch && fiber.root.dirtyCells.size === 0 && !record.hasEagerState) {
|
|
44
|
+
record.eagerState = reducer(cell.workInProgress, action);
|
|
45
|
+
record.hasEagerState = true;
|
|
46
|
+
if (Object.is(cell.current, record.eagerState)) return null;
|
|
47
|
+
}
|
|
48
|
+
return record;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
return cell;
|
|
54
|
+
};
|
|
55
|
+
function useReducerImpl(reducer, getDerivedState, initialArg, initFn, eagerDispatch) {
|
|
56
|
+
const fiber = getCurrentResourceFiber();
|
|
57
|
+
const index = fiber.currentIndex++;
|
|
58
|
+
const existing = fiber.cells[index];
|
|
59
|
+
let cell;
|
|
60
|
+
if (existing === void 0) {
|
|
61
|
+
if (!fiber.isFirstRender && index >= fiber.cells.length) throw new Error("Rendered more hooks than during the previous render. Hooks must be called in the exact same order in every render.");
|
|
62
|
+
cell = createReducerCell(fiber, reducer, initialArg, initFn, eagerDispatch);
|
|
63
|
+
fiber.cells[index] = cell;
|
|
64
|
+
} else {
|
|
65
|
+
if (existing.type !== "reducer") throw new Error("Hook order changed between renders");
|
|
66
|
+
cell = existing;
|
|
67
|
+
}
|
|
68
|
+
const queue = cell.queue;
|
|
69
|
+
if (queue !== null) {
|
|
70
|
+
const sameReducer = reducer === cell.reducer;
|
|
71
|
+
for (let i = 0; i < queue.length; i++) {
|
|
72
|
+
const item = queue[i];
|
|
73
|
+
if (!item.hasEagerState || !sameReducer) {
|
|
74
|
+
item.eagerState = reducer(cell.workInProgress, item.action);
|
|
75
|
+
item.hasEagerState = true;
|
|
76
|
+
if (isDevelopment && fiber.devStrictMode) item.eagerState = reducer(cell.workInProgress, item.action);
|
|
77
|
+
} else if (isDevelopment && fiber.devStrictMode) reducer(cell.workInProgress, item.action);
|
|
78
|
+
item.queued = false;
|
|
79
|
+
cell.workInProgress = item.eagerState;
|
|
80
|
+
}
|
|
81
|
+
cell.queue = null;
|
|
82
|
+
}
|
|
83
|
+
cell.reducer = reducer;
|
|
84
|
+
if (cell.renderQueue !== null || getDerivedState !== void 0) {
|
|
85
|
+
let derived = cell.workInProgress;
|
|
86
|
+
if (cell.renderQueue !== null) {
|
|
87
|
+
for (const action of cell.renderQueue) derived = reducer(derived, action);
|
|
88
|
+
cell.renderQueue = null;
|
|
89
|
+
fiber.renderPendingCells?.delete(cell);
|
|
90
|
+
}
|
|
91
|
+
if (getDerivedState) {
|
|
92
|
+
let changed;
|
|
93
|
+
let passes = 0;
|
|
94
|
+
do {
|
|
95
|
+
if (++passes > 25) throw new Error("Too many derivations. getDerivedState must reach a fixpoint; tap limits the number of iterations to prevent an infinite loop.");
|
|
96
|
+
const result = getDerivedState(derived);
|
|
97
|
+
changed = !Object.is(result, derived);
|
|
98
|
+
derived = result;
|
|
99
|
+
} while (changed);
|
|
100
|
+
}
|
|
101
|
+
if (!Object.is(derived, cell.workInProgress)) {
|
|
102
|
+
markCellDirty(fiber, cell);
|
|
103
|
+
cell.workInProgress = derived;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return [cell.workInProgress, cell.dispatch];
|
|
107
|
+
}
|
|
108
|
+
function useReducer(reducer, initialArg, init) {
|
|
109
|
+
return useReducerImpl(reducer, void 0, initialArg, init, false);
|
|
110
|
+
}
|
|
111
|
+
function useEagerReducer(reducer, initialArg, init) {
|
|
112
|
+
return useReducerImpl(reducer, void 0, initialArg, init, true);
|
|
113
|
+
}
|
|
114
|
+
function useReducerWithDerivedState(reducer, getDerivedState, initialArg, init) {
|
|
115
|
+
return useReducerImpl(reducer, getDerivedState, initialArg, init, true);
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
export { useEagerReducer, useReducer, useReducerWithDerivedState };
|
|
119
|
+
|
|
120
|
+
//# sourceMappingURL=useReducer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useReducer.js","names":[],"sources":["../../src/react-hooks/useReducer.ts"],"sourcesContent":["import { isDevelopment } from \"../core/helpers/env\";\nimport {\n getCurrentResourceFiber,\n peekResourceFiber,\n} from \"../core/helpers/execution-context\";\nimport type { Cell, ChangelogRecord, ResourceFiber } from \"../core/types\";\nimport { applyChangelogRecord, markCellDirty } from \"../core/helpers/root\";\n\ntype Dispatch<A> = (action: A) => void;\n\nconst dispatchOnFiber = (\n fiber: ResourceFiber<any, any>,\n callback: () => ChangelogRecord | null,\n): void => {\n if (fiber.isNeverMounted) {\n throw new Error(\"Resource updated before mount\");\n }\n\n fiber.root.dispatchUpdate(() => {\n const record = callback();\n if (record) {\n applyChangelogRecord(record);\n fiber.root.changelog.push(record);\n return true;\n }\n return false;\n });\n};\n\nconst createReducerCell = (\n fiber: ResourceFiber<any, any>,\n reducer: (state: any, action: any) => any,\n initialArg: any,\n initFn: ((arg: any) => any) | undefined,\n eagerDispatch: boolean,\n): Cell & { type: \"reducer\" } => {\n const initialState = initFn ? initFn(initialArg) : initialArg;\n\n if (isDevelopment && fiber.devStrictMode && initFn) {\n void initFn(initialArg);\n }\n\n const cell: Cell & { type: \"reducer\" } = {\n type: \"reducer\",\n queue: null,\n renderQueue: null,\n workInProgress: initialState,\n current: initialState,\n reducer,\n dispatch: (action) => {\n const currentFiber = peekResourceFiber();\n if (currentFiber !== null) {\n if (currentFiber !== fiber)\n throw new Error(\n \"Cannot update a resource while rendering a different resource.\",\n );\n\n (fiber.renderPendingCells ??= new Set()).add(cell);\n (cell.renderQueue ??= []).push(action);\n } else {\n const record: ChangelogRecord = {\n fiber,\n cell,\n action,\n hasEagerState: false,\n eagerState: undefined,\n queued: false,\n };\n\n dispatchOnFiber(fiber, () => {\n if (\n eagerDispatch &&\n fiber.root.dirtyCells.size === 0 &&\n !record.hasEagerState\n ) {\n record.eagerState = reducer(cell.workInProgress, action);\n record.hasEagerState = true;\n\n if (Object.is(cell.current, record.eagerState)) return null;\n }\n\n return record;\n });\n }\n },\n };\n return cell;\n};\n\nfunction useReducerImpl<S, A, I, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: ((state: S) => R) | undefined,\n initialArg: S | I,\n initFn: ((arg: I) => S) | undefined,\n eagerDispatch: boolean,\n): [R, Dispatch<A>] {\n const fiber = getCurrentResourceFiber();\n const index = fiber.currentIndex++;\n\n const existing = fiber.cells[index];\n let cell: Cell & { type: \"reducer\" };\n if (existing === undefined) {\n if (!fiber.isFirstRender && index >= fiber.cells.length) {\n throw new Error(\n \"Rendered more hooks than during the previous render. \" +\n \"Hooks must be called in the exact same order in every render.\",\n );\n }\n cell = createReducerCell(fiber, reducer, initialArg, initFn, eagerDispatch);\n fiber.cells[index] = cell;\n } else {\n if (existing.type !== \"reducer\") {\n throw new Error(\"Hook order changed between renders\");\n }\n cell = existing;\n }\n\n const queue = cell.queue;\n if (queue !== null) {\n const sameReducer = reducer === cell.reducer;\n\n // The drain consumes entries: a re-render of the same uncommitted lineage\n // sees an empty queue and must not re-apply them. Rollback replays them\n // into the queue via the changelog.\n for (let i = 0; i < queue.length; i++) {\n const item = queue[i]!;\n if (!item.hasEagerState || !sameReducer) {\n item.eagerState = reducer(cell.workInProgress, item.action);\n item.hasEagerState = true;\n\n if (isDevelopment && fiber.devStrictMode) {\n // React keeps the strict re-invocation's result for render-computed\n // actions (unlike eager-computed ones, whose ghost is discarded).\n item.eagerState = reducer(cell.workInProgress, item.action);\n }\n } else if (isDevelopment && fiber.devStrictMode) {\n void reducer(cell.workInProgress, item.action);\n }\n\n item.queued = false;\n cell.workInProgress = item.eagerState;\n }\n cell.queue = null;\n }\n cell.reducer = reducer;\n\n if (cell.renderQueue !== null || getDerivedState !== undefined) {\n let derived = cell.workInProgress;\n if (cell.renderQueue !== null) {\n for (const action of cell.renderQueue) {\n derived = reducer(derived, action);\n }\n\n cell.renderQueue = null;\n fiber.renderPendingCells?.delete(cell);\n }\n\n if (getDerivedState) {\n let changed;\n let passes = 0;\n do {\n if (++passes > 25) {\n throw new Error(\n \"Too many derivations. getDerivedState must reach a fixpoint; \" +\n \"tap limits the number of iterations to prevent an infinite loop.\",\n );\n }\n const result = getDerivedState(derived);\n changed = !Object.is(result, derived);\n derived = result;\n } while (changed);\n }\n\n if (!Object.is(derived, cell.workInProgress)) {\n markCellDirty(fiber, cell);\n cell.workInProgress = derived;\n }\n }\n\n return [cell.workInProgress, cell.dispatch];\n}\n\nexport function useReducer<S, A>(\n reducer: (state: S, action: A) => S,\n initialState: S,\n): [S, Dispatch<A>];\nexport function useReducer<S, A, I>(\n reducer: (state: S, action: A) => S,\n initialArg: I,\n init: (arg: I) => S,\n): [S, Dispatch<A>];\nexport function useReducer<S, A, I>(\n reducer: (state: S, action: A) => S,\n initialArg: S | I,\n init?: (arg: I) => S,\n): [S, Dispatch<A>] {\n return useReducerImpl(\n reducer,\n undefined,\n initialArg as S,\n init as ((arg: S) => S) | undefined,\n false,\n );\n}\n\n/** @internal useState's entry point: eager dispatch, like React's basic state reducer. */\nexport function useEagerReducer<S, A>(\n reducer: (state: S, action: A) => S,\n initialState: S,\n): [S, Dispatch<A>];\nexport function useEagerReducer<S, A, I>(\n reducer: (state: S, action: A) => S,\n initialArg: I,\n init: (arg: I) => S,\n): [S, Dispatch<A>];\nexport function useEagerReducer<S, A, I>(\n reducer: (state: S, action: A) => S,\n initialArg: S | I,\n init?: (arg: I) => S,\n): [S, Dispatch<A>] {\n return useReducerImpl(\n reducer,\n undefined,\n initialArg as S,\n init as ((arg: S) => S) | undefined,\n true,\n );\n}\n\n/**\n * @internal Backs useMemo and useMemoCache: a reducer cell whose state is\n * recomputed during render via getDerivedState. Not part of the public API;\n * user-facing state adjustment during render uses render-phase updates\n * (setState during render), like React.\n */\nexport function useReducerWithDerivedState<S, A, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: (state: S) => R,\n initialState: S,\n): [R, Dispatch<A>];\nexport function useReducerWithDerivedState<S, A, I, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: (state: S) => R,\n initialArg: I,\n init: (arg: I) => S,\n): [R, Dispatch<A>];\nexport function useReducerWithDerivedState<S, A, I, R extends S>(\n reducer: (state: S, action: A) => S,\n getDerivedState: (state: S) => R,\n initialArg: I,\n init?: (arg: I) => S,\n): [R, Dispatch<A>] {\n return useReducerImpl(reducer, getDerivedState, initialArg, init, true);\n}\n"],"mappings":";;;;AAUA,MAAM,mBACJ,OACA,aACS;CACT,IAAI,MAAM,gBACR,MAAM,IAAI,MAAM,+BAA+B;CAGjD,MAAM,KAAK,qBAAqB;EAC9B,MAAM,SAAS,SAAS;EACxB,IAAI,QAAQ;GACV,qBAAqB,MAAM;GAC3B,MAAM,KAAK,UAAU,KAAK,MAAM;GAChC,OAAO;EACT;EACA,OAAO;CACT,CAAC;AACH;AAEA,MAAM,qBACJ,OACA,SACA,YACA,QACA,kBAC+B;CAC/B,MAAM,eAAe,SAAS,OAAO,UAAU,IAAI;CAEnD,IAAI,iBAAiB,MAAM,iBAAiB,QAC1C,OAAY,UAAU;CAGxB,MAAM,OAAmC;EACvC,MAAM;EACN,OAAO;EACP,aAAa;EACb,gBAAgB;EAChB,SAAS;EACT;EACA,WAAW,WAAW;GACpB,MAAM,eAAe,kBAAkB;GACvC,IAAI,iBAAiB,MAAM;IACzB,IAAI,iBAAiB,OACnB,MAAM,IAAI,MACR,gEACF;IAEF,CAAC,MAAM,uCAAuB,IAAI,IAAI,EAAA,CAAG,IAAI,IAAI;IACjD,CAAC,KAAK,gBAAgB,CAAC,EAAA,CAAG,KAAK,MAAM;GACvC,OAAO;IACL,MAAM,SAA0B;KAC9B;KACA;KACA;KACA,eAAe;KACf,YAAY,KAAA;KACZ,QAAQ;IACV;IAEA,gBAAgB,aAAa;KAC3B,IACE,iBACA,MAAM,KAAK,WAAW,SAAS,KAC/B,CAAC,OAAO,eACR;MACA,OAAO,aAAa,QAAQ,KAAK,gBAAgB,MAAM;MACvD,OAAO,gBAAgB;MAEvB,IAAI,OAAO,GAAG,KAAK,SAAS,OAAO,UAAU,GAAG,OAAO;KACzD;KAEA,OAAO;IACT,CAAC;GACH;EACF;CACF;CACA,OAAO;AACT;AAEA,SAAS,eACP,SACA,iBACA,YACA,QACA,eACkB;CAClB,MAAM,QAAQ,wBAAwB;CACtC,MAAM,QAAQ,MAAM;CAEpB,MAAM,WAAW,MAAM,MAAM;CAC7B,IAAI;CACJ,IAAI,aAAa,KAAA,GAAW;EAC1B,IAAI,CAAC,MAAM,iBAAiB,SAAS,MAAM,MAAM,QAC/C,MAAM,IAAI,MACR,oHAEF;EAEF,OAAO,kBAAkB,OAAO,SAAS,YAAY,QAAQ,aAAa;EAC1E,MAAM,MAAM,SAAS;CACvB,OAAO;EACL,IAAI,SAAS,SAAS,WACpB,MAAM,IAAI,MAAM,oCAAoC;EAEtD,OAAO;CACT;CAEA,MAAM,QAAQ,KAAK;CACnB,IAAI,UAAU,MAAM;EAClB,MAAM,cAAc,YAAY,KAAK;EAKrC,KAAK,IAAI,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;GACrC,MAAM,OAAO,MAAM;GACnB,IAAI,CAAC,KAAK,iBAAiB,CAAC,aAAa;IACvC,KAAK,aAAa,QAAQ,KAAK,gBAAgB,KAAK,MAAM;IAC1D,KAAK,gBAAgB;IAErB,IAAI,iBAAiB,MAAM,eAGzB,KAAK,aAAa,QAAQ,KAAK,gBAAgB,KAAK,MAAM;GAE9D,OAAO,IAAI,iBAAiB,MAAM,eAChC,QAAa,KAAK,gBAAgB,KAAK,MAAM;GAG/C,KAAK,SAAS;GACd,KAAK,iBAAiB,KAAK;EAC7B;EACA,KAAK,QAAQ;CACf;CACA,KAAK,UAAU;CAEf,IAAI,KAAK,gBAAgB,QAAQ,oBAAoB,KAAA,GAAW;EAC9D,IAAI,UAAU,KAAK;EACnB,IAAI,KAAK,gBAAgB,MAAM;GAC7B,KAAK,MAAM,UAAU,KAAK,aACxB,UAAU,QAAQ,SAAS,MAAM;GAGnC,KAAK,cAAc;GACnB,MAAM,oBAAoB,OAAO,IAAI;EACvC;EAEA,IAAI,iBAAiB;GACnB,IAAI;GACJ,IAAI,SAAS;GACb,GAAG;IACD,IAAI,EAAE,SAAS,IACb,MAAM,IAAI,MACR,+HAEF;IAEF,MAAM,SAAS,gBAAgB,OAAO;IACtC,UAAU,CAAC,OAAO,GAAG,QAAQ,OAAO;IACpC,UAAU;GACZ,SAAS;EACX;EAEA,IAAI,CAAC,OAAO,GAAG,SAAS,KAAK,cAAc,GAAG;GAC5C,cAAc,OAAO,IAAI;GACzB,KAAK,iBAAiB;EACxB;CACF;CAEA,OAAO,CAAC,KAAK,gBAAgB,KAAK,QAAQ;AAC5C;AAWA,SAAgB,WACd,SACA,YACA,MACkB;CAClB,OAAO,eACL,SACA,KAAA,GACA,YACA,MACA,KACF;AACF;AAYA,SAAgB,gBACd,SACA,YACA,MACkB;CAClB,OAAO,eACL,SACA,KAAA,GACA,YACA,MACA,IACF;AACF;AAmBA,SAAgB,2BACd,SACA,iBACA,YACA,MACkB;CAClB,OAAO,eAAe,SAAS,iBAAiB,YAAY,MAAM,IAAI;AACxE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRef.d.ts","names":[],"sources":["../../src/react-hooks/useRef.ts"],"mappings":";kBAEiB,MAAA;EAAA,UACE,SAAA;IACf,OAAA,EAAS,CAAC;EAAA;AAAA;AAAA,iBAIE,MAAA,IAAU,YAAA,EAAc,CAAA,GAAI,MAAA,CAAO,SAAA,CAAU,CAAA;AAAA,iBAC7C,MAAA,mBAAyB,MAAA,CAAO,SAAS,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRef.js","names":[],"sources":["../../src/react-hooks/useRef.ts"],"sourcesContent":["import { useState } from \"./useState\";\n\nexport namespace useRef {\n export interface RefObject<T> {\n current: T;\n }\n}\n\nexport function useRef<T>(initialValue: T): useRef.RefObject<T>;\nexport function useRef<T = undefined>(): useRef.RefObject<T | undefined>;\nexport function useRef<T>(initialValue?: T): useRef.RefObject<T | undefined> {\n const [state] = useState(() => ({\n current: initialValue,\n }));\n return state;\n}\n"],"mappings":";;AAUA,SAAgB,OAAU,cAAmD;CAC3E,MAAM,CAAC,SAAS,gBAAgB,EAC9B,SAAS,aACX,EAAE;CACF,OAAO;AACT"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useState.d.ts","names":[],"sources":["../../src/react-hooks/useState.ts"],"mappings":";kBAEiB,QAAA;EAAA,KACH,YAAA,MAAkB,CAAA,KAAM,IAAA,EAAM,CAAA,KAAM,CAAA;AAAA;AAAA,iBAclC,QAAA,oBACd,CAAA,eACC,OAAA,EAAS,QAAA,CAAS,YAAA,CAAa,CAAA;AAAA,iBAElB,QAAA,IACd,OAAA,EAAS,CAAA,UAAW,CAAA,KAClB,CAAA,GAAI,OAAA,EAAS,QAAA,CAAS,YAAA,CAAa,CAAA"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//#region src/hooks/useState.ts
|
|
1
|
+
import { useEagerReducer } from "./useReducer.js";
|
|
2
|
+
//#region src/react-hooks/useState.ts
|
|
3
3
|
const stateReducer = (state, action) => typeof action === "function" ? action(state) : action;
|
|
4
4
|
const stateInit = (initial) => typeof initial === "function" ? initial() : initial;
|
|
5
5
|
function useState(initial) {
|
|
6
|
-
return
|
|
6
|
+
return useEagerReducer(stateReducer, initial, stateInit);
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
9
9
|
export { useState };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useState.js","names":[],"sources":["../../src/react-hooks/useState.ts"],"sourcesContent":["import { useEagerReducer } from \"./useReducer\";\n\nexport namespace useState {\n export type StateUpdater<S> = S | ((prev: S) => S);\n}\n\nconst stateReducer = <S>(\n state: S | undefined,\n action: useState.StateUpdater<S>,\n): S =>\n typeof action === \"function\"\n ? (action as (prev: S | undefined) => S)(state)\n : action;\n\nconst stateInit = <S>(initial: S | (() => S)): S =>\n typeof initial === \"function\" ? (initial as () => S)() : initial;\n\nexport function useState<S = undefined>(): [\n S | undefined,\n (updater: useState.StateUpdater<S>) => void,\n];\nexport function useState<S>(\n initial: S | (() => S),\n): [S, (updater: useState.StateUpdater<S>) => void];\nexport function useState<S>(\n initial?: S | (() => S),\n): [S | undefined, (updater: useState.StateUpdater<S>) => void] {\n return useEagerReducer(stateReducer, initial, stateInit);\n}\n"],"mappings":";;AAMA,MAAM,gBACJ,OACA,WAEA,OAAO,WAAW,aACb,OAAsC,KAAK,IAC5C;AAEN,MAAM,aAAgB,YACpB,OAAO,YAAY,aAAc,QAAoB,IAAI;AAS3D,SAAgB,SACd,SAC8D;CAC9D,OAAO,gBAAgB,cAAc,SAAS,SAAS;AACzD"}
|