@shivaedev/react 0.49.1 → 0.50.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/CHANGELOG.md +27 -0
- package/README.md +84 -116
- package/dist/client/FailureHostContext.d.ts +3 -0
- package/dist/client/FailureHostContext.d.ts.map +1 -0
- package/dist/client/FailureHostContext.js +3 -0
- package/dist/client/FailureHostContext.js.map +1 -0
- package/dist/client/NoticeContext.d.ts +1 -1
- package/dist/client/QueryView.d.ts +20 -5
- package/dist/client/QueryView.d.ts.map +1 -1
- package/dist/client/QueryView.js +23 -3
- package/dist/client/QueryView.js.map +1 -1
- package/dist/client/actionCompletion.d.ts +6 -5
- package/dist/client/actionCompletion.d.ts.map +1 -1
- package/dist/client/actionCompletion.js +19 -18
- package/dist/client/actionCompletion.js.map +1 -1
- package/dist/client/adapters/formFields.d.ts +4 -0
- package/dist/client/adapters/formFields.d.ts.map +1 -0
- package/dist/client/adapters/formFields.js +30 -0
- package/dist/client/adapters/formFields.js.map +1 -0
- package/dist/client/adapters/operationHooks.d.ts +5 -0
- package/dist/client/adapters/operationHooks.d.ts.map +1 -0
- package/dist/client/adapters/operationHooks.js +7 -0
- package/dist/client/adapters/operationHooks.js.map +1 -0
- package/dist/client/adapters/rowValues.d.ts +7 -0
- package/dist/client/adapters/rowValues.d.ts.map +1 -0
- package/dist/client/adapters/rowValues.js +17 -0
- package/dist/client/adapters/rowValues.js.map +1 -0
- package/dist/client/adapters/useFeedback.d.ts +11 -0
- package/dist/client/adapters/useFeedback.d.ts.map +1 -0
- package/dist/client/adapters/useFeedback.js +38 -0
- package/dist/client/adapters/useFeedback.js.map +1 -0
- package/dist/client/adapters/useQueryNotice.js +1 -1
- package/dist/client/adapters/useQueryNotice.js.map +1 -1
- package/dist/client/adapters/useStableRun.js +1 -1
- package/dist/client/adapters/useStableRun.js.map +1 -1
- package/dist/client/adapters/useSubmissionCallbacks.d.ts +4 -5
- package/dist/client/adapters/useSubmissionCallbacks.d.ts.map +1 -1
- package/dist/client/adapters/useSubmissionCallbacks.js +37 -17
- package/dist/client/adapters/useSubmissionCallbacks.js.map +1 -1
- package/dist/client/adapters/useUndoableChange.d.ts +13 -12
- package/dist/client/adapters/useUndoableChange.d.ts.map +1 -1
- package/dist/client/adapters/useUndoableChange.js +16 -4
- package/dist/client/adapters/useUndoableChange.js.map +1 -1
- package/dist/client/boundOperation.d.ts +12 -0
- package/dist/client/boundOperation.d.ts.map +1 -0
- package/dist/client/boundOperation.js +2 -0
- package/dist/client/boundOperation.js.map +1 -0
- package/dist/client/create.d.ts +15 -11
- package/dist/client/create.d.ts.map +1 -1
- package/dist/client/create.js +12 -5
- package/dist/client/create.js.map +1 -1
- package/dist/client/editor.d.ts +25 -18
- package/dist/client/editor.d.ts.map +1 -1
- package/dist/client/editor.js +14 -8
- package/dist/client/editor.js.map +1 -1
- package/dist/client/failureDecision.d.ts +16 -0
- package/dist/client/failureDecision.d.ts.map +1 -0
- package/dist/client/failureDecision.js +49 -0
- package/dist/client/failureDecision.js.map +1 -0
- package/dist/client/failures.d.ts +45 -0
- package/dist/client/failures.d.ts.map +1 -0
- package/dist/client/failures.js +2 -0
- package/dist/client/failures.js.map +1 -0
- package/dist/client/notices.d.ts +9 -4
- package/dist/client/notices.d.ts.map +1 -1
- package/dist/client/notices.js +22 -3
- package/dist/client/notices.js.map +1 -1
- package/dist/client/operationForm.d.ts +40 -0
- package/dist/client/operationForm.d.ts.map +1 -0
- package/dist/client/operationForm.js +2 -0
- package/dist/client/operationForm.js.map +1 -0
- package/dist/client/pendingCommand.d.ts +6 -9
- package/dist/client/pendingCommand.d.ts.map +1 -1
- package/dist/client/pendingCommand.js +3 -3
- package/dist/client/pendingCommand.js.map +1 -1
- package/dist/client/resultState.d.ts +7 -4
- package/dist/client/resultState.d.ts.map +1 -1
- package/dist/client/resultState.js +6 -3
- package/dist/client/resultState.js.map +1 -1
- package/dist/client/unexpectedFailure.d.ts +8 -0
- package/dist/client/unexpectedFailure.d.ts.map +1 -0
- package/dist/client/unexpectedFailure.js +17 -0
- package/dist/client/unexpectedFailure.js.map +1 -0
- package/dist/noticeEntry.d.ts +3 -1
- package/dist/noticeEntry.d.ts.map +1 -1
- package/dist/noticeEntry.js +2 -1
- package/dist/noticeEntry.js.map +1 -1
- package/dist/noticeStore.d.ts +3 -3
- package/dist/noticeStore.d.ts.map +1 -1
- package/dist/noticeStore.js +4 -4
- package/dist/noticeStore.js.map +1 -1
- package/dist/noticeTypes.d.ts +1 -2
- package/dist/noticeTypes.d.ts.map +1 -1
- package/dist/submission.d.ts +2 -4
- package/dist/submission.d.ts.map +1 -1
- package/dist/submission.js +1 -3
- package/dist/submission.js.map +1 -1
- package/package.json +22 -22
- package/src/client/FailureHostContext.ts +4 -0
- package/src/client/QueryView.ts +50 -7
- package/src/client/actionCompletion.ts +33 -23
- package/src/client/adapters/formFields.ts +33 -0
- package/src/client/adapters/operationHooks.ts +14 -0
- package/src/client/adapters/rowValues.ts +25 -0
- package/src/client/adapters/useFeedback.ts +50 -0
- package/src/client/adapters/useQueryNotice.ts +1 -1
- package/src/client/adapters/useStableRun.ts +1 -1
- package/src/client/adapters/useSubmissionCallbacks.ts +44 -21
- package/src/client/adapters/useUndoableChange.ts +66 -30
- package/src/client/boundOperation.ts +13 -0
- package/src/client/create.ts +50 -20
- package/src/client/editor.ts +98 -38
- package/src/client/failureDecision.ts +70 -0
- package/src/client/failures.ts +56 -0
- package/src/client/notices.ts +44 -6
- package/src/client/operationForm.ts +52 -0
- package/src/client/pendingCommand.ts +12 -14
- package/src/client/resultState.ts +20 -11
- package/src/client/unexpectedFailure.ts +24 -0
- package/src/noticeEntry.ts +5 -0
- package/src/noticeStore.ts +9 -4
- package/src/noticeTypes.ts +1 -2
- package/src/submission.ts +4 -6
- package/dist/client/adapters/useNoticePublisher.d.ts +0 -4
- package/dist/client/adapters/useNoticePublisher.d.ts.map +0 -1
- package/dist/client/adapters/useNoticePublisher.js +0 -8
- package/dist/client/adapters/useNoticePublisher.js.map +0 -1
- package/src/client/adapters/useNoticePublisher.ts +0 -10
package/src/client/QueryView.ts
CHANGED
|
@@ -1,16 +1,59 @@
|
|
|
1
|
-
import { Option } from "effect";
|
|
2
|
-
import { createElement, Fragment, type ReactNode } from "react";
|
|
1
|
+
import { Option, Predicate } from "effect";
|
|
2
|
+
import { createContext, createElement, Fragment, type ReactNode, useContext } from "react";
|
|
3
|
+
import { isRequestRejection, isServerFailure } from "@shivaedev/errors/taxonomy.ts";
|
|
3
4
|
import { rethrowQueryDefect } from "#client/adapters/rethrowQueryDefect.ts";
|
|
5
|
+
import type { Unhandled } from "#client/failures.ts";
|
|
4
6
|
import type { QueryState } from "#client/resultState.ts";
|
|
5
7
|
|
|
6
|
-
export interface
|
|
8
|
+
export interface QueryViewRenderers {
|
|
9
|
+
readonly failure: (error: unknown, query: QueryState<unknown, unknown>) => ReactNode;
|
|
10
|
+
readonly loading: (query: QueryState<unknown, unknown>) => ReactNode;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function queryFailureMessage(error: unknown): string {
|
|
14
|
+
return (isRequestRejection(error) || isServerFailure(error)) && Predicate.isString(error.message) && error.message !== ""
|
|
15
|
+
? error.message
|
|
16
|
+
: "Could not load. Try again.";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const defaultQueryViewRenderers: QueryViewRenderers = {
|
|
20
|
+
failure: (error, query) =>
|
|
21
|
+
createElement(
|
|
22
|
+
"p",
|
|
23
|
+
{ role: "alert" },
|
|
24
|
+
queryFailureMessage(error),
|
|
25
|
+
" ",
|
|
26
|
+
createElement("button", { disabled: query.pending, onClick: query.refresh, type: "button" }, "Retry"),
|
|
27
|
+
),
|
|
28
|
+
loading: (query) => createElement("p", { role: "status" }, query.refreshing ? "Refreshing…" : "Loading…"),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const QueryViewContext = createContext<QueryViewRenderers>(defaultQueryViewRenderers);
|
|
32
|
+
|
|
33
|
+
// The application chooses once how every QueryView shows loading and an ordinary failure; a view may still pass its own.
|
|
34
|
+
export function QueryViewDefaults({ children, failure, loading }: Partial<QueryViewRenderers> & { readonly children: ReactNode }): ReactNode {
|
|
35
|
+
const inherited = useContext(QueryViewContext);
|
|
36
|
+
return createElement(
|
|
37
|
+
QueryViewContext.Provider,
|
|
38
|
+
{ value: { failure: failure ?? inherited.failure, loading: loading ?? inherited.loading } },
|
|
39
|
+
children,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type FailureRenderer<A, E> = (error: E, query: QueryState<A, E>) => ReactNode;
|
|
44
|
+
|
|
45
|
+
// A query that can fail with a rejection the app declared must say how to show it; the shared ones have a default.
|
|
46
|
+
export type QueryViewProps<A, E> = {
|
|
7
47
|
readonly content: (data: A, query: QueryState<A, E>) => ReactNode;
|
|
8
|
-
readonly
|
|
9
|
-
readonly loading: (query: QueryState<A, E>) => ReactNode;
|
|
48
|
+
readonly loading?: ((query: QueryState<A, E>) => ReactNode) | undefined;
|
|
10
49
|
readonly query: QueryState<A, E>;
|
|
11
|
-
}
|
|
50
|
+
} & ([Unhandled<E>] extends [never] ? { readonly failure?: FailureRenderer<A, E> | undefined } : { readonly failure: FailureRenderer<A, E> });
|
|
12
51
|
|
|
13
|
-
export function QueryView<A, E>(
|
|
52
|
+
export function QueryView<A, E>(props: QueryViewProps<A, E>): ReactNode {
|
|
53
|
+
const { content, query } = props;
|
|
54
|
+
const defaults = useContext(QueryViewContext);
|
|
55
|
+
const loading = props.loading ?? defaults.loading;
|
|
56
|
+
const failure = props.failure ?? defaults.failure;
|
|
14
57
|
const error = Option.flatMap(query.cause, (cause) => {
|
|
15
58
|
rethrowQueryDefect(cause);
|
|
16
59
|
return query.refusal;
|
|
@@ -1,31 +1,41 @@
|
|
|
1
1
|
import { RegistryContext } from "@effect/atom-react";
|
|
2
|
-
import { Effect } from "effect";
|
|
2
|
+
import { Cause, Effect, Option } from "effect";
|
|
3
3
|
import type * as Atom from "effect/reactivity/Atom";
|
|
4
4
|
import { useContext, useInsertionEffect, useMemo, useRef } from "react";
|
|
5
5
|
import { isUnauthorized } from "@shivaedev/errors/taxonomy.ts";
|
|
6
|
-
import
|
|
6
|
+
import { useFeedback } from "#client/adapters/useFeedback.ts";
|
|
7
7
|
import { useStableRun } from "#client/adapters/useStableRun.ts";
|
|
8
|
-
import {
|
|
9
|
-
import type {
|
|
8
|
+
import type { BoundOperation } from "#client/boundOperation.ts";
|
|
9
|
+
import type { FailureChoices, FailuresOption } from "#client/failures.ts";
|
|
10
|
+
import type { NoticeDescriptor, NoticeFeedback } from "#client/notices.ts";
|
|
10
11
|
import { useSessionRecheck } from "#client/sessionBoundary.ts";
|
|
11
|
-
import { publishNotice } from "#noticeStore.ts";
|
|
12
12
|
|
|
13
13
|
export interface ActionOptions<TInput, A, E, R = never> {
|
|
14
14
|
readonly concurrent?: boolean;
|
|
15
|
-
readonly notices?: NoticeFeedback<TInput, A,
|
|
15
|
+
readonly notices?: NoticeFeedback<TInput, A, R>;
|
|
16
16
|
readonly onFailure?: (error: E, input: TInput) => void;
|
|
17
17
|
readonly onSuccess?: (value: A, input: TInput) => void;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export type OperationActionOptions<TInput, A, E, R, TSilent extends boolean = false> = ActionOptions<
|
|
21
|
+
NoInfer<TInput>,
|
|
22
|
+
NoInfer<A>,
|
|
23
|
+
NoInfer<E>,
|
|
24
|
+
NoInfer<R>
|
|
25
|
+
> &
|
|
26
|
+
FailuresOption<E, TInput, NoticeDescriptor<R>, TSilent>;
|
|
27
|
+
|
|
20
28
|
export function useActionSource<TInput, A, E, R, TRuntimeError>(
|
|
21
|
-
source: Atom.AtomResultFn<TInput, A, E> |
|
|
22
|
-
|
|
23
|
-
options: ActionOptions<TInput, A, E, R>,
|
|
29
|
+
source: Atom.AtomResultFn<TInput, A, E> | BoundOperation<TInput, A, E, R, TRuntimeError>,
|
|
30
|
+
options: ActionOptions<TInput, A, E, R> & FailureChoices,
|
|
24
31
|
): Atom.AtomResultFn<TInput, A, E | TRuntimeError> {
|
|
32
|
+
// The operation object may be written inline, so its `run` and `runtime`, not the object, are the action's identity.
|
|
33
|
+
const { native, run, runtime } =
|
|
34
|
+
"runtime" in source ? { native: undefined, run: source.run, runtime: source.runtime } : { native: source, run: undefined, runtime: undefined };
|
|
25
35
|
useStableRun(run);
|
|
26
36
|
const registry = useContext(RegistryContext);
|
|
27
37
|
const recheck = useSessionRecheck();
|
|
28
|
-
const
|
|
38
|
+
const feedback = useFeedback(runtime);
|
|
29
39
|
const latestRef = useRef({ options, recheck });
|
|
30
40
|
useInsertionEffect(() => {
|
|
31
41
|
latestRef.current = { options, recheck };
|
|
@@ -33,29 +43,29 @@ export function useActionSource<TInput, A, E, R, TRuntimeError>(
|
|
|
33
43
|
const concurrent = options.concurrent;
|
|
34
44
|
const owner = useMemo(() => {
|
|
35
45
|
const lifetime = { active: false, registry };
|
|
36
|
-
function rejected(
|
|
46
|
+
function rejected(cause: Cause.Cause<E>, input: TInput) {
|
|
37
47
|
return Effect.gen(function* () {
|
|
38
|
-
|
|
48
|
+
const error = Cause.findErrorOption(cause);
|
|
49
|
+
if (lifetime.active && Option.exists(error, isUnauthorized)) {
|
|
39
50
|
yield* Effect.sync(() => latestRef.current.recheck());
|
|
40
51
|
}
|
|
41
52
|
yield* Effect.sync(() => {
|
|
42
53
|
if (lifetime.active) {
|
|
43
|
-
latestRef.current
|
|
44
|
-
if (
|
|
45
|
-
|
|
54
|
+
const { options: latest } = latestRef.current;
|
|
55
|
+
if (Option.isSome(error)) {
|
|
56
|
+
latest.onFailure?.(error.value, input);
|
|
46
57
|
}
|
|
58
|
+
feedback.failed(cause, input, latest);
|
|
47
59
|
}
|
|
48
60
|
});
|
|
49
|
-
return yield* Effect.
|
|
61
|
+
return yield* Effect.failCause(cause);
|
|
50
62
|
});
|
|
51
63
|
}
|
|
52
64
|
function completed(value: A, input: TInput) {
|
|
53
65
|
return Effect.sync(() => {
|
|
54
66
|
if (lifetime.active) {
|
|
55
67
|
latestRef.current.options.onSuccess?.(value, input);
|
|
56
|
-
|
|
57
|
-
publishNotice(notices, source, latestRef.current.options.notices?.onSuccess?.(value, input));
|
|
58
|
-
}
|
|
68
|
+
feedback.succeeded(latestRef.current.options.notices?.onSuccess?.(value, input));
|
|
59
69
|
}
|
|
60
70
|
return value;
|
|
61
71
|
});
|
|
@@ -65,15 +75,15 @@ export function useActionSource<TInput, A, E, R, TRuntimeError>(
|
|
|
65
75
|
if (!run) {
|
|
66
76
|
return Effect.die(new Error("Missing command Effect"));
|
|
67
77
|
}
|
|
68
|
-
return Effect.
|
|
69
|
-
onFailure: (
|
|
78
|
+
return Effect.matchCauseEffect(run(input), {
|
|
79
|
+
onFailure: (cause) => rejected(cause, input),
|
|
70
80
|
onSuccess: (value) => completed(value, input),
|
|
71
81
|
});
|
|
72
82
|
});
|
|
73
83
|
}
|
|
74
|
-
const atom =
|
|
84
|
+
const atom = runtime === undefined ? native : runtime.fn(execute, { concurrent });
|
|
75
85
|
return { atom, lifetime };
|
|
76
|
-
}, [
|
|
86
|
+
}, [native, runtime, run, registry, concurrent, feedback]);
|
|
77
87
|
useInsertionEffect(() => {
|
|
78
88
|
owner.lifetime.active = true;
|
|
79
89
|
return () => {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { Fields } from "@shivaedev/form/shape.ts";
|
|
3
|
+
|
|
4
|
+
const derived = new WeakMap<Schema.Top, Map<string, Schema.Struct<Fields>>>();
|
|
5
|
+
|
|
6
|
+
function isStruct(schema: unknown): schema is Schema.Struct<Fields> {
|
|
7
|
+
return Schema.isSchema(schema) && schema.ast._tag === "Objects" && "fields" in schema;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// The same payload and fixed keys give the same struct, so a form is not rebuilt because a screen passed a new `fixed` object.
|
|
11
|
+
export function formFields(payload: unknown, fields: Schema.Struct<Fields> | undefined, fixed: object | undefined): Schema.Struct<Fields> {
|
|
12
|
+
if (fields !== undefined) {
|
|
13
|
+
return fields;
|
|
14
|
+
}
|
|
15
|
+
if (!isStruct(payload)) {
|
|
16
|
+
// The hook types already require `fields` here; an untyped caller learns of it during render, the way React reports a hook's misuse.
|
|
17
|
+
throw new Error("This command's payload is not a struct, so pass the form's `fields`");
|
|
18
|
+
}
|
|
19
|
+
const fixedKeys = Object.keys(fixed ?? {});
|
|
20
|
+
if (fixedKeys.length === 0) {
|
|
21
|
+
return payload;
|
|
22
|
+
}
|
|
23
|
+
const key = fixedKeys.sort((left, right) => left.localeCompare(right)).join("\u0000");
|
|
24
|
+
const byKeys = derived.get(payload) ?? new Map<string, Schema.Struct<Fields>>();
|
|
25
|
+
derived.set(payload, byKeys);
|
|
26
|
+
const existing = byKeys.get(key);
|
|
27
|
+
if (existing !== undefined) {
|
|
28
|
+
return existing;
|
|
29
|
+
}
|
|
30
|
+
const Omitted = Schema.Struct(Object.fromEntries(Object.entries(payload.fields).filter(([name]) => !fixedKeys.includes(name))));
|
|
31
|
+
byKeys.set(key, Omitted);
|
|
32
|
+
return Omitted;
|
|
33
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LooseCreate, UseCreate } from "#client/create.ts";
|
|
2
|
+
import type { LooseEditor, UseEditor } from "#client/editor.ts";
|
|
3
|
+
|
|
4
|
+
// The hooks' public types derive form fields, `fixed` and `values` from the operation with conditional types that no single implementation
|
|
5
|
+
// signature can satisfy for every instantiation. Each body runs on erased types; these name the checked type its callers see.
|
|
6
|
+
export function typedCreate(hook: LooseCreate): UseCreate;
|
|
7
|
+
export function typedCreate(hook: unknown): unknown {
|
|
8
|
+
return hook;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function typedEditor(hook: LooseEditor): UseEditor;
|
|
12
|
+
export function typedEditor(hook: unknown): unknown {
|
|
13
|
+
return hook;
|
|
14
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { Fields } from "@shivaedev/form/shape.ts";
|
|
3
|
+
|
|
4
|
+
type Values = Readonly<Record<string, unknown>>;
|
|
5
|
+
type RowValues = (row: unknown) => Values;
|
|
6
|
+
|
|
7
|
+
const encoders = new WeakMap<Schema.Struct<Fields>, RowValues>();
|
|
8
|
+
|
|
9
|
+
// The hooks derive values only for fields that encode without services, which a struct's erased field type cannot show.
|
|
10
|
+
function serviceFree(fields: Schema.Struct<Fields>): Schema.Codec<Values, Values>;
|
|
11
|
+
function serviceFree(fields: unknown): unknown {
|
|
12
|
+
return fields;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function rowValues(fields: Schema.Struct<Fields>): RowValues {
|
|
16
|
+
const existing = encoders.get(fields);
|
|
17
|
+
if (existing !== undefined) {
|
|
18
|
+
return existing;
|
|
19
|
+
}
|
|
20
|
+
const encode = Schema.encodeUnknownSync(serviceFree(fields));
|
|
21
|
+
// A loaded row already passed its schema; checking it again would turn stored data that today's checks reject into a crash instead of a field error.
|
|
22
|
+
const values: RowValues = (row) => encode(row, { disableChecks: true });
|
|
23
|
+
encoders.set(fields, values);
|
|
24
|
+
return values;
|
|
25
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Cause, Option } from "effect";
|
|
2
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import { useContext, useMemo } from "react";
|
|
4
|
+
import type { FieldRejection } from "@shivaedev/form/fieldRejection.ts";
|
|
5
|
+
import { FailureHostContext } from "#client/FailureHostContext.ts";
|
|
6
|
+
import { type Decision, decide } from "#client/failureDecision.ts";
|
|
7
|
+
import type { FailureChoices } from "#client/failures.ts";
|
|
8
|
+
import { NoticeContext } from "#client/NoticeContext.ts";
|
|
9
|
+
import type { NoticeDescriptor } from "#client/notices.ts";
|
|
10
|
+
import { unexpectedFailure } from "#client/unexpectedFailure.ts";
|
|
11
|
+
import { publishNotice } from "#noticeStore.ts";
|
|
12
|
+
|
|
13
|
+
const nothing = { _tag: "silent" } as const;
|
|
14
|
+
function noField(): undefined {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// An operation's owner presents each settled invocation once, so a failure reaches the screen or the failure host once however many
|
|
19
|
+
// components read the same result.
|
|
20
|
+
export function useFeedback<R, E>(runtime: Atom.AtomRuntime<R, E> | undefined) {
|
|
21
|
+
const store = useContext(NoticeContext);
|
|
22
|
+
const host = useContext(FailureHostContext);
|
|
23
|
+
return useMemo(
|
|
24
|
+
() => ({
|
|
25
|
+
failed: (
|
|
26
|
+
cause: Cause.Cause<unknown>,
|
|
27
|
+
input: unknown,
|
|
28
|
+
choices: FailureChoices,
|
|
29
|
+
field: (error: unknown) => FieldRejection<string> | undefined = noField,
|
|
30
|
+
): Decision<R> => {
|
|
31
|
+
const error = Cause.findErrorOption(cause);
|
|
32
|
+
if (Option.isNone(error)) {
|
|
33
|
+
if (Cause.hasDies(cause) && !choices.silent) {
|
|
34
|
+
host?.(unexpectedFailure(cause));
|
|
35
|
+
}
|
|
36
|
+
return nothing;
|
|
37
|
+
}
|
|
38
|
+
const decision = decide<R>(error.value, input, choices, field);
|
|
39
|
+
if (decision._tag === "notice" && runtime) {
|
|
40
|
+
publishNotice(store, runtime, decision.descriptor, "rejection");
|
|
41
|
+
} else if (decision._tag === "unexpected") {
|
|
42
|
+
host?.(unexpectedFailure(cause));
|
|
43
|
+
}
|
|
44
|
+
return decision;
|
|
45
|
+
},
|
|
46
|
+
succeeded: (descriptor: NoticeDescriptor<R> | undefined) => runtime && publishNotice(store, runtime, descriptor, "success"),
|
|
47
|
+
}),
|
|
48
|
+
[host, runtime, store],
|
|
49
|
+
);
|
|
50
|
+
}
|
|
@@ -26,7 +26,7 @@ export function useQueryNotice<A, E>(
|
|
|
26
26
|
seenRef.current = result;
|
|
27
27
|
if (store?.active && AsyncResult.isFailure(result)) {
|
|
28
28
|
const descriptor = latestRef.current?.({ cause: result.cause, refreshing });
|
|
29
|
-
publishNotice(store, runtime, descriptor);
|
|
29
|
+
publishNotice(store, runtime, descriptor, "rejection");
|
|
30
30
|
}
|
|
31
31
|
}, [result, refreshing, store]);
|
|
32
32
|
}
|
|
@@ -16,7 +16,7 @@ export function useStableRun(run: unknown) {
|
|
|
16
16
|
const changes = seen === committedRef.current ? 1 : seen.changes + 1;
|
|
17
17
|
if (changes === UNCOMMITTED_CHANGES) {
|
|
18
18
|
throw new Error(
|
|
19
|
-
`useAction received a new \`run\` function on ${UNCOMMITTED_CHANGES} renders in a row. \`run\` is the action's identity and must stay the same function between renders: pass a bound command such as \`api.rename
|
|
19
|
+
`useAction received a new \`run\` function on ${UNCOMMITTED_CHANGES} renders in a row. \`run\` is the action's identity and must stay the same function between renders: pass a bound command such as \`api.rename\` directly, or wrap your \`run\` in \`useCallback\` or \`useMemo\`.`,
|
|
20
20
|
);
|
|
21
21
|
}
|
|
22
22
|
see({ changes, run });
|
|
@@ -1,53 +1,76 @@
|
|
|
1
|
-
import { Effect, type Schema } from "effect";
|
|
1
|
+
import { Cause, Effect, Option, type Schema } from "effect";
|
|
2
2
|
import type * as Atom from "effect/reactivity/Atom";
|
|
3
3
|
import { useInsertionEffect, useMemo, useRef } from "react";
|
|
4
|
-
import { fieldRejectionOf
|
|
5
|
-
import type
|
|
4
|
+
import { fieldRejectionOf } from "@shivaedev/form/fieldRejection.ts";
|
|
5
|
+
import { type Decoded, FieldFailure, type Fields, type Name, type Submitter } from "@shivaedev/form/shape.ts";
|
|
6
6
|
import type { AtomServices } from "#AtomServices.ts";
|
|
7
|
-
import {
|
|
7
|
+
import { useFeedback } from "#client/adapters/useFeedback.ts";
|
|
8
|
+
import type { Decision } from "#client/failureDecision.ts";
|
|
9
|
+
import type { FailureChoices } from "#client/failures.ts";
|
|
8
10
|
import type { NoticeFeedback } from "#client/notices.ts";
|
|
9
11
|
|
|
12
|
+
function isFieldFailure(cause: Cause.Cause<unknown>) {
|
|
13
|
+
return Option.exists(Cause.findErrorOption(cause), (error) => error instanceof FieldFailure);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function placeOnField<TName extends string>(decision: Decision<unknown>, isName: (name: string) => name is TName, submitter: Submitter<TName>) {
|
|
17
|
+
if (decision._tag !== "field") {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const { field, message } = decision.rejection;
|
|
21
|
+
const name = typeof field === "string" ? field : field[0];
|
|
22
|
+
if (!isName(name)) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
return submitter.fail(typeof field === "string" ? name : [name, ...field.slice(1)], message);
|
|
26
|
+
}
|
|
27
|
+
|
|
10
28
|
export function useSubmissionCallbacks<TFields extends Fields, A, E, R, TRuntime, TRuntimeError>(
|
|
11
29
|
fields: Schema.Struct<TFields>,
|
|
12
30
|
save: (values: Decoded<TFields>) => Effect.Effect<A, E, AtomServices<R>>,
|
|
13
|
-
|
|
31
|
+
choices: FailureChoices,
|
|
14
32
|
runtime: Atom.AtomRuntime<TRuntime, TRuntimeError>,
|
|
15
|
-
notices: NoticeFeedback<Decoded<TFields>, A,
|
|
33
|
+
notices: NoticeFeedback<Decoded<TFields>, A, TRuntime> | undefined,
|
|
16
34
|
) {
|
|
17
|
-
const
|
|
18
|
-
const latestRef = useRef({
|
|
35
|
+
const feedback = useFeedback(runtime);
|
|
36
|
+
const latestRef = useRef({ choices, notices, save });
|
|
19
37
|
useInsertionEffect(() => {
|
|
20
|
-
latestRef.current = {
|
|
21
|
-
}, [
|
|
38
|
+
latestRef.current = { choices, notices, save };
|
|
39
|
+
}, [choices, notices, save]);
|
|
22
40
|
const callbacks = useMemo(() => {
|
|
23
41
|
const automatic = fieldRejectionOf(fields);
|
|
24
|
-
function
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
function isName(field: string): field is Name<TFields> {
|
|
43
|
+
return Object.hasOwn(fields.fields, field);
|
|
44
|
+
}
|
|
45
|
+
function rejected(cause: Cause.Cause<E>, values: Decoded<TFields>, submitter: Submitter<Name<TFields>>) {
|
|
46
|
+
return Effect.suspend((): Effect.Effect<never, E | FieldFailure> => {
|
|
47
|
+
// A save that already answered with a field failure has placed its own feedback.
|
|
48
|
+
if (!callbacks.active || isFieldFailure(cause)) {
|
|
49
|
+
return Effect.failCause(cause);
|
|
28
50
|
}
|
|
29
|
-
|
|
51
|
+
const decision = feedback.failed(cause, values, latestRef.current.choices, automatic);
|
|
52
|
+
return placeOnField(decision, isName, submitter) ?? Effect.failCause(cause);
|
|
53
|
+
});
|
|
30
54
|
}
|
|
31
55
|
function completed(value: A, values: Decoded<TFields>) {
|
|
32
56
|
return Effect.sync(() => {
|
|
33
57
|
if (callbacks.active) {
|
|
34
|
-
|
|
58
|
+
feedback.succeeded(latestRef.current.notices?.onSuccess?.(value, values));
|
|
35
59
|
}
|
|
36
60
|
return value;
|
|
37
61
|
});
|
|
38
62
|
}
|
|
39
63
|
return {
|
|
40
64
|
active: false,
|
|
41
|
-
|
|
42
|
-
save: (values: Decoded<TFields>): Effect.Effect<A, E, AtomServices<R>> =>
|
|
65
|
+
save: (values: Decoded<TFields>, submitter: Submitter<Name<TFields>>): Effect.Effect<A, E | FieldFailure, AtomServices<R>> =>
|
|
43
66
|
Effect.suspend(() => latestRef.current.save(values)).pipe(
|
|
44
|
-
Effect.
|
|
45
|
-
onFailure: (
|
|
67
|
+
Effect.matchCauseEffect({
|
|
68
|
+
onFailure: (cause) => rejected(cause, values, submitter),
|
|
46
69
|
onSuccess: (value) => completed(value, values),
|
|
47
70
|
}),
|
|
48
71
|
),
|
|
49
72
|
};
|
|
50
|
-
}, [
|
|
73
|
+
}, [feedback, fields]);
|
|
51
74
|
useInsertionEffect(() => {
|
|
52
75
|
callbacks.active = true;
|
|
53
76
|
return () => {
|
|
@@ -3,15 +3,13 @@ import { Effect, Option } from "effect";
|
|
|
3
3
|
import type * as AsyncResult from "effect/reactivity/AsyncResult";
|
|
4
4
|
import * as Atom from "effect/reactivity/Atom";
|
|
5
5
|
import { useContext, useMemo } from "react";
|
|
6
|
-
import type {
|
|
6
|
+
import type { BoundOperation } from "#client/boundOperation.ts";
|
|
7
7
|
import type { deployUpdate } from "#client/deployUpdate.ts";
|
|
8
|
-
import {
|
|
8
|
+
import type { FailureChoices, FailuresOption } from "#client/failures.ts";
|
|
9
|
+
import type { NoticeDescriptor } from "#client/notices.ts";
|
|
10
|
+
import { useActionState } from "#client/resultState.ts";
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
readonly run: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>;
|
|
12
|
-
readonly runtime: Atom.AtomRuntime<R, TRuntimeError>;
|
|
13
|
-
readonly update: Pick<ReturnType<typeof deployUpdate>, "block">;
|
|
14
|
-
}
|
|
12
|
+
type DeployUpdate = Pick<ReturnType<typeof deployUpdate>, "block">;
|
|
15
13
|
|
|
16
14
|
export interface UndoableNotice<TInput, A> {
|
|
17
15
|
readonly input: TInput;
|
|
@@ -30,25 +28,55 @@ export interface UndoableChange<TInput, A, E, TInverse extends readonly unknown[
|
|
|
30
28
|
readonly undoResult: AsyncResult.AsyncResult<A, E>;
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
function failuresFor<TAction>(choices: FailureChoices, input: (action: TAction) => unknown): FailureChoices {
|
|
32
|
+
const { failures, silent } = choices;
|
|
33
|
+
const handlers =
|
|
34
|
+
failures
|
|
35
|
+
&& Object.fromEntries(
|
|
36
|
+
Object.entries(failures).map(([tag, choice]) => [
|
|
37
|
+
tag,
|
|
38
|
+
typeof choice === "function" ? (error: unknown, action: TAction) => choice(error, input(action)) : choice,
|
|
39
|
+
]),
|
|
40
|
+
);
|
|
41
|
+
return { failures: handlers, silent };
|
|
42
|
+
}
|
|
43
|
+
// A failed removal or Undo hands the change's own input to `failures`.
|
|
44
|
+
type UndoableFailures<TInput, E, R, TSilent extends boolean> = FailuresOption<E, TInput, NoticeDescriptor<R>, TSilent>;
|
|
45
|
+
|
|
46
|
+
export function useUndoableChange<TInput, A, E, R, TRuntimeError, TSilent extends boolean = false>(
|
|
47
|
+
command: BoundOperation<TInput, A, E, R, TRuntimeError>,
|
|
35
48
|
options: {
|
|
36
49
|
readonly describe: (value: NoInfer<A>, input: NoInfer<TInput>) => string;
|
|
37
50
|
readonly undoInput: (value: NoInfer<A>, input: NoInfer<TInput>) => NoInfer<TInput>;
|
|
38
|
-
|
|
51
|
+
readonly update: DeployUpdate;
|
|
52
|
+
} & UndoableFailures<NoInfer<TInput>, E, R, TSilent>,
|
|
39
53
|
): UndoableChange<TInput, A, E | TRuntimeError, []>;
|
|
40
|
-
export function useUndoableChange<
|
|
41
|
-
|
|
42
|
-
|
|
54
|
+
export function useUndoableChange<
|
|
55
|
+
TInput extends { readonly expectedVersion: number },
|
|
56
|
+
A extends { readonly version: number },
|
|
57
|
+
E,
|
|
58
|
+
R,
|
|
59
|
+
TRuntimeError,
|
|
60
|
+
TSilent extends boolean = false,
|
|
61
|
+
>(
|
|
62
|
+
command: BoundOperation<TInput, A, E, R, TRuntimeError>,
|
|
63
|
+
options: { readonly describe: (value: NoInfer<A>, input: NoInfer<TInput>) => string; readonly update: DeployUpdate } & UndoableFailures<
|
|
64
|
+
NoInfer<TInput>,
|
|
65
|
+
E,
|
|
66
|
+
R,
|
|
67
|
+
TSilent
|
|
68
|
+
>,
|
|
43
69
|
): UndoableChange<TInput, A, E | TRuntimeError, [inverse: Omit<TInput, "expectedVersion">]>;
|
|
44
70
|
export function useUndoableChange<TInput, A extends { readonly version: number }, E, R, TRuntimeError>(
|
|
45
|
-
command:
|
|
71
|
+
command: BoundOperation<TInput, A, E, R, TRuntimeError>,
|
|
46
72
|
options: {
|
|
47
73
|
readonly describe: (value: A, input: TInput) => string;
|
|
48
74
|
readonly undoInput?: (value: A, input: TInput) => TInput;
|
|
49
|
-
|
|
75
|
+
readonly update: DeployUpdate;
|
|
76
|
+
} & FailureChoices,
|
|
50
77
|
) {
|
|
51
|
-
const { run, runtime
|
|
78
|
+
const { run, runtime } = command;
|
|
79
|
+
const { update } = options;
|
|
52
80
|
const registry = useContext(RegistryContext);
|
|
53
81
|
type Entry = UndoableNotice<TInput, A> & { readonly inverse: TInput };
|
|
54
82
|
interface Removal {
|
|
@@ -71,22 +99,30 @@ export function useUndoableChange<TInput, A extends { readonly version: number }
|
|
|
71
99
|
);
|
|
72
100
|
const removeRun = useMemo(() => (request: Removal) => execute(request.input), [execute]);
|
|
73
101
|
const undoRun = useMemo(() => (entry: Entry) => execute(entry.inverse), [execute]);
|
|
74
|
-
const removal =
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
102
|
+
const removal = useActionState(
|
|
103
|
+
{ run: removeRun, runtime: owner.runtime },
|
|
104
|
+
{
|
|
105
|
+
...failuresFor(options, (request: Removal) => request.input),
|
|
106
|
+
concurrent: true,
|
|
107
|
+
onSuccess: (value, { input, inverse, sequence }) => {
|
|
108
|
+
if (sequence === owner.sequence) {
|
|
109
|
+
const undoInput = options.undoInput ? options.undoInput(value, input) : { ...input, ...inverse, expectedVersion: value.version };
|
|
110
|
+
owner.registry.set(owner.notice, Option.some({ input, inverse: undoInput, message: options.describe(value, input), value }));
|
|
111
|
+
}
|
|
112
|
+
},
|
|
81
113
|
},
|
|
82
|
-
|
|
83
|
-
const restoration =
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
114
|
+
);
|
|
115
|
+
const restoration = useActionState(
|
|
116
|
+
{ run: undoRun, runtime: owner.runtime },
|
|
117
|
+
{
|
|
118
|
+
...failuresFor(options, (entry: Entry) => entry.inverse),
|
|
119
|
+
onSuccess: (_value, entry) => {
|
|
120
|
+
if (Option.getOrUndefined(owner.registry.get(owner.notice)) === entry) {
|
|
121
|
+
owner.registry.set(owner.notice, Option.none());
|
|
122
|
+
}
|
|
123
|
+
},
|
|
88
124
|
},
|
|
89
|
-
|
|
125
|
+
);
|
|
90
126
|
const notice: Option.Option<UndoableNotice<TInput, A>> = useAtomValue(owner.notice);
|
|
91
127
|
return {
|
|
92
128
|
notice,
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import type * as Atom from "effect/reactivity/Atom";
|
|
3
|
+
import type { AtomServices } from "#AtomServices.ts";
|
|
4
|
+
|
|
5
|
+
// A bound contract command fits as it is. Any other Effect becomes one by naming the runtime that provides its services.
|
|
6
|
+
export interface BoundOperation<TInput, A, E, R, TRuntimeError> {
|
|
7
|
+
// A bound query also has `run` and `runtime`; this property turns passing one into an error that says why.
|
|
8
|
+
readonly query?: "a bound query only reads: read it with useQuery, and pass a command here";
|
|
9
|
+
readonly run: (input: TInput) => Effect.Effect<A, E, AtomServices<R>>;
|
|
10
|
+
readonly runtime: Atom.AtomRuntime<R, TRuntimeError>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type LooseOperation = BoundOperation<object, unknown, unknown, unknown, unknown> & { readonly payload?: unknown };
|