trama-forms 0.1.1 → 0.2.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 CHANGED
@@ -31,18 +31,20 @@ The public entries are `trama-forms/core` and `trama-forms/react`, which point t
31
31
 
32
32
  ## Interactive guide
33
33
 
34
+ Open the guide at https://aleluman.github.io/trama/. It explains fields, sessions, React, loading with TanStack Query, value formulas, events, field state, remote select, outputs, a complete example, the API, and errors and limits. Each example shows the file that runs the demo beside it, with steps to try it. The guide documents only what is implemented. The published guide documents the latest release, and its sidebar shows the version.
35
+
36
+ To run the guide from a checkout, which can be ahead of the latest release, use these commands and open http://127.0.0.1:5173.
37
+
34
38
  ```sh
35
39
  pnpm install --frozen-lockfile
36
40
  pnpm run docs:dev
37
41
  ```
38
42
 
39
- Open http://127.0.0.1:5173. The guide (`docs/site/`) explains fields, sessions, React, loading with TanStack Query, value formulas, events, field state, remote select, outputs, a complete example, the API, and errors and limits. Each example shows the file that runs the demo beside it, with steps to try it. The guide documents only what is implemented.
40
-
41
43
  `pnpm run docs:dev` builds Trama, and the guide uses the public entries from `dist`. To rebuild the library while editing `src/`, run `pnpm run build:watch` in another terminal. If a definition changes during HMR, reload the page. `pnpm run docs:site` builds the guide in `.artifacts/site/` and `pnpm run docs:preview` serves it at http://127.0.0.1:4173.
42
44
 
43
45
  ## Development
44
46
 
45
- The pnpm version is pinned in `packageManager`, and dependencies are pinned in `pnpm-lock.yaml`. Use Node 26.9.0 (`.node-version`) or later and pnpm 12.4.2. Node is used for development and verification and is outside the runtime contract. There is no guarantee of support for CommonJS, SSR, or older browsers.
47
+ The pnpm version is pinned in `packageManager`, and dependencies are pinned in `pnpm-lock.yaml`. Use Node 26.9.0 (`.node-version`) or later and pnpm 12.4.2 for development and verification. The React binding supports server rendering and hydration. General production use in Node is outside the runtime contract. There is no guarantee of support for CommonJS or older browsers.
46
48
 
47
49
  ```sh
48
50
  pnpm run check
@@ -60,13 +62,13 @@ pnpm test
60
62
 
61
63
  - **Build.** TypeScript builds the core and the declarations. The build distributes the binding compiled with React Compiler through `oxc-transform-react` 0.145.0, without Babel. The binding uses `react/compiler-runtime`, which ships with React 19. The native compiler is experimental, and its version is pinned alongside `@vitejs/plugin-react` 6.1.1.
62
64
  - **Lint and formatting.** [Oxlint](https://oxc.rs/docs/guide/usage/linter/config) with type-aware analysis (`oxlint-tsgolint`) treats the `correctness`, `suspicious`, `pedantic`, and `perf` categories as errors and rejects unused suppressions. [Oxfmt](https://oxc.rs/docs/guide/usage/formatter) formats the code. In `docs/site/`, the `react-perf` rules for new props are disabled because React Compiler memoizes the guide. The Hooks rules remain enabled.
63
- - **Tests.** `node:test` tests the actual core, React, and TanStack Query with controlled transport, and the definition of the guide's complete example. The package test installs a `.tgz` in an isolated project, runs the core without React, checks the typing of valid and invalid uses, verifies that internal imports are rejected, and builds the guide against the installed package. Artifacts are kept in `.artifacts/package-check/`.
65
+ - **Tests.** `node:test` tests the actual core, React, and TanStack Query with controlled transport, and the definition of the guide's complete example. The package test installs a `.tgz` in an isolated project, runs the core without React, checks the typing of valid and invalid uses, verifies that internal imports are rejected, and builds the guide against the installed package, with relative asset paths and the package version in its sidebar. Artifacts are kept in `.artifacts/package-check/`.
64
66
  - **Continuous integration.** GitHub Actions runs `pnpm run check` on every pull request and on every push to `main` (`.github/workflows/ci.yml`).
65
- - **Releases.** Releases come from `aleluman/trama`. Set the version in `package.json` and push a matching tag, such as `v0.1.1`. The release workflow (`.github/workflows/release.yml`) checks that the tag matches the version, runs `pnpm run check`, and packs the package. If that version is not on npm yet, a separate job stages the tarball through npm trusted publishing, with provenance. The version becomes public only after a maintainer approves it on npmjs.com with two-factor authentication. Approve staged versions in version order, because each one keeps the `latest` tag it was staged with. On npmjs.com, the trusted publisher names this repository and the `release.yml` workflow, and it is stage-only, with "Allow npm publish" and "Allow npm dist-tag" unchecked. A new trusted publisher expires unless its first publish succeeds within two days, so create it just before pushing the tag. Provenance requires the repository to be public. Version 0.1.0 is published by hand, because trusted publishing can only be configured for a package that already exists.
67
+ - **Releases.** Releases come from `aleluman/trama`. Set the version in `package.json` and push a matching tag, such as `v0.1.1`. The release workflow (`.github/workflows/release.yml`) checks that the tag matches the version, runs `pnpm run check`, and packs the package. If that version is not on npm yet, a separate job stages the tarball through npm trusted publishing, with provenance. The version becomes public only after a maintainer approves it on npmjs.com with two-factor authentication. Approve staged versions in version order, because each one keeps the `latest` tag it was staged with. On npmjs.com, the trusted publisher names this repository and the `release.yml` workflow, and it is stage-only, with "Allow npm publish" and "Allow npm dist-tag" unchecked. A new trusted publisher expires unless its first publish succeeds within two days, so create it just before pushing the tag. Provenance requires the repository to be public. Version 0.1.0 is published by hand, because trusted publishing can only be configured for a package that already exists. The same workflow deploys the guide built by `pnpm run check` to GitHub Pages, whose source is GitHub Actions. The guide is deployed after the version is staged, or when it is already on npm, and never when staging fails. The `github-pages` environment accepts only `v*` tags.
66
68
 
67
69
  ## Project documentation
68
70
 
69
- - [Interactive guide](docs/site/) on using the library, with demos. Open it with `pnpm run docs:dev`.
71
+ - [Interactive guide](https://aleluman.github.io/trama/) on using the library, with demos. Its source is in [docs/site/](docs/site/).
70
72
  - The technical document records the current contracts, limits, decisions, and performance. `pnpm run docs:build` generates standalone HTML, and `pnpm run docs:check` checks that it is up to date. [Technical document](docs/technical.html) · [Source](docs/technical/content.html)
71
73
  - [CONTEXT.md](CONTEXT.md) is the glossary.
72
74
  - [AGENTS.md](AGENTS.md) contains the working rules.
@@ -1,7 +1,7 @@
1
1
  import { type ComponentType } from "react";
2
- import type { Option, OptionList } from "../core/index.js";
2
+ import { type FieldSource } from "./field.js";
3
3
  import type { CommonFieldProps } from "./props.js";
4
- export declare const AsyncField: ({ renderer, lists, ...props }: CommonFieldProps<unknown> & {
4
+ export declare const AsyncField: ({ renderer, source, ...props }: CommonFieldProps<unknown> & {
5
5
  readonly renderer: ComponentType<CommonFieldProps<unknown>>;
6
- readonly lists: (name: string) => OptionList<Option>;
6
+ readonly source: FieldSource;
7
7
  }) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
@@ -1,67 +1,85 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { useState, useSyncExternalStore } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
+ import { openingOf } from "./field.js";
5
+ import { sameEntries } from "./form-state.js";
4
6
  // Creates the option list of its view, from the session or from the handle of
5
7
  // a row, which only a field that is not hidden has, and adds it to the props
6
- // of the renderer with actions that keep their identity.
8
+ // of the renderer with actions that keep their identity. The server and the
9
+ // hydration render the list of the opening, when its params then differ.
7
10
  export const AsyncField = (t0) => {
8
- const $ = _c(17);
9
- let lists;
11
+ const $ = _c(21);
10
12
  let props;
11
13
  let renderer;
14
+ let source;
12
15
  if ($[0] !== t0) {
13
- ({renderer, lists, ...props} = t0);
16
+ ({renderer, source, ...props} = t0);
14
17
  $[0] = t0;
15
- $[1] = lists;
16
- $[2] = props;
17
- $[3] = renderer;
18
+ $[1] = props;
19
+ $[2] = renderer;
20
+ $[3] = source;
18
21
  } else {
19
- lists = $[1];
20
- props = $[2];
21
- renderer = $[3];
22
+ props = $[1];
23
+ renderer = $[2];
24
+ source = $[3];
22
25
  }
23
26
  let t1;
24
- if ($[4] !== lists || $[5] !== props.name) {
25
- t1 = () => lists(props.name);
26
- $[4] = lists;
27
- $[5] = props.name;
27
+ if ($[4] !== props.name || $[5] !== source) {
28
+ t1 = () => source.list(props.name);
29
+ $[4] = props.name;
30
+ $[5] = source;
28
31
  $[6] = t1;
29
32
  } else {
30
33
  t1 = $[6];
31
34
  }
32
35
  const [list] = useState(t1);
33
- const snapshot = useSyncExternalStore(list.subscribe, list.getSnapshot);
34
36
  let t2;
35
- if ($[7] !== list.more || $[8] !== list.open || $[9] !== list.retry || $[10] !== list.search || $[11] !== snapshot) {
36
- t2 = Object.freeze({
37
+ if ($[7] !== list || $[8] !== props.name || $[9] !== source.owner) {
38
+ t2 = () => {
39
+ const current = list.getSnapshot();
40
+ const opened = openingOf(source.owner)?.lists.get(props.name);
41
+ return opened === undefined || sameEntries(opened, current) ? current : opened;
42
+ };
43
+ $[7] = list;
44
+ $[8] = props.name;
45
+ $[9] = source.owner;
46
+ $[10] = t2;
47
+ } else {
48
+ t2 = $[10];
49
+ }
50
+ const server = t2;
51
+ const snapshot = useSyncExternalStore(list.subscribe, list.getSnapshot, server);
52
+ let t3;
53
+ if ($[11] !== list.more || $[12] !== list.open || $[13] !== list.retry || $[14] !== list.search || $[15] !== snapshot) {
54
+ t3 = Object.freeze({
37
55
  ...snapshot,
38
56
  open: list.open,
39
57
  search: list.search,
40
58
  more: list.more,
41
59
  retry: list.retry
42
60
  });
43
- $[7] = list.more;
44
- $[8] = list.open;
45
- $[9] = list.retry;
46
- $[10] = list.search;
47
- $[11] = snapshot;
48
- $[12] = t2;
61
+ $[11] = list.more;
62
+ $[12] = list.open;
63
+ $[13] = list.retry;
64
+ $[14] = list.search;
65
+ $[15] = snapshot;
66
+ $[16] = t3;
49
67
  } else {
50
- t2 = $[12];
68
+ t3 = $[16];
51
69
  }
52
- const view = t2;
53
- let t3;
54
- if ($[13] !== props || $[14] !== renderer || $[15] !== view) {
55
- t3 = jsx(renderer, {
70
+ const view = t3;
71
+ let t4;
72
+ if ($[17] !== props || $[18] !== renderer || $[19] !== view) {
73
+ t4 = jsx(renderer, {
56
74
  ...props,
57
75
  list: view
58
76
  });
59
- $[13] = props;
60
- $[14] = renderer;
61
- $[15] = view;
62
- $[16] = t3;
77
+ $[17] = props;
78
+ $[18] = renderer;
79
+ $[19] = view;
80
+ $[20] = t4;
63
81
  } else {
64
- t3 = $[16];
82
+ t4 = $[20];
65
83
  }
66
- return t3;
84
+ return t4;
67
85
  };
@@ -1,5 +1,15 @@
1
1
  import type { FieldDefinition, FormDefinition, Loader } from "../core/index.js";
2
2
  import type { ShowErrors } from "./visible-messages.js";
3
+ export declare const listed: {
4
+ text: false;
5
+ number: false;
6
+ checkbox: false;
7
+ select: false;
8
+ async: true;
9
+ date: false;
10
+ multiSelect: false;
11
+ multiAsync: true;
12
+ };
3
13
  export type RendererTable = Readonly<Record<string, unknown>>;
4
14
  type Forms = {
5
15
  readonly field: Readonly<Record<string, unknown>>;
@@ -1,3 +1,15 @@
1
+ // Whether the renderer of each field type takes the list of its view, as its
2
+ // props say.
3
+ export const listed = {
4
+ text: false,
5
+ number: false,
6
+ checkbox: false,
7
+ select: false,
8
+ async: true,
9
+ date: false,
10
+ multiSelect: false,
11
+ multiAsync: true
12
+ };
1
13
  // The subtype of a field, undefined for a field of a base type.
2
14
  export const subtypeOf = (item) => item.subtype;
3
15
  // How a diagnostic names the subtype of a field.
@@ -1,5 +1,5 @@
1
1
  import { type ShowErrors } from "./visible-messages.js";
2
- import type { FieldDefinition, FormDefinition, Option, OptionList, RowHandle, Session } from "../core/index.js";
2
+ import type { FieldDefinition, FormDefinition, Option, OptionList, OptionListSnapshot, RowHandle, Session } from "../core/index.js";
3
3
  export type FieldSource = {
4
4
  readonly fields: Readonly<Record<string, FieldDefinition>>;
5
5
  readonly subscribe: (listener: () => void) => () => void;
@@ -45,12 +45,20 @@ export type Selection = {
45
45
  export declare const sessionSource: (session: Session<FormDefinition>) => FieldSource;
46
46
  export declare const sourceOf: (props: object) => FieldSource;
47
47
  export declare const rowSource: (row: RowHandle) => FieldSource;
48
+ type Opened = {
49
+ readonly snapshot: unknown;
50
+ readonly lists: ReadonlyMap<string, OptionListSnapshot<Option>>;
51
+ };
52
+ export declare const openingOf: (owner: object) => Opened | undefined;
53
+ export declare const openedSnapshot: <S>(owner: object, current: () => S) => S;
54
+ export declare const rememberOpening: (session: Session<FormDefinition>) => void;
48
55
  export declare const useField: (source: FieldSource, name: string, policy: ShowErrors) => {
49
56
  id: string;
50
57
  selection: Selection | undefined;
51
58
  view: {
52
59
  subscribe: (listener: () => void) => () => void;
53
60
  select: () => Selection | undefined;
61
+ serverSelect: () => Selection | undefined;
54
62
  report: (message?: string) => void;
55
63
  edit: (next: unknown) => void;
56
64
  touch: () => void;
@@ -1,7 +1,8 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { useId, useSyncExternalStore } from "react";
3
3
  import { showsErrors, visibleErrors } from "./visible-messages.js";
4
- import { sessionKey } from "./outputs.js";
4
+ import { listed } from "./coverage.js";
5
+ import { rememberOutputs, sessionKey } from "./outputs.js";
5
6
  const sessions = new WeakMap();
6
7
  export const sessionSource = (session) => {
7
8
  let source = sessions.get(session);
@@ -53,39 +54,65 @@ export const rowSource = (row) => {
53
54
  }
54
55
  return source;
55
56
  };
57
+ const openings = new WeakMap();
58
+ export const openingOf = (owner) => openings.get(owner);
59
+ // What a server snapshot reads: the opening of a session or row the binding
60
+ // remembered, or else its current snapshot.
61
+ export const openedSnapshot = (owner, current) => {
62
+ const opened = openings.get(owner);
63
+ return opened === undefined ? current() : opened.snapshot;
64
+ };
65
+ export const rememberOpening = (session) => {
66
+ if (openings.has(session)) return;
67
+ remember(sessionSource(session));
68
+ const keysBy = session.getSnapshot().rows;
69
+ for (const [collection, keys] of Object.entries(keysBy)) for (const key of keys) remember(rowSource(session.row(collection, key)));
70
+ rememberOutputs(session);
71
+ };
72
+ const remember = (source) => {
73
+ const lists = new Map();
74
+ for (const [name, item] of Object.entries(source.fields)) if (listed[item.type]) lists.set(name, source.list(name).getSnapshot());
75
+ openings.set(source.owner, {
76
+ snapshot: source.snapshot(),
77
+ lists
78
+ });
79
+ };
56
80
  // The view of one field of a source, the same while the Field keeps its
57
81
  // source, name and policy.
58
82
  // - Its selection keeps its object while the value, the state object, the
59
83
  // reset version and the errors shown do not change: an attempt renders
60
- // again only the fields whose errors it reveals.
84
+ // again only the fields whose errors it reveals. The server and the
85
+ // hydration select from the opening with the same held selection, so React
86
+ // renders again after hydrating only the fields that changed since.
61
87
  // - Its commands: the source accepts every edit, and the binding ignores one
62
88
  // from the renderer while the field is disabled or read-only.
63
89
  const viewOf = (source, name, policy) => {
64
90
  let selected;
65
91
  let attempted = false;
66
92
  const { subscribe, report } = pendingInput(source, name);
93
+ const selectFrom = (snapshot) => {
94
+ if (snapshot === undefined) return selected = undefined;
95
+ const value = snapshot.values[name];
96
+ const state = snapshot.fieldStates[name];
97
+ const { resetVersion } = snapshot;
98
+ const scope = source.attempted(snapshot);
99
+ const held = selected;
100
+ const same = held !== undefined && Object.is(held.value, value) && held.state === state && held.resetVersion === resetVersion;
101
+ if (same && attempted === scope) return held;
102
+ attempted = scope;
103
+ const errors = visibleErrors(state, showsErrors(policy, state.touched, scope), held?.errors);
104
+ if (same && held.errors === errors) return held;
105
+ return selected = {
106
+ value,
107
+ state,
108
+ resetVersion,
109
+ errors
110
+ };
111
+ };
67
112
  return {
68
113
  subscribe,
69
- select: () => {
70
- const snapshot = source.snapshot();
71
- if (snapshot === undefined) return selected = undefined;
72
- const value = snapshot.values[name];
73
- const state = snapshot.fieldStates[name];
74
- const { resetVersion } = snapshot;
75
- const scope = source.attempted(snapshot);
76
- const held = selected;
77
- const same = held !== undefined && Object.is(held.value, value) && held.state === state && held.resetVersion === resetVersion;
78
- if (same && attempted === scope) return held;
79
- attempted = scope;
80
- const errors = visibleErrors(state, showsErrors(policy, state.touched, scope), held?.errors);
81
- if (same && held.errors === errors) return held;
82
- return selected = {
83
- value,
84
- state,
85
- resetVersion,
86
- errors
87
- };
88
- },
114
+ select: () => selectFrom(source.snapshot()),
115
+ serverSelect: () => selectFrom(openedSnapshot(source.owner, source.snapshot)),
89
116
  report,
90
117
  edit: (next) => {
91
118
  const state = source.snapshot()?.fieldStates[name];
@@ -164,7 +191,7 @@ export const useField = (source, name, policy) => {
164
191
  t0 = $[3];
165
192
  }
166
193
  const view = t0;
167
- const selection = useSyncExternalStore(view.subscribe, view.select);
194
+ const selection = useSyncExternalStore(view.subscribe, view.select, view.serverSelect);
168
195
  let t1;
169
196
  if ($[4] !== source.owner) {
170
197
  t1 = sessionKey(source.owner);
@@ -11,4 +11,5 @@ type Scope = {
11
11
  type View = Omit<Scope, "externalErrors"> & Readonly<Record<string, unknown>>;
12
12
  type Source = Session<FormDefinition> | RowHandle;
13
13
  export declare const formStateOf: (policy: ShowErrors) => (source: Source, collection?: string) => View | undefined;
14
+ export declare const sameEntries: (a: object, b: object) => boolean;
14
15
  export {};
@@ -5,11 +5,12 @@
5
5
  // It keeps its object while that content does not change, so the hook of a
6
6
  // row does not render for another, and a removed row gives undefined.
7
7
  import { useMemo, useSyncExternalStore } from "react";
8
+ import { openedSnapshot } from "./field.js";
8
9
  import { showsErrors, visibleErrors } from "./visible-messages.js";
9
10
  export const formStateOf = (policy) => (source, collection) => {
10
11
  if ("definition" in source && collection !== undefined && !hasCollection(source, collection)) throw new Error(`Unknown collection "${collection}"`);
11
12
  const select = useMemo(() => selectorOf(source, collection, policy), [source, collection]);
12
- return useSyncExternalStore(source.subscribe, select);
13
+ return useSyncExternalStore(source.subscribe, select.current, select.server);
13
14
  };
14
15
  const hasCollection = (session, name) => Object.hasOwn(session.definition.collections, name);
15
16
  // What a scope reads of the snapshot of its source: the form without its
@@ -17,7 +18,8 @@ const hasCollection = (session, name) => Object.hasOwn(session.definition.collec
17
18
  // version and queries, and whether it shows its own errors. Its view has the
18
19
  // errors shown in place of the two lists, and keeps its object while the
19
20
  // scope read and whether it shows are the same, and, when they are not, while
20
- // its content is.
21
+ // its content is. The server and the hydration read the opening of the
22
+ // source through the same view.
21
23
  const selectorOf = (source, collection, policy) => {
22
24
  let held;
23
25
  let heldShown = false;
@@ -31,22 +33,28 @@ const selectorOf = (source, collection, policy) => {
31
33
  ...withoutKeys(scope, omitted),
32
34
  errors
33
35
  };
34
- if (last === undefined || !sameView(last, candidate)) last = Object.freeze(candidate);
36
+ if (last === undefined || !sameEntries(last, candidate)) last = Object.freeze(candidate);
35
37
  return last;
36
38
  };
37
- if ("definition" in source) return () => {
38
- const { form } = source.getSnapshot();
39
- if (collection !== undefined) {
39
+ if ("definition" in source) {
40
+ const read = ({ form }) => {
41
+ if (collection === undefined) return view(form, form.attempted, ofForm);
40
42
  const scope = form.collections[collection];
41
43
  return view(scope, form.attempted, external);
42
- }
43
- return view(form, form.attempted, ofForm);
44
- };
45
- return () => {
46
- const row = source.getSnapshot();
44
+ };
45
+ return {
46
+ current: () => read(source.getSnapshot()),
47
+ server: () => read(openedSnapshot(source, () => source.getSnapshot()))
48
+ };
49
+ }
50
+ const read = (row) => {
47
51
  if (row === undefined) return last = undefined;
48
52
  return view(row, showsErrors(policy, row.touched, row.attempted), ofRow);
49
53
  };
54
+ return {
55
+ current: () => read(source.getSnapshot()),
56
+ server: () => read(openedSnapshot(source, () => source.getSnapshot()))
57
+ };
50
58
  };
51
59
  // What a view leaves out of each scope: the external errors, which are among
52
60
  // its errors, and what is not of the scope alone.
@@ -64,7 +72,10 @@ const withoutKeys = (scope, keys) => {
64
72
  for (const key of Object.keys(scope)) if (!keys.includes(key)) rest[key] = scope[key];
65
73
  return rest;
66
74
  };
67
- const sameView = (a, b) => {
68
- const keys = Object.keys(b);
69
- return keys.length === Object.keys(a).length && keys.every((key) => a[key] === b[key]);
75
+ // Whether two objects have the same keys with the same values.
76
+ export const sameEntries = (a, b) => {
77
+ const left = a;
78
+ const right = b;
79
+ const keys = Object.keys(right);
80
+ return keys.length === Object.keys(left).length && keys.every((key) => left[key] === right[key]);
70
81
  };
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  import { bind } from "./binding.js";
2
3
  export { OutputBoundary, useOutputs } from "./outputs.js";
3
4
  export { useRows } from "./rows.js";
@@ -1,6 +1,7 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { useEffect, useLayoutEffect, useState, useSyncExternalStore } from "react";
3
3
  import { createSession as createCoreSession } from "../core/index.js";
4
+ import { rememberOpening } from "./field.js";
4
5
  // The core requires scope for a form with async fields and the context a form
5
6
  // declares, which a generic definition cannot show; useFormSession requires
6
7
  // them in types.
@@ -46,6 +47,10 @@ class Opening {
46
47
  return this.#result.status !== "ready" || sameData(this.#context, context);
47
48
  }
48
49
  getSnapshot = () => this.#result;
50
+ getServerSnapshot = () => {
51
+ if (this.#result.status === "ready") rememberOpening(this.#result.session);
52
+ return this.#result;
53
+ };
49
54
  subscribe = (listener) => {
50
55
  this.#listeners.add(listener);
51
56
  return () => {
@@ -193,7 +198,7 @@ export const useSession = (definition, options, loader) => {
193
198
  t4 = $[12];
194
199
  }
195
200
  useEffect(t4);
196
- return useSyncExternalStore(opening.subscribe, opening.getSnapshot);
201
+ return useSyncExternalStore(opening.subscribe, opening.getSnapshot, opening.getServerSnapshot);
197
202
  };
198
203
  // Whether two values are equal as data, as the core compares them: the
199
204
  // binding reaches the core only through its public entry.
@@ -6,6 +6,7 @@ type BoundaryProps<D extends FormDefinition> = {
6
6
  children?: ReactNode;
7
7
  };
8
8
  export declare const sessionKey: (session: object) => number;
9
+ export declare const rememberOutputs: (session: Session<FormDefinition>) => void;
9
10
  export declare const OutputBoundary: <D extends FormDefinition>(props: BoundaryProps<D>) => ReactNode;
10
11
  export declare const useOutputs: <D extends FormDefinition>(session: Session<D>) => FormOutputs<D>;
11
12
  export {};
@@ -14,6 +14,21 @@ export const sessionKey = (session) => {
14
14
  }
15
15
  return key;
16
16
  };
17
+ // The outputs of a session at its opening, which the server and the hydration
18
+ // render: the view of watchOutputs reads the evaluations it was given.
19
+ const openedOutputs = new WeakMap();
20
+ export const rememberOutputs = (session) => {
21
+ openedOutputs.set(session, outputsOf(session));
22
+ };
23
+ // The current outputs of a session: watchOutputs gives them to its observer
24
+ // at once.
25
+ const outputsOf = (session) => {
26
+ let current;
27
+ session.watchOutputs((outputs) => {
28
+ current = outputs;
29
+ })();
30
+ return current;
31
+ };
17
32
  export const OutputBoundary = (props) => createElement(CatchOutput, {
18
33
  ...props,
19
34
  key: sessionKey(props.session)
@@ -78,7 +93,7 @@ const RecoverOutput = (t0) => {
78
93
  export const useOutputs = (session) => {
79
94
  "use no memo";
80
95
  const selection = selectOutputs(session);
81
- const snapshot = useSyncExternalStore(selection.subscribe, selection.getSnapshot);
96
+ const snapshot = useSyncExternalStore(selection.subscribe, selection.getSnapshot, selection.getServerSnapshot);
82
97
  useLayoutEffect(() => {
83
98
  selection.finishRender();
84
99
  });
@@ -89,20 +104,14 @@ const selectOutputs = (session) => {
89
104
  failures.set(session, errors);
90
105
  const names = new Set();
91
106
  let tracking = true;
92
- const capture = () => {
93
- let current;
94
- session.watchOutputs((outputs) => {
95
- current = outputs;
96
- })();
97
- return current;
98
- };
99
- let snapshot = capture();
107
+ let snapshot = outputsOf(session);
100
108
  const update = (current) => {
101
109
  if ([...names].some((name) => !sameRead(snapshot, current, name))) snapshot = current;
102
110
  return snapshot;
103
111
  };
104
112
  return {
105
- getSnapshot: () => update(capture()),
113
+ getSnapshot: () => update(outputsOf(session)),
114
+ getServerSnapshot: () => snapshot = openedOutputs.get(session) ?? snapshot,
106
115
  subscribe: (notify) => session.watchOutputs((current) => {
107
116
  const previous = snapshot;
108
117
  for (const name of names) read(current, name);
@@ -2,7 +2,7 @@ import { type RendererTable } from "./coverage.js";
2
2
  import type { ShowErrors } from "./visible-messages.js";
3
3
  import { type FieldSource } from "./field.js";
4
4
  import type { AsyncFieldProps, CheckboxFieldProps, CommonFieldProps, DateFieldProps, MultiAsyncFieldProps, MultiSelectFieldProps, NumberFieldProps, SelectFieldProps, TextFieldProps, ViewProps } from "./props.js";
5
- type Props = {
5
+ export type Props = {
6
6
  text: TextFieldProps;
7
7
  number: NumberFieldProps;
8
8
  checkbox: CheckboxFieldProps;
@@ -1,7 +1,7 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { jsx } from "react/jsx-runtime";
3
3
  import { AsyncField } from "./async.js";
4
- import { rendererOf, subtypeOf } from "./coverage.js";
4
+ import { listed, rendererOf, subtypeOf } from "./coverage.js";
5
5
  import { useField } from "./field.js";
6
6
  // What each field type adds to the common props of its renderer, from its
7
7
  // definition and its state. Every field type of the core needs one, with a
@@ -27,18 +27,6 @@ export const typeProps = {
27
27
  multiSelect: optionProps,
28
28
  multiAsync: placeholderProps
29
29
  };
30
- // Whether the renderer of each field type takes the list of its view, as its
31
- // props say.
32
- const listed = {
33
- text: false,
34
- number: false,
35
- checkbox: false,
36
- select: false,
37
- async: true,
38
- date: false,
39
- multiSelect: false,
40
- multiAsync: true
41
- };
42
30
  // Renders the field name of a source, or nothing for a row that no longer
43
31
  // exists or a hidden field. The props of a Field override the label and the
44
32
  // placeholder of the state.
@@ -56,7 +44,7 @@ export const useFieldRenderer = (renderers, source, props, policy) => {
56
44
  const { value, state, resetVersion, errors } = selection;
57
45
  const t0 = props.label ?? state.label;
58
46
  let t1;
59
- if ($[0] !== errors || $[1] !== id || $[2] !== item || $[3] !== key || $[4] !== name || $[5] !== props.placeholder || $[6] !== renderers || $[7] !== resetVersion || $[8] !== source.list || $[9] !== state || $[10] !== t0 || $[11] !== value || $[12] !== view.edit || $[13] !== view.report || $[14] !== view.touch) {
47
+ if ($[0] !== errors || $[1] !== id || $[2] !== item || $[3] !== key || $[4] !== name || $[5] !== props.placeholder || $[6] !== renderers || $[7] !== resetVersion || $[8] !== source || $[9] !== state || $[10] !== t0 || $[11] !== value || $[12] !== view.edit || $[13] !== view.report || $[14] !== view.touch) {
60
48
  const common = {
61
49
  id,
62
50
  name,
@@ -71,7 +59,7 @@ export const useFieldRenderer = (renderers, source, props, policy) => {
71
59
  onBlur: view.touch,
72
60
  onInputError: view.report
73
61
  };
74
- t1 = renderField(renderers, source.list, item, value, view.edit, common, {
62
+ t1 = renderField(renderers, source, item, value, view.edit, common, {
75
63
  placeholder: props.placeholder ?? state.placeholder,
76
64
  state,
77
65
  key: `${key}:${resetVersion}`
@@ -84,7 +72,7 @@ export const useFieldRenderer = (renderers, source, props, policy) => {
84
72
  $[5] = props.placeholder;
85
73
  $[6] = renderers;
86
74
  $[7] = resetVersion;
87
- $[8] = source.list;
75
+ $[8] = source;
88
76
  $[9] = state;
89
77
  $[10] = t0;
90
78
  $[11] = value;
@@ -100,7 +88,7 @@ export const useFieldRenderer = (renderers, source, props, policy) => {
100
88
  // The element of the renderer, whose key, out of its props, remounts it with
101
89
  // another source, name or reset: jsx takes the props as they are, without
102
90
  // copying them.
103
- const renderField = (renderers, lists, item, value, edit, common, { placeholder, state, key }) => {
91
+ const renderField = (renderers, source, item, value, edit, common, { placeholder, state, key }) => {
104
92
  const own = typeProps[item.type](item, placeholder, state);
105
93
  const Renderer = rendererOf(renderers, item, `Field "${common.name}"`);
106
94
  const props = {
@@ -113,7 +101,7 @@ const renderField = (renderers, lists, item, value, edit, common, { placeholder,
113
101
  if (listed[item.type]) return jsx(AsyncField, {
114
102
  ...props,
115
103
  renderer: Renderer,
116
- lists
104
+ source
117
105
  }, key);
118
106
  return jsx(Renderer, props, key);
119
107
  };
@@ -1,19 +1,15 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { useSyncExternalStore } from "react";
3
+ import { openedSnapshot } from "./field.js";
3
4
  // The keys of the rows of a collection, in visual order. The array keeps its
4
5
  // identity while the rows of the collection and their order do not change, so
5
- // a table that renders them does not render again on every key stroke.
6
+ // a table that renders them does not render again on every key stroke. The
7
+ // server and the hydration render the keys of the opening.
6
8
  export const useRows = (session, collection) => {
7
- const $ = _c(3);
9
+ const $ = _c(6);
8
10
  let t0;
9
11
  if ($[0] !== collection || $[1] !== session) {
10
- t0 = () => {
11
- const keys = session.getSnapshot().rows[collection];
12
- if (keys === undefined) {
13
- throw new Error(`Unknown collection "${collection}"`);
14
- }
15
- return keys;
16
- };
12
+ t0 = () => keysOf(session.getSnapshot(), collection);
17
13
  $[0] = collection;
18
14
  $[1] = session;
19
15
  $[2] = t0;
@@ -21,5 +17,20 @@ export const useRows = (session, collection) => {
21
17
  t0 = $[2];
22
18
  }
23
19
  const select = t0;
24
- return useSyncExternalStore(session.subscribe, select);
20
+ let t1;
21
+ if ($[3] !== collection || $[4] !== session) {
22
+ t1 = () => keysOf(openedSnapshot(session, () => session.getSnapshot()), collection);
23
+ $[3] = collection;
24
+ $[4] = session;
25
+ $[5] = t1;
26
+ } else {
27
+ t1 = $[5];
28
+ }
29
+ const server = t1;
30
+ return useSyncExternalStore(session.subscribe, select, server);
31
+ };
32
+ const keysOf = (snapshot, collection) => {
33
+ const keys = snapshot.rows[collection];
34
+ if (keys === undefined) throw new Error(`Unknown collection "${collection}"`);
35
+ return keys;
25
36
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trama-forms",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Declarative forms library for modern browsers.",
5
5
  "keywords": [
6
6
  "declarative",