functype-react 1.27.2 → 2.0.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 +22 -35
- package/dist/AsyncState-DxKw9NF6.d.ts +20 -0
- package/dist/async/AsyncState.d.ts +2 -0
- package/dist/async/index.d.ts +7 -10
- package/dist/async/index.js +1 -1
- package/dist/async/index.js.map +1 -1
- package/dist/async/useIO.d.ts +26 -0
- package/dist/async/useIO.js +2 -0
- package/dist/async/useIO.js.map +1 -0
- package/dist/async/useIOPromise.d.ts +14 -0
- package/dist/async/useIOPromise.js +2 -0
- package/dist/async/useIOPromise.js.map +1 -0
- package/dist/async/useIOValue.d.ts +23 -0
- package/dist/async/useIOValue.js +2 -0
- package/dist/async/useIOValue.js.map +1 -0
- package/dist/{eq-CK6oQqWG.d.ts → eq-CQkAVer1.d.ts} +9 -7
- package/dist/forms/Validated.d.ts +1 -4
- package/dist/forms/Validated.js.map +1 -1
- package/dist/forms/index.d.ts +1 -1
- package/dist/forms/index.js +1 -1
- package/dist/forms/useValidatedField.d.ts +1 -4
- package/dist/forms/useValidatedField.js.map +1 -1
- package/dist/forms/useValidatedForm.d.ts +1 -4
- package/dist/forms/useValidatedForm.js.map +1 -1
- package/dist/hooks/eq.d.ts +1 -1
- package/dist/hooks/eq.js +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 -10
- package/dist/index.d.ts +18 -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 +1 -4
- package/dist/query/IOQueryError.js +1 -1
- package/dist/query/IOQueryError.js.map +1 -1
- package/dist/query/index.d.ts +18 -0
- package/dist/query/ioQueryFn.d.ts +4 -6
- package/dist/query/ioQueryFn.js +1 -1
- package/dist/query/ioQueryFn.js.map +1 -1
- package/dist/query/queryState.d.ts +11 -15
- package/dist/query/queryState.js.map +1 -1
- package/dist/query/useIOMutation.d.ts +1 -4
- package/dist/query/useIOMutation.js.map +1 -1
- package/dist/query/useIOMutationState.d.ts +5 -8
- package/dist/query/useIOMutationState.js.map +1 -1
- package/dist/query/useIOQuery.d.ts +1 -4
- package/dist/query/useIOQuery.js.map +1 -1
- package/dist/query/useIOQueryState.d.ts +6 -9
- package/dist/query/useIOQueryState.js.map +1 -1
- package/dist/{useEither-DyPW7OaA.d.ts → useEither-D1Rn_tLR.d.ts} +1 -4
- package/dist/{useList-B5Iox1xv.d.ts → useList-C_7X5OA_.d.ts} +1 -4
- package/dist/{useOption-CXXNC1_S.d.ts → useOption-B8UyOh8h.d.ts} +1 -4
- package/dist/{useStableCallback-CXQQOhl6.d.ts → useStableCallback-DPo_3DGi.d.ts} +2 -5
- package/dist/{useStableEffect-C-5NCd1T.d.ts → useStableEffect-C2D6ZQG5.d.ts} +2 -5
- package/dist/{useStableMemo-PYyLBWwd.d.ts → useStableMemo-DUoJxF-7.d.ts} +2 -5
- package/dist/{useStableState-fi6nURHS.d.ts → useStableState-DLLiFi5r.d.ts} +2 -5
- package/dist/{useTry-B0HIcFFb.d.ts → useTry-ie1X0NiH.d.ts} +1 -4
- package/package.json +9 -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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIOQueryState.js","names":["useIOQueryState","query","useIOQuery","queryKey","io","options","state","toQueryState"],"sources":["../../src/query/useIOQueryState.ts"],"sourcesContent":["\"use client\"\n\nimport type { QueryFunctionContext, QueryKey, UseQueryResult } from \"@tanstack/react-query\"\nimport type { IO } from \"functype/io\"\n\nimport type {
|
|
1
|
+
{"version":3,"file":"useIOQueryState.js","names":["useIOQueryState","query","useIOQuery","queryKey","io","options","state","toQueryState"],"sources":["../../src/query/useIOQueryState.ts"],"sourcesContent":["\"use client\"\n\nimport type { QueryFunctionContext, QueryKey, UseQueryResult } from \"@tanstack/react-query\"\nimport type { IO } from \"functype/io\"\n\nimport type { AsyncState } from \"../async/AsyncState\"\nimport type { IOQueryError } from \"./IOQueryError\"\nimport { toQueryState } from \"./queryState\"\nimport type { UseIOQueryOptions } from \"./useIOQuery\"\nimport { useIOQuery } from \"./useIOQuery\"\n\n/**\n * `AsyncState` plus the convenience flags and refetch trigger, mirroring\n * `UseIOResult` from `functype-react/async` so both async surfaces read alike.\n *\n * `refetch` keeps React Query's own signature rather than a lossy re-declaration.\n */\nexport type UseIOQueryStateResult<A, E> = AsyncState<IOQueryError<E>, A> &\n Pick<UseQueryResult<A, IOQueryError<E>>, \"refetch\"> & {\n readonly isIdle: boolean\n readonly isPending: boolean\n readonly isSuccess: boolean\n readonly isFailure: boolean\n }\n\n/**\n * `useIOQuery` projected straight onto the `AsyncState` ADT — the flag-soup-free path\n * in one call.\n *\n * ```tsx\n * const user = useIOQueryState([\"user\", id], ({ signal }) => Http.get<User>(url, { signal }))\n *\n * <Match value={user}>\n * {{\n * Idle: () => null,\n * Pending: () => <Spinner />,\n * Failure: ({ error }) => <Err e={error.error} />,\n * Success: ({ value }) => <Profile user={value} />,\n * }}\n * </Match>\n * ```\n *\n * Reads only `status` / `fetchStatus` / `data` / `error` / `refetch` off the query, so\n * React Query's tracked-properties optimization still applies — the component is not\n * subscribed to fields it never renders. When you need the rest of the result\n * (`isFetching`, `dataUpdatedAt`, `failureCount`), use {@link useIOQuery} with\n * {@link toQueryState} instead.\n */\nexport function useIOQueryState<A, E, TData = A, TQueryKey extends QueryKey = QueryKey>(\n queryKey: TQueryKey,\n io: (context: QueryFunctionContext<TQueryKey>) => IO<never, E, A>,\n options?: UseIOQueryOptions<A, E, TData, TQueryKey>,\n): UseIOQueryStateResult<TData, E> {\n const query = useIOQuery(queryKey, io, options)\n const state = toQueryState(query)\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: query.refetch,\n }\n}\n"],"mappings":"A,yGAgDA,SAAgBA,EACd,EACA,EACA,EACiC,CACjC,IAAMC,EAAQC,EAAWC,EAAUC,EAAIC,CAAO,EACxCC,EAAQC,EAAaN,CAAK,EAEhC,MAAO,CACL,GAAGK,EACH,OAAQA,EAAM,OAAS,OACvB,UAAWA,EAAM,OAAS,UAC1B,UAAWA,EAAM,OAAS,UAC1B,UAAWA,EAAM,OAAS,UAC1B,QAASL,EAAM,OACjB,CACF,C"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Either } from "functype/either";
|
|
2
|
-
//#region src/hooks/useEither.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Stateful Either container. Defaults to Left(undefined) when no initial.
|
|
5
4
|
*/
|
|
@@ -9,6 +8,4 @@ declare function useEither<E, A>(initial?: Either<E, A>): {
|
|
|
9
8
|
setLeft: (e: E) => void;
|
|
10
9
|
fold: <R>(onLeft: (e: E) => R, onRight: (a: A) => R) => R;
|
|
11
10
|
};
|
|
12
|
-
|
|
13
|
-
export { useEither as t };
|
|
14
|
-
//# sourceMappingURL=useEither-DyPW7OaA.d.ts.map
|
|
11
|
+
export { useEither as t };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { List } from "functype/list";
|
|
2
|
-
//#region src/hooks/useList.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Stateful immutable List container. All mutators return new state.
|
|
5
4
|
*/
|
|
@@ -12,6 +11,4 @@ declare function useList<A>(initial?: readonly A[]): {
|
|
|
12
11
|
map: <B>(f: (a: A) => B) => List<B>;
|
|
13
12
|
filter: (p: (a: A) => boolean) => List<A>;
|
|
14
13
|
};
|
|
15
|
-
|
|
16
|
-
export { useList as t };
|
|
17
|
-
//# sourceMappingURL=useList-B5Iox1xv.d.ts.map
|
|
14
|
+
export { useList as t };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Option } from "functype/option";
|
|
2
|
-
//#region src/hooks/useOption.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* Stateful Option container. `set(null | undefined)` clears to None.
|
|
5
4
|
*
|
|
@@ -12,6 +11,4 @@ declare function useOption<A>(initial?: A): {
|
|
|
12
11
|
map: <B>(f: (a: A) => B) => Option<B>;
|
|
13
12
|
fold: <R>(onNone: () => R, onSome: (a: A) => R) => R;
|
|
14
13
|
};
|
|
15
|
-
|
|
16
|
-
export { useOption as t };
|
|
17
|
-
//# sourceMappingURL=useOption-CXXNC1_S.d.ts.map
|
|
14
|
+
export { useOption as t };
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { t as Eq } from "./eq-
|
|
1
|
+
import { t as Eq } from "./eq-CQkAVer1.js";
|
|
2
2
|
import { DependencyList } from "react";
|
|
3
|
-
//#region src/hooks/useStableCallback.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Like `useCallback`, but only returns a new function when *some* dep has
|
|
6
5
|
* changed under the supplied (or default) comparator.
|
|
7
6
|
*/
|
|
8
7
|
declare function useStableCallback<F extends (...args: never[]) => unknown>(callback: F, deps: DependencyList, eqs?: ReadonlyArray<Eq<unknown> | undefined>): F;
|
|
9
|
-
|
|
10
|
-
export { useStableCallback as t };
|
|
11
|
-
//# sourceMappingURL=useStableCallback-CXQQOhl6.d.ts.map
|
|
8
|
+
export { useStableCallback as t };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { t as Eq } from "./eq-
|
|
1
|
+
import { t as Eq } from "./eq-CQkAVer1.js";
|
|
2
2
|
import { DependencyList, EffectCallback } from "react";
|
|
3
|
-
//#region src/hooks/useStableEffect.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Like `useEffect`, but re-runs only when *some* dep has changed under the
|
|
6
5
|
* supplied (or default) comparator. The `eqs` array is aligned positionally
|
|
@@ -10,6 +9,4 @@ import { DependencyList, EffectCallback } from "react";
|
|
|
10
9
|
* structural identity is what matters, but whose references churn every render.
|
|
11
10
|
*/
|
|
12
11
|
declare function useStableEffect(effect: EffectCallback, deps: DependencyList, eqs?: ReadonlyArray<Eq<unknown> | undefined>): void;
|
|
13
|
-
|
|
14
|
-
export { useStableEffect as t };
|
|
15
|
-
//# sourceMappingURL=useStableEffect-C-5NCd1T.d.ts.map
|
|
12
|
+
export { useStableEffect as t };
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { t as Eq } from "./eq-
|
|
1
|
+
import { t as Eq } from "./eq-CQkAVer1.js";
|
|
2
2
|
import { DependencyList } from "react";
|
|
3
|
-
//#region src/hooks/useStableMemo.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Like `useMemo`, but recomputes only when *some* dep has changed under the
|
|
6
5
|
* supplied (or default) comparator. The `eqs` array is aligned positionally
|
|
7
6
|
* with `deps`; missing entries fall back to `referenceEq`.
|
|
8
7
|
*/
|
|
9
8
|
declare function useStableMemo<A>(factory: () => A, deps: DependencyList, eqs?: ReadonlyArray<Eq<unknown> | undefined>): A;
|
|
10
|
-
|
|
11
|
-
export { useStableMemo as t };
|
|
12
|
-
//# sourceMappingURL=useStableMemo-PYyLBWwd.d.ts.map
|
|
9
|
+
export { useStableMemo as t };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { t as Eq } from "./eq-
|
|
2
|
-
//#region src/hooks/useStableState.d.ts
|
|
1
|
+
import { t as Eq } from "./eq-CQkAVer1.js";
|
|
3
2
|
/**
|
|
4
3
|
* Drop-in replacement for `useState` that no-ops when the next value is equal
|
|
5
4
|
* to the current under `eq`. Defaults to `Object.is` (React's built-in), so the
|
|
@@ -9,6 +8,4 @@ import { t as Eq } from "./eq-CK6oQqWG.js";
|
|
|
9
8
|
* @param eq - Comparator used to decide whether to skip the update
|
|
10
9
|
*/
|
|
11
10
|
declare function useStableState<A>(initial: A | (() => A), eq?: Eq<A>): readonly [A, (next: A | ((prev: A) => A)) => void];
|
|
12
|
-
|
|
13
|
-
export { useStableState as t };
|
|
14
|
-
//# sourceMappingURL=useStableState-fi6nURHS.d.ts.map
|
|
11
|
+
export { useStableState as t };
|
|
@@ -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,4 @@ 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
|
-
|
|
13
|
-
export { useTry as t };
|
|
14
|
-
//# sourceMappingURL=useTry-B0HIcFFb.d.ts.map
|
|
11
|
+
export { useTry as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functype-react",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "React bindings for functype — ADT-aware hooks and exhaustive pattern matching components",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"functype",
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
"adt",
|
|
11
11
|
"option",
|
|
12
12
|
"either",
|
|
13
|
-
"task",
|
|
14
13
|
"typescript",
|
|
15
14
|
"functional"
|
|
16
15
|
],
|
|
@@ -27,7 +26,7 @@
|
|
|
27
26
|
},
|
|
28
27
|
"peerDependencies": {
|
|
29
28
|
"@tanstack/react-query": ">=5.0.0",
|
|
30
|
-
"functype": ">=0.
|
|
29
|
+
"functype": ">=2.0.0",
|
|
31
30
|
"react": ">=18.0.0 <20.0.0",
|
|
32
31
|
"react-dom": ">=18.0.0 <20.0.0"
|
|
33
32
|
},
|
|
@@ -40,20 +39,20 @@
|
|
|
40
39
|
}
|
|
41
40
|
},
|
|
42
41
|
"devDependencies": {
|
|
43
|
-
"@tanstack/react-query": "^5.
|
|
42
|
+
"@tanstack/react-query": "^5.107.1",
|
|
44
43
|
"@testing-library/dom": "^10.4.2",
|
|
45
44
|
"@testing-library/jest-dom": "^7.0.1",
|
|
46
45
|
"@testing-library/react": "^16.3.3",
|
|
47
|
-
"@types/node": "^24.19.
|
|
46
|
+
"@types/node": "^24.19.2",
|
|
48
47
|
"@types/react": "^19.3.0",
|
|
49
48
|
"@types/react-dom": "^19.3.0",
|
|
50
49
|
"fast-check": "^4.10.2",
|
|
51
|
-
"functype": "^
|
|
52
|
-
"jsdom": "^
|
|
53
|
-
"react": "^19.3.
|
|
54
|
-
"react-dom": "^19.3.
|
|
50
|
+
"functype": "^2.0.0",
|
|
51
|
+
"jsdom": "^31.0.1",
|
|
52
|
+
"react": "^19.3.1",
|
|
53
|
+
"react-dom": "^19.3.1",
|
|
55
54
|
"ts-builds": "^4.5.0",
|
|
56
|
-
"tsdown": "^0.23.
|
|
55
|
+
"tsdown": "^0.23.7"
|
|
57
56
|
},
|
|
58
57
|
"type": "module",
|
|
59
58
|
"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
|