@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
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useEffect.js","names":[],"sources":["../../src/hooks/useEffect.ts"],"sourcesContent":["import type { Cell } from \"../core/types\";\nimport { depsShallowEqual } from \"./utils/depsShallowEqual\";\nimport { useCell, registerRenderMountTask } from \"./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 const errors: unknown[] = [];\n\n try {\n cell.cleanup?.();\n } catch (error) {\n errors.push(error);\n } finally {\n cell.cleanup = undefined;\n }\n\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 } catch (error) {\n errors.push(error);\n }\n\n cell.deps = deps;\n\n if (errors.length > 0) {\n if (errors.length === 1) {\n throw errors[0];\n } else {\n for (const error of errors) {\n console.error(error);\n }\n throw new AggregateError(errors, \"Errors during commit\");\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,8BAA8B;EAC5B,MAAM,SAAoB,CAAC;EAE3B,IAAI;GACF,KAAK,UAAU;EACjB,SAAS,OAAO;GACd,OAAO,KAAK,KAAK;EACnB,UAAU;GACR,KAAK,UAAU,KAAA;EACjB;EAEA,IAAI;GACF,MAAM,UAAU,OAAO;GAEvB,IAAI,YAAY,KAAA,KAAa,OAAO,YAAY,YAC9C,MAAM,IAAI,MACR,kFACe,OAAO,SACxB;GAGF,KAAK,UAAU;EACjB,SAAS,OAAO;GACd,OAAO,KAAK,KAAK;EACnB;EAEA,KAAK,OAAO;EAEZ,IAAI,OAAO,SAAS,GAClB,IAAI,OAAO,WAAW,GACpB,MAAM,OAAO;OACR;GACL,KAAK,MAAM,SAAS,QAClB,QAAQ,MAAM,KAAK;GAErB,MAAM,IAAI,eAAe,QAAQ,sBAAsB;EACzD;CAEJ,CAAC;AACH"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useEffectEvent.d.ts","names":[],"sources":["../../src/hooks/useEffectEvent.ts"],"mappings":";;AAqBA;;;;;;;;;;AAEI;;;;iBAFY,cAAA,eAA6B,IAAA,iBAC3C,QAAA,EAAU,CAAA,GACT,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useEffectEvent.js","names":[],"sources":["../../src/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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useMemo.d.ts","names":[],"sources":["../../src/hooks/useMemo.ts"],"mappings":";cAWa,OAAA,MAAc,EAAA,QAAU,CAAA,EAAG,IAAA,yBAA2B,CAkBlE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useMemo.js","names":[],"sources":["../../src/hooks/useMemo.ts"],"sourcesContent":["import { isDevelopment } from \"../core/helpers/env\";\nimport { getCurrentResourceFiber } from \"../core/helpers/execution-context\";\nimport { useReducerWithDerivedState } from \"./useReducer\";\nimport { depsShallowEqual } from \"./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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useMemoCache.d.ts","names":[],"sources":["../../src/hooks/useMemoCache.ts"],"mappings":";;AASA;;;;cAAa,YAAA,GAAgB,IAAY"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useMemoCache.js","names":[],"sources":["../../src/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"}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
//#region src/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
|
-
/**
|
|
6
|
-
* @deprecated experimental — a `getDerivedStateFromProps` replacement for
|
|
7
|
-
* resources: adjust state in response to props without setting during render.
|
|
8
|
-
* Tap-only for now (call it inside a resource render, not a React component) and
|
|
9
|
-
* may change before stabilizing.
|
|
10
|
-
*/
|
|
11
|
-
declare function useReducerWithDerivedState<S, A, R extends S>(reducer: (state: S, action: A) => S, getDerivedState: (state: S) => R, initialState: S): [R, Dispatch<A>];
|
|
12
|
-
/**
|
|
13
|
-
* @deprecated experimental — a `getDerivedStateFromProps` replacement for
|
|
14
|
-
* resources: adjust state in response to props without setting during render.
|
|
15
|
-
* Tap-only for now (call it inside a resource render, not a React component) and
|
|
16
|
-
* may change before stabilizing.
|
|
17
|
-
*/
|
|
18
|
-
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>];
|
|
19
|
-
//#endregion
|
|
20
|
-
export { useReducer, useReducerWithDerivedState };
|
|
21
|
-
//# sourceMappingURL=useReducer.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useReducer.d.ts","names":[],"sources":["../../src/hooks/useReducer.ts"],"mappings":";KAMK,QAAA,OAAe,MAAA,EAAQ,CAAC;AAAA,iBAqGb,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;;;;;;;iBAoBA,0BAAA,iBAA2C,CAAA,EACzD,OAAA,GAAU,KAAA,EAAO,CAAA,EAAG,MAAA,EAAQ,CAAA,KAAM,CAAA,EAClC,eAAA,GAAkB,KAAA,EAAO,CAAA,KAAM,CAAA,EAC/B,YAAA,EAAc,CAAA,IACZ,CAAA,EAAG,QAAA,CAAS,CAAA;AAhChB;;;;;;AAAA,iBAuCgB,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"}
|
package/dist/hooks/useReducer.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { isDevelopment } from "../core/helpers/env.js";
|
|
2
|
-
import { getCurrentResourceFiber } from "../core/helpers/execution-context.js";
|
|
3
|
-
import { markCellDirty } from "../core/helpers/root.js";
|
|
4
|
-
import { useCell } from "./utils/useCell.js";
|
|
5
|
-
//#region src/hooks/useReducer.ts
|
|
6
|
-
const dispatchOnFiber = (fiber, callback) => {
|
|
7
|
-
if (fiber.renderContext) throw new Error("Resource updated during render");
|
|
8
|
-
if (fiber.isNeverMounted) throw new Error("Resource updated before mount");
|
|
9
|
-
fiber.root.dispatchUpdate(() => {
|
|
10
|
-
const result = callback();
|
|
11
|
-
if (result) {
|
|
12
|
-
result();
|
|
13
|
-
fiber.root.changelog.push(result);
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
return false;
|
|
17
|
-
});
|
|
18
|
-
};
|
|
19
|
-
function useReducerImpl(reducer, getDerivedState, initialArg, initFn) {
|
|
20
|
-
const cell = useCell("reducer", () => {
|
|
21
|
-
const fiber = getCurrentResourceFiber();
|
|
22
|
-
const initialState = initFn ? initFn(initialArg) : initialArg;
|
|
23
|
-
if (isDevelopment && fiber.devStrictMode && initFn) initFn(initialArg);
|
|
24
|
-
return {
|
|
25
|
-
type: "reducer",
|
|
26
|
-
queue: /* @__PURE__ */ new Set(),
|
|
27
|
-
dirty: false,
|
|
28
|
-
workInProgress: initialState,
|
|
29
|
-
current: initialState,
|
|
30
|
-
reducer,
|
|
31
|
-
dispatch: (action) => {
|
|
32
|
-
const entry = {
|
|
33
|
-
action,
|
|
34
|
-
hasEagerState: false,
|
|
35
|
-
eagerState: void 0
|
|
36
|
-
};
|
|
37
|
-
dispatchOnFiber(fiber, () => {
|
|
38
|
-
if (fiber.root.dirtyCells.length === 0 && !entry.hasEagerState) {
|
|
39
|
-
entry.eagerState = reducer(cell.workInProgress, action);
|
|
40
|
-
entry.hasEagerState = true;
|
|
41
|
-
if (Object.is(cell.current, entry.eagerState)) return null;
|
|
42
|
-
}
|
|
43
|
-
return () => {
|
|
44
|
-
markCellDirty(fiber, cell);
|
|
45
|
-
cell.queue.add(entry);
|
|
46
|
-
};
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
const fiber = getCurrentResourceFiber();
|
|
52
|
-
const sameReducer = reducer === cell.reducer;
|
|
53
|
-
cell.reducer = reducer;
|
|
54
|
-
for (const item of cell.queue) {
|
|
55
|
-
if (!item.hasEagerState || !sameReducer) {
|
|
56
|
-
item.eagerState = reducer(cell.workInProgress, item.action);
|
|
57
|
-
item.hasEagerState = true;
|
|
58
|
-
}
|
|
59
|
-
if (isDevelopment && fiber.devStrictMode) reducer(cell.workInProgress, item.action);
|
|
60
|
-
cell.workInProgress = item.eagerState;
|
|
61
|
-
}
|
|
62
|
-
cell.queue.clear();
|
|
63
|
-
if (getDerivedState) {
|
|
64
|
-
const derived = getDerivedState(cell.workInProgress);
|
|
65
|
-
if (!Object.is(derived, cell.workInProgress)) {
|
|
66
|
-
markCellDirty(fiber, cell);
|
|
67
|
-
cell.workInProgress = derived;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return [cell.workInProgress, cell.dispatch];
|
|
71
|
-
}
|
|
72
|
-
function useReducer(reducer, initialArg, init) {
|
|
73
|
-
return useReducerImpl(reducer, void 0, initialArg, init);
|
|
74
|
-
}
|
|
75
|
-
function useReducerWithDerivedState(reducer, getDerivedState, initialArg, init) {
|
|
76
|
-
return useReducerImpl(reducer, getDerivedState, initialArg, init);
|
|
77
|
-
}
|
|
78
|
-
//#endregion
|
|
79
|
-
export { useReducer, useReducerWithDerivedState };
|
|
80
|
-
|
|
81
|
-
//# sourceMappingURL=useReducer.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useReducer.js","names":[],"sources":["../../src/hooks/useReducer.ts"],"sourcesContent":["import { isDevelopment } from \"../core/helpers/env\";\nimport { getCurrentResourceFiber } from \"../core/helpers/execution-context\";\nimport type { ReducerQueueEntry, ResourceFiber } from \"../core/types\";\nimport { markCellDirty } from \"../core/helpers/root\";\nimport { useCell } from \"./utils/useCell\";\n\ntype Dispatch<A> = (action: A) => void;\n\nconst dispatchOnFiber = (\n fiber: ResourceFiber<any, any>,\n callback: () => (() => void) | null,\n): void => {\n if (fiber.renderContext) {\n throw new Error(\"Resource updated during render\");\n }\n if (fiber.isNeverMounted) {\n throw new Error(\"Resource updated before mount\");\n }\n\n fiber.root.dispatchUpdate(() => {\n const result = callback();\n if (result) {\n result();\n fiber.root.changelog.push(result);\n return true;\n }\n return false;\n });\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): [R, Dispatch<A>] {\n const cell = useCell(\"reducer\", () => {\n const fiber = getCurrentResourceFiber();\n\n // First render: compute initial state\n const initialState = initFn ? initFn(initialArg as I) : initialArg;\n\n if (isDevelopment && fiber.devStrictMode && initFn) {\n void initFn(initialArg as I);\n }\n\n return {\n type: \"reducer\",\n queue: new Set(),\n dirty: false,\n workInProgress: initialState,\n current: initialState,\n reducer,\n dispatch: (action: A) => {\n const entry: ReducerQueueEntry = {\n action,\n hasEagerState: false,\n eagerState: undefined,\n };\n\n dispatchOnFiber(fiber, () => {\n if (fiber.root.dirtyCells.length === 0 && !entry.hasEagerState) {\n entry.eagerState = reducer(cell.workInProgress, action);\n entry.hasEagerState = true;\n\n if (Object.is(cell.current, entry.eagerState)) return null;\n }\n\n return () => {\n markCellDirty(fiber, cell);\n cell.queue.add(entry);\n };\n });\n },\n };\n });\n\n const fiber = getCurrentResourceFiber();\n const sameReducer = reducer === cell.reducer;\n cell.reducer = reducer;\n\n for (const item of cell.queue) {\n if (!item.hasEagerState || !sameReducer) {\n item.eagerState = reducer(cell.workInProgress, item.action);\n item.hasEagerState = true;\n }\n\n if (isDevelopment && fiber.devStrictMode) {\n void reducer(cell.workInProgress, item.action);\n }\n\n cell.workInProgress = item.eagerState;\n }\n cell.queue.clear();\n\n if (getDerivedState) {\n const derived = getDerivedState(cell.workInProgress);\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 );\n}\n\n/**\n * @deprecated experimental — a `getDerivedStateFromProps` replacement for\n * resources: adjust state in response to props without setting during render.\n * Tap-only for now (call it inside a resource render, not a React component) and\n * may change before stabilizing.\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>];\n/**\n * @deprecated experimental — a `getDerivedStateFromProps` replacement for\n * resources: adjust state in response to props without setting during render.\n * Tap-only for now (call it inside a resource render, not a React component) and\n * may change before stabilizing.\n */\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);\n}\n"],"mappings":";;;;;AAQA,MAAM,mBACJ,OACA,aACS;CACT,IAAI,MAAM,eACR,MAAM,IAAI,MAAM,gCAAgC;CAElD,IAAI,MAAM,gBACR,MAAM,IAAI,MAAM,+BAA+B;CAGjD,MAAM,KAAK,qBAAqB;EAC9B,MAAM,SAAS,SAAS;EACxB,IAAI,QAAQ;GACV,OAAO;GACP,MAAM,KAAK,UAAU,KAAK,MAAM;GAChC,OAAO;EACT;EACA,OAAO;CACT,CAAC;AACH;AAEA,SAAS,eACP,SACA,iBACA,YACA,QACkB;CAClB,MAAM,OAAO,QAAQ,iBAAiB;EACpC,MAAM,QAAQ,wBAAwB;EAGtC,MAAM,eAAe,SAAS,OAAO,UAAe,IAAI;EAExD,IAAI,iBAAiB,MAAM,iBAAiB,QAC1C,OAAY,UAAe;EAG7B,OAAO;GACL,MAAM;GACN,uBAAO,IAAI,IAAI;GACf,OAAO;GACP,gBAAgB;GAChB,SAAS;GACT;GACA,WAAW,WAAc;IACvB,MAAM,QAA2B;KAC/B;KACA,eAAe;KACf,YAAY,KAAA;IACd;IAEA,gBAAgB,aAAa;KAC3B,IAAI,MAAM,KAAK,WAAW,WAAW,KAAK,CAAC,MAAM,eAAe;MAC9D,MAAM,aAAa,QAAQ,KAAK,gBAAgB,MAAM;MACtD,MAAM,gBAAgB;MAEtB,IAAI,OAAO,GAAG,KAAK,SAAS,MAAM,UAAU,GAAG,OAAO;KACxD;KAEA,aAAa;MACX,cAAc,OAAO,IAAI;MACzB,KAAK,MAAM,IAAI,KAAK;KACtB;IACF,CAAC;GACH;EACF;CACF,CAAC;CAED,MAAM,QAAQ,wBAAwB;CACtC,MAAM,cAAc,YAAY,KAAK;CACrC,KAAK,UAAU;CAEf,KAAK,MAAM,QAAQ,KAAK,OAAO;EAC7B,IAAI,CAAC,KAAK,iBAAiB,CAAC,aAAa;GACvC,KAAK,aAAa,QAAQ,KAAK,gBAAgB,KAAK,MAAM;GAC1D,KAAK,gBAAgB;EACvB;EAEA,IAAI,iBAAiB,MAAM,eACzB,QAAa,KAAK,gBAAgB,KAAK,MAAM;EAG/C,KAAK,iBAAiB,KAAK;CAC7B;CACA,KAAK,MAAM,MAAM;CAEjB,IAAI,iBAAiB;EACnB,MAAM,UAAU,gBAAgB,KAAK,cAAc;EAEnD,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,IACF;AACF;AAyBA,SAAgB,2BACd,SACA,iBACA,YACA,MACkB;CAClB,OAAO,eAAe,SAAS,iBAAiB,YAAY,IAAI;AAClE"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRef.d.ts","names":[],"sources":["../../src/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"}
|
package/dist/hooks/useRef.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useRef.js","names":[],"sources":["../../src/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"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { ResourceElement } from "../core/types.js";
|
|
2
|
-
|
|
3
|
-
//#region src/hooks/useResourceRoot.d.ts
|
|
4
|
-
declare namespace useResourceRoot {
|
|
5
|
-
type Unsubscribe = () => void;
|
|
6
|
-
interface SubscribableResource<TState> {
|
|
7
|
-
/**
|
|
8
|
-
* Get the current state of the store.
|
|
9
|
-
*/
|
|
10
|
-
getValue(): TState;
|
|
11
|
-
/**
|
|
12
|
-
* Subscribe to the store.
|
|
13
|
-
*/
|
|
14
|
-
subscribe(listener: () => void): Unsubscribe;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
declare const useResourceRoot: <TState>(element: ResourceElement<TState>) => useResourceRoot.SubscribableResource<TState>;
|
|
18
|
-
//#endregion
|
|
19
|
-
export { useResourceRoot };
|
|
20
|
-
//# sourceMappingURL=useResourceRoot.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useResourceRoot.d.ts","names":[],"sources":["../../src/hooks/useResourceRoot.ts"],"mappings":";;;kBAmBiB,eAAA;EAAA,KACH,WAAA;EAAA,UAEK,oBAAA;IAHa;;;IAO5B,QAAA,IAAY,MAAA;IAJG;;;IASf,SAAA,CAAU,QAAA,eAAuB,WAAW;EAAA;AAAA;AAAA,cAMnC,eAAA,WACX,OAAA,EAAS,eAAA,CAAgB,MAAA,MACxB,eAAA,CAAgB,oBAAA,CAAqB,MAAA"}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { isDevelopment } from "../core/helpers/env.js";
|
|
2
|
-
import { commitRoot, createResourceFiberRoot, setRootVersion } from "../core/helpers/root.js";
|
|
3
|
-
import { useRef } from "./useRef.js";
|
|
4
|
-
import { useMemo } from "./useMemo.js";
|
|
5
|
-
import { useEffect } from "./useEffect.js";
|
|
6
|
-
import { useEffectEvent } from "./useEffectEvent.js";
|
|
7
|
-
import { commitResourceFiber, createResourceFiber, renderResourceFiber, unmountResourceFiber } from "../core/ResourceFiber.js";
|
|
8
|
-
import { UpdateScheduler } from "../core/scheduler.js";
|
|
9
|
-
//#region src/hooks/useResourceRoot.ts
|
|
10
|
-
const useResourceRoot = (element) => {
|
|
11
|
-
const scheduler = useMemo(() => new UpdateScheduler(() => handleUpdate(null)), []);
|
|
12
|
-
const queue = useMemo(() => [], []);
|
|
13
|
-
const fiber = useMemo(() => {
|
|
14
|
-
element.key;
|
|
15
|
-
return createResourceFiber(element.type, createResourceFiberRoot((callback) => {
|
|
16
|
-
if (!scheduler.isDirty && !callback()) return;
|
|
17
|
-
queue.push(callback);
|
|
18
|
-
scheduler.markDirty();
|
|
19
|
-
}));
|
|
20
|
-
}, [
|
|
21
|
-
element.type,
|
|
22
|
-
element.key,
|
|
23
|
-
queue,
|
|
24
|
-
scheduler
|
|
25
|
-
]);
|
|
26
|
-
setRootVersion(fiber.root, fiber.root.committedVersion);
|
|
27
|
-
const render = renderResourceFiber(fiber, element.props);
|
|
28
|
-
const isMountedRef = useRef(false);
|
|
29
|
-
const committedPropsRef = useRef(element.props);
|
|
30
|
-
const valueRef = useRef(render.output);
|
|
31
|
-
const subscribers = useMemo(() => /* @__PURE__ */ new Set(), []);
|
|
32
|
-
const handleUpdate = useEffectEvent((render) => {
|
|
33
|
-
if (render === null) {
|
|
34
|
-
setRootVersion(fiber.root, 2);
|
|
35
|
-
setRootVersion(fiber.root, 1);
|
|
36
|
-
queue.forEach((callback) => {
|
|
37
|
-
if (isDevelopment && fiber.devStrictMode) callback();
|
|
38
|
-
callback();
|
|
39
|
-
});
|
|
40
|
-
if (isDevelopment && fiber.devStrictMode) renderResourceFiber(fiber, committedPropsRef.current);
|
|
41
|
-
render = renderResourceFiber(fiber, committedPropsRef.current);
|
|
42
|
-
}
|
|
43
|
-
if (scheduler.isDirty) throw new Error("Scheduler is dirty, this should never happen");
|
|
44
|
-
commitRoot(fiber.root);
|
|
45
|
-
queue.length = 0;
|
|
46
|
-
if (isMountedRef.current) commitResourceFiber(fiber, render);
|
|
47
|
-
if (scheduler.isDirty || valueRef.current === render.output) return;
|
|
48
|
-
valueRef.current = render.output;
|
|
49
|
-
subscribers.forEach((callback) => callback());
|
|
50
|
-
});
|
|
51
|
-
useEffect(() => {
|
|
52
|
-
isMountedRef.current = true;
|
|
53
|
-
return () => {
|
|
54
|
-
isMountedRef.current = false;
|
|
55
|
-
unmountResourceFiber(fiber);
|
|
56
|
-
};
|
|
57
|
-
}, [fiber]);
|
|
58
|
-
useEffect(() => {
|
|
59
|
-
committedPropsRef.current = render.props;
|
|
60
|
-
commitRoot(fiber.root);
|
|
61
|
-
commitResourceFiber(fiber, render);
|
|
62
|
-
if (scheduler.isDirty || valueRef.current === render.output) return;
|
|
63
|
-
valueRef.current = render.output;
|
|
64
|
-
subscribers.forEach((callback) => callback());
|
|
65
|
-
});
|
|
66
|
-
return useMemo(() => ({
|
|
67
|
-
getValue: () => valueRef.current,
|
|
68
|
-
subscribe: (listener) => {
|
|
69
|
-
subscribers.add(listener);
|
|
70
|
-
return () => subscribers.delete(listener);
|
|
71
|
-
}
|
|
72
|
-
}), [subscribers]);
|
|
73
|
-
};
|
|
74
|
-
//#endregion
|
|
75
|
-
export { useResourceRoot };
|
|
76
|
-
|
|
77
|
-
//# sourceMappingURL=useResourceRoot.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useResourceRoot.js","names":[],"sources":["../../src/hooks/useResourceRoot.ts"],"sourcesContent":["import {\n commitResourceFiber,\n createResourceFiber,\n renderResourceFiber,\n unmountResourceFiber,\n} from \"../core/ResourceFiber\";\nimport { UpdateScheduler } from \"../core/scheduler\";\nimport { useMemo } from \"./useMemo\";\nimport { useEffect } from \"./useEffect\";\nimport { useEffectEvent } from \"./useEffectEvent\";\nimport { useRef } from \"./useRef\";\nimport type { RenderResult, ResourceElement } from \"../core/types\";\nimport { isDevelopment } from \"../core/helpers/env\";\nimport {\n commitRoot,\n createResourceFiberRoot,\n setRootVersion,\n} from \"../core/helpers/root\";\n\nexport namespace useResourceRoot {\n export type Unsubscribe = () => void;\n\n export interface SubscribableResource<TState> {\n /**\n * Get the current state of the store.\n */\n getValue(): TState;\n\n /**\n * Subscribe to the store.\n */\n subscribe(listener: () => void): Unsubscribe;\n }\n}\n\n// The root is never reset, because rollbacks are not supported in useResourceRoot.\n\nexport const useResourceRoot = <TState>(\n element: ResourceElement<TState>,\n): useResourceRoot.SubscribableResource<TState> => {\n const scheduler = useMemo(\n () => new UpdateScheduler(() => handleUpdate(null)),\n [],\n );\n const queue = useMemo(() => [] as (() => void)[], []);\n\n const fiber = useMemo(() => {\n void element.key;\n\n return createResourceFiber(\n element.type,\n createResourceFiberRoot((callback) => {\n if (!scheduler.isDirty && !callback()) return;\n queue.push(callback);\n scheduler.markDirty();\n }),\n );\n }, [element.type, element.key, queue, scheduler]);\n\n setRootVersion(fiber.root, fiber.root.committedVersion);\n const render = renderResourceFiber(fiber, element.props);\n\n const isMountedRef = useRef(false);\n const committedPropsRef = useRef(element.props);\n const valueRef = useRef<TState>(render.output);\n const subscribers = useMemo(() => new Set<() => void>(), []);\n const handleUpdate = useEffectEvent((render: RenderResult | null) => {\n if (render === null) {\n setRootVersion(fiber.root, 2);\n setRootVersion(fiber.root, 1);\n\n queue.forEach((callback) => {\n if (isDevelopment && fiber.devStrictMode) {\n callback();\n }\n\n callback();\n });\n\n if (isDevelopment && fiber.devStrictMode) {\n void renderResourceFiber(fiber, committedPropsRef.current);\n }\n\n render = renderResourceFiber(fiber, committedPropsRef.current);\n }\n\n if (scheduler.isDirty)\n throw new Error(\"Scheduler is dirty, this should never happen\");\n\n commitRoot(fiber.root);\n queue.length = 0;\n\n if (isMountedRef.current) {\n commitResourceFiber(fiber, render);\n }\n\n if (scheduler.isDirty || valueRef.current === render.output) return;\n valueRef.current = render.output;\n subscribers.forEach((callback) => callback());\n });\n\n useEffect(() => {\n isMountedRef.current = true;\n return () => {\n isMountedRef.current = false;\n unmountResourceFiber(fiber);\n };\n }, [fiber]);\n\n useEffect(() => {\n committedPropsRef.current = render.props;\n commitRoot(fiber.root);\n commitResourceFiber(fiber, render);\n\n if (scheduler.isDirty || valueRef.current === render.output) return;\n valueRef.current = render.output;\n subscribers.forEach((callback) => callback());\n });\n\n return useMemo(\n () => ({\n getValue: () => valueRef.current,\n subscribe: (listener: () => void) => {\n subscribers.add(listener);\n return () => subscribers.delete(listener);\n },\n }),\n [subscribers],\n );\n};\n"],"mappings":";;;;;;;;;AAqCA,MAAa,mBACX,YACiD;CACjD,MAAM,YAAY,cACV,IAAI,sBAAsB,aAAa,IAAI,CAAC,GAClD,CAAC,CACH;CACA,MAAM,QAAQ,cAAc,CAAC,GAAqB,CAAC,CAAC;CAEpD,MAAM,QAAQ,cAAc;EAC1B,QAAa;EAEb,OAAO,oBACL,QAAQ,MACR,yBAAyB,aAAa;GACpC,IAAI,CAAC,UAAU,WAAW,CAAC,SAAS,GAAG;GACvC,MAAM,KAAK,QAAQ;GACnB,UAAU,UAAU;EACtB,CAAC,CACH;CACF,GAAG;EAAC,QAAQ;EAAM,QAAQ;EAAK;EAAO;CAAS,CAAC;CAEhD,eAAe,MAAM,MAAM,MAAM,KAAK,gBAAgB;CACtD,MAAM,SAAS,oBAAoB,OAAO,QAAQ,KAAK;CAEvD,MAAM,eAAe,OAAO,KAAK;CACjC,MAAM,oBAAoB,OAAO,QAAQ,KAAK;CAC9C,MAAM,WAAW,OAAe,OAAO,MAAM;CAC7C,MAAM,cAAc,8BAAc,IAAI,IAAgB,GAAG,CAAC,CAAC;CAC3D,MAAM,eAAe,gBAAgB,WAAgC;EACnE,IAAI,WAAW,MAAM;GACnB,eAAe,MAAM,MAAM,CAAC;GAC5B,eAAe,MAAM,MAAM,CAAC;GAE5B,MAAM,SAAS,aAAa;IAC1B,IAAI,iBAAiB,MAAM,eACzB,SAAS;IAGX,SAAS;GACX,CAAC;GAED,IAAI,iBAAiB,MAAM,eACzB,oBAAyB,OAAO,kBAAkB,OAAO;GAG3D,SAAS,oBAAoB,OAAO,kBAAkB,OAAO;EAC/D;EAEA,IAAI,UAAU,SACZ,MAAM,IAAI,MAAM,8CAA8C;EAEhE,WAAW,MAAM,IAAI;EACrB,MAAM,SAAS;EAEf,IAAI,aAAa,SACf,oBAAoB,OAAO,MAAM;EAGnC,IAAI,UAAU,WAAW,SAAS,YAAY,OAAO,QAAQ;EAC7D,SAAS,UAAU,OAAO;EAC1B,YAAY,SAAS,aAAa,SAAS,CAAC;CAC9C,CAAC;CAED,gBAAgB;EACd,aAAa,UAAU;EACvB,aAAa;GACX,aAAa,UAAU;GACvB,qBAAqB,KAAK;EAC5B;CACF,GAAG,CAAC,KAAK,CAAC;CAEV,gBAAgB;EACd,kBAAkB,UAAU,OAAO;EACnC,WAAW,MAAM,IAAI;EACrB,oBAAoB,OAAO,MAAM;EAEjC,IAAI,UAAU,WAAW,SAAS,YAAY,OAAO,QAAQ;EAC7D,SAAS,UAAU,OAAO;EAC1B,YAAY,SAAS,aAAa,SAAS,CAAC;CAC9C,CAAC;CAED,OAAO,eACE;EACL,gBAAgB,SAAS;EACzB,YAAY,aAAyB;GACnC,YAAY,IAAI,QAAQ;GACxB,aAAa,YAAY,OAAO,QAAQ;EAC1C;CACF,IACA,CAAC,WAAW,CACd;AACF"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useState.d.ts","names":[],"sources":["../../src/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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useState.js","names":[],"sources":["../../src/hooks/useState.ts"],"sourcesContent":["import { useReducer } 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 useReducer(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,WAAW,cAAc,SAAS,SAAS;AACpD"}
|
package/dist/react/hooks.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { useResourceRoot as useResourceRoot$1 } from "../hooks/useResourceRoot.js";
|
|
2
|
-
import { useResource as useResource$1 } from "../hooks/useResource.js";
|
|
3
|
-
import { useResources as useResources$1 } from "../hooks/useResources.js";
|
|
4
|
-
|
|
5
|
-
//#region src/react/hooks.d.ts
|
|
6
|
-
/**
|
|
7
|
-
* Hosts a resource element. Inside a resource render it hosts the element as a
|
|
8
|
-
* child resource; inside a React component it hosts it via the React bridge.
|
|
9
|
-
* `propsDeps` is a resource-render optimization and is ignored on the React side.
|
|
10
|
-
*/
|
|
11
|
-
declare const useResource: typeof useResource$1;
|
|
12
|
-
/**
|
|
13
|
-
* Hosts a keyed list of resource elements. Inside a resource render it composes
|
|
14
|
-
* them directly; inside a React component it hosts them via the React bridge.
|
|
15
|
-
*/
|
|
16
|
-
declare const useResources: typeof useResources$1;
|
|
17
|
-
/**
|
|
18
|
-
* Hosts a resource element as a subscribable root. Inside a resource render it
|
|
19
|
-
* uses the root hook directly; inside a React component it hosts that root via
|
|
20
|
-
* the React bridge (host it in one place; observe it via getValue/subscribe).
|
|
21
|
-
*/
|
|
22
|
-
declare const useResourceRoot: typeof useResourceRoot$1;
|
|
23
|
-
//#endregion
|
|
24
|
-
export { useResource, useResourceRoot, useResources };
|
|
25
|
-
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","names":[],"sources":["../../src/react/hooks.ts"],"mappings":";;;;;;;;;AA4FA;cAAa,WAAA,SAAoB,aAEhC;;;AAAA;AAMD;cAAa,YAAA,SAAqB,cAEjC;;;AAAA;AAOD;;cAAa,eAAA,SAAwB,iBAEpC"}
|
package/dist/react/hooks.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { resource } from "../core/resource.js";
|
|
2
|
-
import { isDevelopment } from "../core/helpers/env.js";
|
|
3
|
-
import { peekResourceFiber } from "../core/helpers/execution-context.js";
|
|
4
|
-
import { commitRoot, createResourceFiberRoot, setRootVersion } from "../core/helpers/root.js";
|
|
5
|
-
import { commitResourceFiber, createResourceFiber, renderResourceFiber, unmountResourceFiber } from "../core/ResourceFiber.js";
|
|
6
|
-
import { useResourceRoot as useResourceRoot$1 } from "../hooks/useResourceRoot.js";
|
|
7
|
-
import { useResource as useResource$1 } from "../hooks/useResource.js";
|
|
8
|
-
import { useResources as useResources$1 } from "../hooks/useResources.js";
|
|
9
|
-
import { useLayoutEffect, useMemo, useReducer, useRef, useState } from "react";
|
|
10
|
-
//#region src/react/hooks.ts
|
|
11
|
-
const useDevStrictMode = () => {
|
|
12
|
-
if (!isDevelopment) return null;
|
|
13
|
-
const count = useRef(0);
|
|
14
|
-
const isFirstRender = count.current === 0;
|
|
15
|
-
useState(() => count.current++);
|
|
16
|
-
if (count.current !== 2) return null;
|
|
17
|
-
return isFirstRender ? "child" : "root";
|
|
18
|
-
};
|
|
19
|
-
const HostResource = resource(function HostResource(callback) {
|
|
20
|
-
return callback();
|
|
21
|
-
});
|
|
22
|
-
const useResourceHost = (callback) => {
|
|
23
|
-
const root = useMemo(() => {
|
|
24
|
-
return createResourceFiberRoot((cb) => dispatch(cb));
|
|
25
|
-
}, []);
|
|
26
|
-
const [version, dispatch] = useReducer((v, cb) => {
|
|
27
|
-
setRootVersion(root, v);
|
|
28
|
-
return v + (cb() ? 1 : 0);
|
|
29
|
-
}, 0);
|
|
30
|
-
setRootVersion(root, version);
|
|
31
|
-
const devStrictMode = useDevStrictMode();
|
|
32
|
-
const fiber = useMemo(() => {
|
|
33
|
-
return createResourceFiber(HostResource, root, void 0, devStrictMode);
|
|
34
|
-
}, [root, devStrictMode]);
|
|
35
|
-
const result = renderResourceFiber(fiber, callback);
|
|
36
|
-
useLayoutEffect(() => {
|
|
37
|
-
return () => unmountResourceFiber(fiber);
|
|
38
|
-
}, [fiber]);
|
|
39
|
-
useLayoutEffect(() => {
|
|
40
|
-
commitRoot(root);
|
|
41
|
-
commitResourceFiber(fiber, result);
|
|
42
|
-
});
|
|
43
|
-
return result.output;
|
|
44
|
-
};
|
|
45
|
-
const makeHook = (hook) => ((...args) => {
|
|
46
|
-
if (peekResourceFiber()) return hook(...args);
|
|
47
|
-
return useResourceHost(() => hook(...args));
|
|
48
|
-
});
|
|
49
|
-
/**
|
|
50
|
-
* Hosts a resource element. Inside a resource render it hosts the element as a
|
|
51
|
-
* child resource; inside a React component it hosts it via the React bridge.
|
|
52
|
-
* `propsDeps` is a resource-render optimization and is ignored on the React side.
|
|
53
|
-
*/
|
|
54
|
-
const useResource = makeHook(useResource$1);
|
|
55
|
-
/**
|
|
56
|
-
* Hosts a keyed list of resource elements. Inside a resource render it composes
|
|
57
|
-
* them directly; inside a React component it hosts them via the React bridge.
|
|
58
|
-
*/
|
|
59
|
-
const useResources = makeHook(useResources$1);
|
|
60
|
-
/**
|
|
61
|
-
* Hosts a resource element as a subscribable root. Inside a resource render it
|
|
62
|
-
* uses the root hook directly; inside a React component it hosts that root via
|
|
63
|
-
* the React bridge (host it in one place; observe it via getValue/subscribe).
|
|
64
|
-
*/
|
|
65
|
-
const useResourceRoot = makeHook(useResourceRoot$1);
|
|
66
|
-
//#endregion
|
|
67
|
-
export { useResource, useResourceRoot, useResources };
|
|
68
|
-
|
|
69
|
-
//# sourceMappingURL=hooks.js.map
|
package/dist/react/hooks.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.js","names":["hooks.useResource","hooks.useResources","hooks.useResourceRoot"],"sources":["../../src/react/hooks.ts"],"sourcesContent":["import { useLayoutEffect, useMemo, useReducer, useRef, useState } from \"react\";\nimport type { ResourceFiberRoot, Resource } from \"../core/types\";\nimport {\n createResourceFiber,\n unmountResourceFiber,\n renderResourceFiber,\n commitResourceFiber,\n} from \"../core/ResourceFiber\";\nimport { isDevelopment } from \"../core/helpers/env\";\nimport {\n commitRoot,\n createResourceFiberRoot,\n setRootVersion,\n} from \"../core/helpers/root\";\nimport { peekResourceFiber } from \"../core/helpers/execution-context\";\nimport * as hooks from \"../hooks\";\nimport { resource } from \"../core/resource\";\n\nconst useDevStrictMode = () => {\n if (!isDevelopment) return null;\n\n // oxlint-disable-next-line react/rules-of-hooks -- isDevelopment is a build-time constant, so this branch is stable per build\n const count = useRef(0);\n const isFirstRender = count.current === 0;\n // oxlint-disable-next-line react/rules-of-hooks -- isDevelopment is a build-time constant, so this branch is stable per build\n useState(() => count.current++);\n if (count.current !== 2) return null;\n return isFirstRender ? (\"child\" as const) : (\"root\" as const);\n};\n\nconst HostResource = resource(function HostResource<T>(callback: () => T) {\n return callback();\n});\n\n// Runs `callback` inside a resource render hosted by a React component, so the\n// resource composition hooks (useResource/useResources/useResourceRoot) work from\n// React. `callback` executes inside the resource fiber below, so it may only call\n// resource hooks, not React's own hooks (which would have no fiber to attach to).\n// This is the single React->resource bridge; the React branch of every public\n// hook goes through it.\nconst useResourceHost = <T>(callback: () => T): T => {\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 const devStrictMode = useDevStrictMode();\n const fiber = useMemo(() => {\n return createResourceFiber(\n HostResource as unknown as Resource<T, () => T>,\n root,\n undefined,\n devStrictMode,\n );\n }, [root, devStrictMode]);\n\n const result = renderResourceFiber(fiber, callback);\n useLayoutEffect(() => {\n return () => unmountResourceFiber(fiber);\n }, [fiber]);\n useLayoutEffect(() => {\n commitRoot(root);\n commitResourceFiber(fiber, result);\n });\n\n return result.output;\n};\n\n// Turns a resource hook into an isomorphic hook with the SAME type (overloads\n// included): inside a resource render it calls the hook directly; inside a React\n// component it hosts it via the `useResourceHost` bridge. peekResourceFiber() is\n// stable per call site (a given call always renders in the same environment), so\n// the branch order across renders is fixed even though rules-of-hooks can't prove\n// it.\nconst makeHook = <F extends (...args: any[]) => any>(hook: F): F =>\n ((...args: any[]) => {\n /* oxlint-disable react/rules-of-hooks */\n if (peekResourceFiber()) return hook(...args);\n return useResourceHost(() => hook(...args));\n /* oxlint-enable react/rules-of-hooks */\n }) as F;\n\n/**\n * Hosts a resource element. Inside a resource render it hosts the element as a\n * child resource; inside a React component it hosts it via the React bridge.\n * `propsDeps` is a resource-render optimization and is ignored on the React side.\n */\nexport const useResource: typeof hooks.useResource = makeHook(\n hooks.useResource,\n);\n\n/**\n * Hosts a keyed list of resource elements. Inside a resource render it composes\n * them directly; inside a React component it hosts them via the React bridge.\n */\nexport const useResources: typeof hooks.useResources = makeHook(\n hooks.useResources,\n);\n\n/**\n * Hosts a resource element as a subscribable root. Inside a resource render it\n * uses the root hook directly; inside a React component it hosts that root via\n * the React bridge (host it in one place; observe it via getValue/subscribe).\n */\nexport const useResourceRoot: typeof hooks.useResourceRoot = makeHook(\n hooks.useResourceRoot,\n);\n"],"mappings":";;;;;;;;;;AAkBA,MAAM,yBAAyB;CAC7B,IAAI,CAAC,eAAe,OAAO;CAG3B,MAAM,QAAQ,OAAO,CAAC;CACtB,MAAM,gBAAgB,MAAM,YAAY;CAExC,eAAe,MAAM,SAAS;CAC9B,IAAI,MAAM,YAAY,GAAG,OAAO;CAChC,OAAO,gBAAiB,UAAqB;AAC/C;AAEA,MAAM,eAAe,SAAS,SAAS,aAAgB,UAAmB;CACxE,OAAO,SAAS;AAClB,CAAC;AAQD,MAAM,mBAAsB,aAAyB;CACnD,MAAM,OAAO,cAAiC;EAC5C,OAAO,yBAAyB,OAAO,SAAS,EAAE,CAAC;CACrD,GAAG,CAAC,CAAC;CAEL,MAAM,CAAC,SAAS,YAAY,YAAY,GAAW,OAAsB;EACvE,eAAe,MAAM,CAAC;EACtB,OAAO,KAAK,GAAG,IAAI,IAAI;CACzB,GAAG,CAAC;CACJ,eAAe,MAAM,OAAO;CAE5B,MAAM,gBAAgB,iBAAiB;CACvC,MAAM,QAAQ,cAAc;EAC1B,OAAO,oBACL,cACA,MACA,KAAA,GACA,aACF;CACF,GAAG,CAAC,MAAM,aAAa,CAAC;CAExB,MAAM,SAAS,oBAAoB,OAAO,QAAQ;CAClD,sBAAsB;EACpB,aAAa,qBAAqB,KAAK;CACzC,GAAG,CAAC,KAAK,CAAC;CACV,sBAAsB;EACpB,WAAW,IAAI;EACf,oBAAoB,OAAO,MAAM;CACnC,CAAC;CAED,OAAO,OAAO;AAChB;AAQA,MAAM,YAA+C,WACjD,GAAG,SAAgB;CAEnB,IAAI,kBAAkB,GAAG,OAAO,KAAK,GAAG,IAAI;CAC5C,OAAO,sBAAsB,KAAK,GAAG,IAAI,CAAC;AAE5C;;;;;;AAOF,MAAa,cAAwC,SACnDA,aACF;;;;;AAMA,MAAa,eAA0C,SACrDC,cACF;;;;;;AAOA,MAAa,kBAAgD,SAC3DC,iBACF"}
|