functype-react 1.19.0 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -18
- package/dist/AsyncState-DxKw9NF6.d.ts +21 -0
- package/dist/async/AsyncState.d.ts +2 -0
- package/dist/async/index.d.ts +7 -9
- package/dist/async/index.js +1 -1
- package/dist/async/index.js.map +1 -1
- package/dist/async/useIO.d.ts +27 -0
- package/dist/async/useIO.js +2 -0
- package/dist/async/useIO.js.map +1 -0
- package/dist/async/useIOPromise.d.ts +15 -0
- package/dist/async/useIOPromise.js +2 -0
- package/dist/async/useIOPromise.js.map +1 -0
- package/dist/async/useIOValue.d.ts +24 -0
- package/dist/async/useIOValue.js +2 -0
- package/dist/async/useIOValue.js.map +1 -0
- package/dist/{eq-CK6oQqWG.d.ts → eq-CR56QmPb.d.ts} +1 -3
- package/dist/forms/Validated.d.ts +0 -2
- package/dist/forms/Validated.js.map +1 -1
- package/dist/forms/useValidatedField.d.ts +0 -2
- package/dist/forms/useValidatedField.js.map +1 -1
- package/dist/forms/useValidatedForm.d.ts +0 -2
- package/dist/forms/useValidatedForm.js.map +1 -1
- package/dist/hooks/eq.d.ts +1 -1
- package/dist/hooks/eq.js.map +1 -1
- package/dist/hooks/index.d.ts +9 -9
- package/dist/hooks/useEither.d.ts +1 -1
- package/dist/hooks/useEither.js.map +1 -1
- package/dist/hooks/useList.d.ts +1 -1
- package/dist/hooks/useList.js.map +1 -1
- package/dist/hooks/useOption.d.ts +1 -1
- package/dist/hooks/useOption.js.map +1 -1
- package/dist/hooks/useStableCallback.d.ts +1 -1
- package/dist/hooks/useStableCallback.js.map +1 -1
- package/dist/hooks/useStableEffect.d.ts +1 -1
- package/dist/hooks/useStableEffect.js.map +1 -1
- package/dist/hooks/useStableMemo.d.ts +1 -1
- package/dist/hooks/useStableMemo.js.map +1 -1
- package/dist/hooks/useStableState.d.ts +1 -1
- package/dist/hooks/useStableState.js.map +1 -1
- package/dist/hooks/useTry.d.ts +1 -1
- package/dist/hooks/useTry.js.map +1 -1
- package/dist/{index-DjzwkXMF.d.ts → index-Dr09mq-A.d.ts} +1 -9
- package/dist/index.d.ts +10 -10
- package/dist/match/index.d.ts +1 -1
- package/dist/match/index.js +1 -1
- package/dist/match-CkGESkj9.js.map +1 -1
- package/dist/query/IOQueryError.d.ts +0 -2
- package/dist/query/IOQueryError.js.map +1 -1
- package/dist/query/ioQueryFn.d.ts +3 -4
- package/dist/query/ioQueryFn.js +1 -1
- package/dist/query/ioQueryFn.js.map +1 -1
- package/dist/query/queryState.d.ts +8 -10
- package/dist/query/queryState.js.map +1 -1
- package/dist/query/useIOMutation.d.ts +0 -2
- package/dist/query/useIOMutation.js.map +1 -1
- package/dist/query/useIOMutationState.d.ts +4 -6
- package/dist/query/useIOMutationState.js.map +1 -1
- package/dist/query/useIOQuery.d.ts +0 -2
- package/dist/query/useIOQuery.js.map +1 -1
- package/dist/query/useIOQueryState.d.ts +5 -7
- package/dist/query/useIOQueryState.js.map +1 -1
- package/dist/{useEither-DyPW7OaA.d.ts → useEither-D1Rn_tLR.d.ts} +1 -3
- package/dist/{useList-B5Iox1xv.d.ts → useList-C_7X5OA_.d.ts} +1 -3
- package/dist/{useOption-CXXNC1_S.d.ts → useOption-B8UyOh8h.d.ts} +1 -3
- package/dist/{useStableCallback-CXQQOhl6.d.ts → useStableCallback-C5jd1hKf.d.ts} +2 -4
- package/dist/{useStableEffect-C-5NCd1T.d.ts → useStableEffect-BiDhoNpC.d.ts} +2 -4
- package/dist/{useStableMemo-PYyLBWwd.d.ts → useStableMemo-DCeNIpt5.d.ts} +2 -4
- package/dist/{useStableState-fi6nURHS.d.ts → useStableState-BPVQ-XMX.d.ts} +2 -4
- package/dist/{useTry-B0HIcFFb.d.ts → useTry-ie1X0NiH.d.ts} +1 -3
- package/package.json +10 -10
- package/dist/TaskState-DWxGWqXf.d.ts +0 -24
- package/dist/async/TaskState.d.ts +0 -2
- package/dist/async/useTask.d.ts +0 -24
- package/dist/async/useTask.js +0 -2
- package/dist/async/useTask.js.map +0 -1
- package/dist/async/useTaskPromise.d.ts +0 -14
- package/dist/async/useTaskPromise.js +0 -2
- package/dist/async/useTaskPromise.js.map +0 -1
- package/dist/async/useTaskValue.d.ts +0 -24
- package/dist/async/useTaskValue.js +0 -2
- package/dist/async/useTaskValue.js.map +0 -1
- /package/dist/async/{TaskState.js → AsyncState.js} +0 -0
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Try } from "functype/try";
|
|
2
|
-
//#region src/hooks/useTry.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Stateful Try container. Defaults to Failure(new Error("uninitialized")) when no initial.
|
|
5
4
|
*/
|
|
@@ -9,6 +8,5 @@ declare function useTry<A>(initial?: Try<A>): {
|
|
|
9
8
|
setFailure: (e: Error) => void;
|
|
10
9
|
fold: <R>(onFailure: (e: Error) => R, onSuccess: (a: A) => R) => R;
|
|
11
10
|
};
|
|
12
|
-
//#endregion
|
|
13
11
|
export { useTry as t };
|
|
14
|
-
//# sourceMappingURL=useTry-
|
|
12
|
+
//# sourceMappingURL=useTry-ie1X0NiH.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functype-react",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.21.0",
|
|
4
4
|
"description": "React bindings for functype — ADT-aware hooks and exhaustive pattern matching components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"functype",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"@tanstack/react-query": ">=5.0.0",
|
|
30
|
-
"functype": ">=
|
|
30
|
+
"functype": ">=1.14.0",
|
|
31
31
|
"react": ">=18.0.0 <20.0.0",
|
|
32
32
|
"react-dom": ">=18.0.0 <20.0.0"
|
|
33
33
|
},
|
|
@@ -40,20 +40,20 @@
|
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@tanstack/react-query": "^5.104.
|
|
43
|
+
"@tanstack/react-query": "^5.104.5",
|
|
44
44
|
"@testing-library/dom": "^10.4.2",
|
|
45
45
|
"@testing-library/jest-dom": "^7.0.1",
|
|
46
46
|
"@testing-library/react": "^16.3.3",
|
|
47
|
-
"@types/node": "^24.19.
|
|
47
|
+
"@types/node": "^24.19.2",
|
|
48
48
|
"@types/react": "^19.3.0",
|
|
49
49
|
"@types/react-dom": "^19.3.0",
|
|
50
50
|
"fast-check": "^4.10.2",
|
|
51
|
-
"functype": "^1.
|
|
52
|
-
"jsdom": "^30.1.
|
|
53
|
-
"react": "^19.3.
|
|
54
|
-
"react-dom": "^19.3.
|
|
55
|
-
"ts-builds": "^
|
|
56
|
-
"tsdown": "^0.23.
|
|
51
|
+
"functype": "^1.21.0",
|
|
52
|
+
"jsdom": "^30.1.3",
|
|
53
|
+
"react": "^19.3.1",
|
|
54
|
+
"react-dom": "^19.3.1",
|
|
55
|
+
"ts-builds": "^4.0.0",
|
|
56
|
+
"tsdown": "^0.23.2"
|
|
57
57
|
},
|
|
58
58
|
"type": "module",
|
|
59
59
|
"main": "./dist/index.js",
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
//#region src/async/TaskState.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Lifecycle state of an async operation managed by `useTask`.
|
|
4
|
-
*
|
|
5
|
-
* Note: the tag values are `Idle | Pending | Success | Failure` — these are
|
|
6
|
-
* the consumer-facing lifecycle phases, intentionally broader than functype
|
|
7
|
-
* core's `TaskOutcome<T>` (which has only `Ok | Err`). The hook maps
|
|
8
|
-
* `TaskOutcome.Ok → Success`, `TaskOutcome.Err → Failure`, and adds `Idle`
|
|
9
|
-
* (pre-mount) and `Pending` (in flight) for the rendering side.
|
|
10
|
-
*/
|
|
11
|
-
type TaskState<E, A> = {
|
|
12
|
-
readonly _tag: "Idle";
|
|
13
|
-
} | {
|
|
14
|
-
readonly _tag: "Pending";
|
|
15
|
-
} | {
|
|
16
|
-
readonly _tag: "Success";
|
|
17
|
-
readonly value: A;
|
|
18
|
-
} | {
|
|
19
|
-
readonly _tag: "Failure";
|
|
20
|
-
readonly error: E;
|
|
21
|
-
};
|
|
22
|
-
//#endregion
|
|
23
|
-
export { TaskState as t };
|
|
24
|
-
//# sourceMappingURL=TaskState-DWxGWqXf.d.ts.map
|
package/dist/async/useTask.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { t as TaskState } from "../TaskState-DWxGWqXf.js";
|
|
2
|
-
import { Throwable } from "functype";
|
|
3
|
-
//#region src/async/useTask.d.ts
|
|
4
|
-
export type UseTaskResult<E, A> = TaskState<E, A> & {
|
|
5
|
-
readonly isIdle: boolean;
|
|
6
|
-
readonly isPending: boolean;
|
|
7
|
-
readonly isSuccess: boolean;
|
|
8
|
-
readonly isFailure: boolean;
|
|
9
|
-
refetch: () => void;
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* Run an async operation tied to a React component's lifecycle.
|
|
13
|
-
*
|
|
14
|
-
* - Returns a discriminated `TaskState<Throwable, A>` plus boolean flags and
|
|
15
|
-
* a `refetch` trigger.
|
|
16
|
-
* - The `task` callback receives an `AbortSignal` wired to a cancellation
|
|
17
|
-
* token that fires when the component unmounts or `deps` change. Pass the
|
|
18
|
-
* signal to `fetch` (or any abortable API) to cancel in-flight work.
|
|
19
|
-
* - StrictMode-safe: the cleanup fn cancels the token and discards any
|
|
20
|
-
* late-arriving result.
|
|
21
|
-
*/
|
|
22
|
-
export declare function useTask<A>(task: (signal: AbortSignal) => Promise<A> | A, deps: ReadonlyArray<unknown>): UseTaskResult<Throwable, A>;
|
|
23
|
-
//#endregion
|
|
24
|
-
//# sourceMappingURL=useTask.d.ts.map
|
package/dist/async/useTask.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use client";import{useCallback as e,useEffect as t,useReducer as n,useRef as r}from"react";import{Task as i,createCancellationTokenSource as a}from"functype";function o(e,t){return t.type===`PENDING`?{_tag:`Pending`}:t.type===`SUCCESS`?{_tag:`Success`,value:t.value}:{_tag:`Failure`,error:t.error}}function s(s,c){let[l,u]=n(o,{_tag:`Idle`}),[d,f]=n(e=>e+1,0),p=r(s);p.current=s,t(()=>{let e={value:!1},t=a();return u({type:`PENDING`}),i().Async(()=>p.current(t.token.signal),void 0,void 0,t.token).then(t=>{e.value||(t.isOk()?u({type:`SUCCESS`,value:t.value}):u({type:`FAILURE`,error:t.error}))}),()=>{e.value=!0,t.cancel()}},[...c,d]);let m=e(()=>f(),[]);return{...l,isIdle:l._tag===`Idle`,isPending:l._tag===`Pending`,isSuccess:l._tag===`Success`,isFailure:l._tag===`Failure`,refetch:m}}export{s as useTask};
|
|
2
|
-
//# sourceMappingURL=useTask.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useTask.js","names":["reducer","action","useTask","state","dispatch","useReducer","refetchTick","forceRefetch","n","taskRef","useRef","task","useEffect","cancelled","tokenSource","createCancellationTokenSource","Task","outcome","deps","refetch","useCallback"],"sources":["../../src/async/useTask.ts"],"sourcesContent":["\"use client\"\n\nimport { createCancellationTokenSource, Task, type Throwable } from \"functype\"\nimport { useCallback, useEffect, useReducer, useRef } from \"react\"\n\nimport type { TaskState } from \"./TaskState\"\n\ntype Action<E, A> =\n | { readonly type: \"PENDING\" }\n | { readonly type: \"SUCCESS\"; readonly value: A }\n | { readonly type: \"FAILURE\"; readonly error: E }\n\nfunction reducer<E, A>(_state: TaskState<E, A>, action: Action<E, A>): TaskState<E, A> {\n if (action.type === \"PENDING\") return { _tag: \"Pending\" }\n if (action.type === \"SUCCESS\") return { _tag: \"Success\", value: action.value }\n return { _tag: \"Failure\", error: action.error }\n}\n\nexport type UseTaskResult<E, A> = TaskState<E, A> & {\n readonly isIdle: boolean\n readonly isPending: boolean\n readonly isSuccess: boolean\n readonly isFailure: boolean\n refetch: () => void\n}\n\n/**\n * Run an async operation tied to a React component's lifecycle.\n *\n * - Returns a discriminated `TaskState<Throwable, A>` plus boolean flags and\n * a `refetch` trigger.\n * - The `task` callback receives an `AbortSignal` wired to a cancellation\n * token that fires when the component unmounts or `deps` change. Pass the\n * signal to `fetch` (or any abortable API) to cancel in-flight work.\n * - StrictMode-safe: the cleanup fn cancels the token and discards any\n * late-arriving result.\n */\nexport function useTask<A>(\n task: (signal: AbortSignal) => Promise<A> | A,\n deps: ReadonlyArray<unknown>,\n): UseTaskResult<Throwable, A> {\n const [state, dispatch] = useReducer(reducer<Throwable, A>, { _tag: \"Idle\" })\n const [refetchTick, forceRefetch] = useReducer((n: number) => n + 1, 0)\n const taskRef = useRef(task)\n taskRef.current = task\n\n useEffect(() => {\n const cancelled = { value: false }\n const tokenSource = createCancellationTokenSource()\n dispatch({ type: \"PENDING\" })\n\n void Task<A>()\n .Async<A>(() => taskRef.current(tokenSource.token.signal) as Promise<A>, undefined, undefined, tokenSource.token)\n .then((outcome) => {\n if (cancelled.value) return\n if (outcome.isOk()) {\n dispatch({ type: \"SUCCESS\", value: outcome.value as A })\n } else {\n dispatch({ type: \"FAILURE\", error: outcome.error as Throwable })\n }\n })\n\n return () => {\n cancelled.value = true\n tokenSource.cancel()\n }\n }, [...deps, refetchTick])\n\n const refetch = useCallback(() => forceRefetch(), [])\n\n return {\n ...state,\n isIdle: state._tag === \"Idle\",\n isPending: state._tag === \"Pending\",\n isSuccess: state._tag === \"Success\",\n isFailure: state._tag === \"Failure\",\n refetch,\n }\n}\n"],"mappings":"+JAYA,SAASA,EAAc,EAAyB,EAAuC,CAGrF,OAFIC,EAAO,OAAS,UAAkB,CAAE,KAAM,SAAU,EACpDA,EAAO,OAAS,UAAkB,CAAE,KAAM,UAAW,MAAOA,EAAO,KAAM,EACtE,CAAE,KAAM,UAAW,MAAOA,EAAO,KAAM,CAChD,CAqBA,SAAgBC,EACd,EACA,EAC6B,CAC7B,GAAM,CAACC,EAAOC,GAAYC,EAAWL,EAAuB,CAAE,KAAM,MAAO,CAAC,EACtE,CAACM,EAAaC,GAAgBF,EAAY,GAAcG,EAAI,EAAG,CAAC,EAChEC,EAAUC,EAAOC,CAAI,EAC3B,EAAQ,QAAUA,EAElBC,MAAgB,CACd,IAAMC,EAAY,CAAE,MAAO,EAAM,EAC3BC,EAAcC,EAA8B,EAclD,OAbAX,EAAS,CAAE,KAAM,SAAU,CAAC,EAE5BY,EAAa,CAAC,CACX,UAAeP,EAAQ,QAAQK,EAAY,MAAM,MAAM,EAAiB,IAAA,GAAW,IAAA,GAAWA,EAAY,KAAK,CAAC,CAChH,KAAM,GAAY,CACbD,EAAU,QACVI,EAAQ,KAAK,EACfb,EAAS,CAAE,KAAM,UAAW,MAAOa,EAAQ,KAAW,CAAC,EAEvDb,EAAS,CAAE,KAAM,UAAW,MAAOa,EAAQ,KAAmB,CAAC,EAEnE,CAAC,MAEU,CACX,EAAU,MAAQ,GAClBH,EAAY,OAAO,CACrB,CACF,EAAG,CAAC,GAAGI,EAAMZ,CAAW,CAAC,EAEzB,IAAMa,EAAUC,MAAkBb,EAAa,EAAG,CAAC,CAAC,EAEpD,MAAO,CACL,GAAGJ,EACH,OAAQA,EAAM,OAAS,OACvB,UAAWA,EAAM,OAAS,UAC1B,UAAWA,EAAM,OAAS,UAC1B,UAAWA,EAAM,OAAS,UAC1B,SACF,CACF"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { TaskOutcome } from "functype";
|
|
2
|
-
//#region src/async/useTaskPromise.d.ts
|
|
3
|
-
/**
|
|
4
|
-
* Returns a `Promise<TaskOutcome<A>>` that is stable across renders with the
|
|
5
|
-
* same `deps`. Intended for consumers of React 19's `use()` hook — providing
|
|
6
|
-
* a new promise reference on each render would infinite-suspend.
|
|
7
|
-
*
|
|
8
|
-
* The `task` callback is read through a ref, so the latest closure is invoked
|
|
9
|
-
* even if it isn't included in `deps`. Encode in `deps` whatever semantically
|
|
10
|
-
* changes the task's result.
|
|
11
|
-
*/
|
|
12
|
-
export declare function useTaskPromise<A>(task: (signal: AbortSignal) => Promise<A> | A, deps: ReadonlyArray<unknown>): Promise<TaskOutcome<A>>;
|
|
13
|
-
//#endregion
|
|
14
|
-
//# sourceMappingURL=useTaskPromise.d.ts.map
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use client";import{useMemo as e,useRef as t}from"react";import{Task as n,createCancellationTokenSource as r}from"functype";function i(i,a){let o=t(i);return o.current=i,e(()=>{let e=r();return n().Async(()=>o.current(e.token.signal),void 0,void 0,e.token)},a)}export{i as useTaskPromise};
|
|
2
|
-
//# sourceMappingURL=useTaskPromise.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useTaskPromise.js","names":["useTaskPromise","taskRef","useRef","task","useMemo","tokenSource","createCancellationTokenSource","Task","deps"],"sources":["../../src/async/useTaskPromise.ts"],"sourcesContent":["\"use client\"\n\nimport { createCancellationTokenSource, Task, type TaskOutcome } from \"functype\"\nimport { useMemo, useRef } from \"react\"\n\n/**\n * Returns a `Promise<TaskOutcome<A>>` that is stable across renders with the\n * same `deps`. Intended for consumers of React 19's `use()` hook — providing\n * a new promise reference on each render would infinite-suspend.\n *\n * The `task` callback is read through a ref, so the latest closure is invoked\n * even if it isn't included in `deps`. Encode in `deps` whatever semantically\n * changes the task's result.\n */\nexport function useTaskPromise<A>(\n task: (signal: AbortSignal) => Promise<A> | A,\n deps: ReadonlyArray<unknown>,\n): Promise<TaskOutcome<A>> {\n const taskRef = useRef(task)\n taskRef.current = task\n\n return useMemo(() => {\n const tokenSource = createCancellationTokenSource()\n return Task<A>().Async<A>(\n () => taskRef.current(tokenSource.token.signal) as Promise<A>,\n undefined,\n undefined,\n tokenSource.token,\n )\n }, deps)\n}\n"],"mappings":"4HAcA,SAAgBA,EACd,EACA,EACyB,CACzB,IAAMC,EAAUC,EAAOC,CAAI,EAG3B,MAFA,GAAQ,QAAUA,EAEXC,MAAc,CACnB,IAAMC,EAAcC,EAA8B,EAClD,OAAOC,EAAQ,CAAC,CAAC,UACTN,EAAQ,QAAQI,EAAY,MAAM,MAAM,EAC9C,IAAA,GACA,IAAA,GACAA,EAAY,KACd,CACF,EAAGG,CAAI,CACT"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
//#region src/async/useTaskValue.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Suspense-aware variant of `useTask` for React 19+. Suspends the component
|
|
4
|
-
* while the task is pending and throws on failure — both behaviors integrate
|
|
5
|
-
* with `<TaskBoundary>` (or a hand-rolled Suspense + ErrorBoundary pair).
|
|
6
|
-
*
|
|
7
|
-
* Invariants enforced by React 19:
|
|
8
|
-
* 1. The promise must be stable across renders. `useTaskPromise` memoizes by
|
|
9
|
-
* `deps`, so passing the same deps yields the same promise.
|
|
10
|
-
* 2. An ErrorBoundary must wrap the Suspense, never the reverse — otherwise
|
|
11
|
-
* Suspense will catch the thrown error instead of the boundary.
|
|
12
|
-
* 3. Do not call this on the server. Pass the underlying `Task` (or its
|
|
13
|
-
* promise) into a Client Component and call `useTaskValue` there.
|
|
14
|
-
*
|
|
15
|
-
* @interop React's `use()` reaches the nearest ErrorBoundary only through a throw, not an Either return.
|
|
16
|
-
*
|
|
17
|
-
* Testing note: React 19's `use()` does not unsuspend reliably under jsdom +
|
|
18
|
-
* @testing-library/react. End-to-end tests of `useTaskValue` + `<TaskBoundary>`
|
|
19
|
-
* require a real browser scheduler (Playwright); unit tests of `useTask` and
|
|
20
|
-
* `useTaskPromise` cover the Task plumbing.
|
|
21
|
-
*/
|
|
22
|
-
export declare function useTaskValue<A>(task: (signal: AbortSignal) => Promise<A> | A, deps: ReadonlyArray<unknown>): A;
|
|
23
|
-
//#endregion
|
|
24
|
-
//# sourceMappingURL=useTaskValue.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useTaskValue.js","names":["useTaskValue","outcome","use","useTaskPromise","task","deps"],"sources":["../../src/async/useTaskValue.ts"],"sourcesContent":["\"use client\"\n\nimport { use } from \"react\"\n\nimport { useTaskPromise } from \"./useTaskPromise\"\n\n/**\n * Suspense-aware variant of `useTask` for React 19+. Suspends the component\n * while the task is pending and throws on failure — both behaviors integrate\n * with `<TaskBoundary>` (or a hand-rolled Suspense + ErrorBoundary pair).\n *\n * Invariants enforced by React 19:\n * 1. The promise must be stable across renders. `useTaskPromise` memoizes by\n * `deps`, so passing the same deps yields the same promise.\n * 2. An ErrorBoundary must wrap the Suspense, never the reverse — otherwise\n * Suspense will catch the thrown error instead of the boundary.\n * 3. Do not call this on the server. Pass the underlying `Task` (or its\n * promise) into a Client Component and call `useTaskValue` there.\n *\n * @interop React's `use()` reaches the nearest ErrorBoundary only through a throw, not an Either return.\n *\n * Testing note: React 19's `use()` does not unsuspend reliably under jsdom +\n * @testing-library/react. End-to-end tests of `useTaskValue` + `<TaskBoundary>`\n * require a real browser scheduler (Playwright); unit tests of `useTask` and\n * `useTaskPromise` cover the Task plumbing.\n */\nexport function useTaskValue<A>(task: (signal: AbortSignal) => Promise<A> | A, deps: ReadonlyArray<unknown>): A {\n const outcome = use(useTaskPromise(task, deps))\n if (outcome.isErr()) {\n throw outcome.error\n }\n return outcome.value as A\n}\n"],"mappings":"8FA0BA,SAAgBA,EAAgB,EAA+C,EAAiC,CAC9G,IAAMC,EAAUC,EAAIC,EAAeC,EAAMC,CAAI,CAAC,EAC9C,GAAIJ,EAAQ,MAAM,EAChB,MAAMA,EAAQ,MAEhB,OAAOA,EAAQ,KACjB"}
|
|
File without changes
|