trama-forms 0.0.0-stage → 0.1.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.
Files changed (225) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +83 -2
  3. package/dist/core/acceptance.d.ts +15 -0
  4. package/dist/core/acceptance.js +37 -0
  5. package/dist/core/address.d.ts +45 -0
  6. package/dist/core/address.js +68 -0
  7. package/dist/core/authoring-checks.d.ts +39 -0
  8. package/dist/core/authoring-checks.js +1 -0
  9. package/dist/core/authoring-form.d.ts +23 -0
  10. package/dist/core/authoring-form.js +1 -0
  11. package/dist/core/authoring-params.d.ts +37 -0
  12. package/dist/core/authoring-params.js +1 -0
  13. package/dist/core/authoring-relations.d.ts +46 -0
  14. package/dist/core/authoring-relations.js +1 -0
  15. package/dist/core/authoring-rows.d.ts +109 -0
  16. package/dist/core/authoring-rows.js +1 -0
  17. package/dist/core/authoring-values.d.ts +36 -0
  18. package/dist/core/authoring-values.js +1 -0
  19. package/dist/core/authoring.d.ts +117 -0
  20. package/dist/core/authoring.js +1 -0
  21. package/dist/core/baseline.d.ts +12 -0
  22. package/dist/core/baseline.js +66 -0
  23. package/dist/core/candidates.d.ts +26 -0
  24. package/dist/core/candidates.js +155 -0
  25. package/dist/core/collections.d.ts +59 -0
  26. package/dist/core/collections.js +138 -0
  27. package/dist/core/commands.d.ts +15 -0
  28. package/dist/core/commands.js +64 -0
  29. package/dist/core/compile.d.ts +18 -0
  30. package/dist/core/compile.js +60 -0
  31. package/dist/core/compiled.d.ts +34 -0
  32. package/dist/core/compiled.js +12 -0
  33. package/dist/core/config.d.ts +103 -0
  34. package/dist/core/config.js +1 -0
  35. package/dist/core/cycles.d.ts +14 -0
  36. package/dist/core/cycles.js +39 -0
  37. package/dist/core/data.d.ts +40 -0
  38. package/dist/core/data.js +236 -0
  39. package/dist/core/declarations.d.ts +61 -0
  40. package/dist/core/declarations.js +202 -0
  41. package/dist/core/definition.d.ts +54 -0
  42. package/dist/core/definition.js +3 -0
  43. package/dist/core/derived-fields.d.ts +37 -0
  44. package/dist/core/derived-fields.js +121 -0
  45. package/dist/core/derived-scope.d.ts +55 -0
  46. package/dist/core/derived-scope.js +105 -0
  47. package/dist/core/derived.d.ts +76 -0
  48. package/dist/core/derived.js +180 -0
  49. package/dist/core/endpoints.d.ts +94 -0
  50. package/dist/core/endpoints.js +95 -0
  51. package/dist/core/engine-types.d.ts +43 -0
  52. package/dist/core/engine-types.js +1 -0
  53. package/dist/core/engine.d.ts +9 -0
  54. package/dist/core/engine.js +140 -0
  55. package/dist/core/evaluation.d.ts +20 -0
  56. package/dist/core/evaluation.js +62 -0
  57. package/dist/core/external.d.ts +13 -0
  58. package/dist/core/external.js +49 -0
  59. package/dist/core/field-definitions.d.ts +103 -0
  60. package/dist/core/field-definitions.js +20 -0
  61. package/dist/core/field-traits.d.ts +29 -0
  62. package/dist/core/field-traits.js +149 -0
  63. package/dist/core/fields.d.ts +26 -0
  64. package/dist/core/fields.js +89 -0
  65. package/dist/core/forms.d.ts +23 -0
  66. package/dist/core/forms.js +46 -0
  67. package/dist/core/handles.d.ts +56 -0
  68. package/dist/core/handles.js +128 -0
  69. package/dist/core/index.d.ts +13 -0
  70. package/dist/core/index.js +7 -0
  71. package/dist/core/invalidation.d.ts +30 -0
  72. package/dist/core/invalidation.js +161 -0
  73. package/dist/core/issues.d.ts +22 -0
  74. package/dist/core/issues.js +60 -0
  75. package/dist/core/known-keys.d.ts +4 -0
  76. package/dist/core/known-keys.js +22 -0
  77. package/dist/core/lists.d.ts +27 -0
  78. package/dist/core/lists.js +174 -0
  79. package/dist/core/lock.d.ts +4 -0
  80. package/dist/core/lock.js +70 -0
  81. package/dist/core/marking.d.ts +16 -0
  82. package/dist/core/marking.js +28 -0
  83. package/dist/core/marks.d.ts +32 -0
  84. package/dist/core/marks.js +203 -0
  85. package/dist/core/memory.d.ts +79 -0
  86. package/dist/core/memory.js +127 -0
  87. package/dist/core/messages.d.ts +20 -0
  88. package/dist/core/messages.js +46 -0
  89. package/dist/core/multiple.d.ts +12 -0
  90. package/dist/core/multiple.js +123 -0
  91. package/dist/core/notify.d.ts +32 -0
  92. package/dist/core/notify.js +160 -0
  93. package/dist/core/observation.d.ts +7 -0
  94. package/dist/core/observation.js +80 -0
  95. package/dist/core/observe.d.ts +19 -0
  96. package/dist/core/observe.js +59 -0
  97. package/dist/core/operation.d.ts +8 -0
  98. package/dist/core/operation.js +48 -0
  99. package/dist/core/options.d.ts +40 -0
  100. package/dist/core/options.js +32 -0
  101. package/dist/core/outputs.d.ts +58 -0
  102. package/dist/core/outputs.js +149 -0
  103. package/dist/core/param-memory.d.ts +13 -0
  104. package/dist/core/param-memory.js +17 -0
  105. package/dist/core/params.d.ts +37 -0
  106. package/dist/core/params.js +70 -0
  107. package/dist/core/pass.d.ts +6 -0
  108. package/dist/core/pass.js +141 -0
  109. package/dist/core/patches.d.ts +15 -0
  110. package/dist/core/patches.js +106 -0
  111. package/dist/core/prepare.d.ts +19 -0
  112. package/dist/core/prepare.js +105 -0
  113. package/dist/core/prepared.d.ts +10 -0
  114. package/dist/core/prepared.js +14 -0
  115. package/dist/core/program.d.ts +43 -0
  116. package/dist/core/program.js +100 -0
  117. package/dist/core/publication.d.ts +20 -0
  118. package/dist/core/publication.js +56 -0
  119. package/dist/core/published.d.ts +18 -0
  120. package/dist/core/published.js +83 -0
  121. package/dist/core/queries.d.ts +10 -0
  122. package/dist/core/queries.js +148 -0
  123. package/dist/core/query-batch.d.ts +2 -0
  124. package/dist/core/query-batch.js +67 -0
  125. package/dist/core/query-entries.d.ts +78 -0
  126. package/dist/core/query-entries.js +139 -0
  127. package/dist/core/query-response.d.ts +2 -0
  128. package/dist/core/query-response.js +110 -0
  129. package/dist/core/reads.d.ts +30 -0
  130. package/dist/core/reads.js +88 -0
  131. package/dist/core/rebase.d.ts +16 -0
  132. package/dist/core/rebase.js +40 -0
  133. package/dist/core/relations.d.ts +10 -0
  134. package/dist/core/relations.js +84 -0
  135. package/dist/core/remote-fields.d.ts +6 -0
  136. package/dist/core/remote-fields.js +29 -0
  137. package/dist/core/resolution.d.ts +12 -0
  138. package/dist/core/resolution.js +157 -0
  139. package/dist/core/restarts.d.ts +17 -0
  140. package/dist/core/restarts.js +42 -0
  141. package/dist/core/revisions.d.ts +11 -0
  142. package/dist/core/revisions.js +57 -0
  143. package/dist/core/row-arrays.d.ts +12 -0
  144. package/dist/core/row-arrays.js +47 -0
  145. package/dist/core/row-maps.d.ts +16 -0
  146. package/dist/core/row-maps.js +52 -0
  147. package/dist/core/row-states.d.ts +68 -0
  148. package/dist/core/row-states.js +75 -0
  149. package/dist/core/row-types.d.ts +76 -0
  150. package/dist/core/row-types.js +1 -0
  151. package/dist/core/rule-set-config.d.ts +56 -0
  152. package/dist/core/rule-set-config.js +1 -0
  153. package/dist/core/rule-set-types.d.ts +91 -0
  154. package/dist/core/rule-set-types.js +1 -0
  155. package/dist/core/rule-sets.d.ts +15 -0
  156. package/dist/core/rule-sets.js +107 -0
  157. package/dist/core/rules.d.ts +67 -0
  158. package/dist/core/rules.js +144 -0
  159. package/dist/core/saving.d.ts +55 -0
  160. package/dist/core/saving.js +132 -0
  161. package/dist/core/scope-declarations.d.ts +13 -0
  162. package/dist/core/scope-declarations.js +18 -0
  163. package/dist/core/scope-messages.d.ts +9 -0
  164. package/dist/core/scope-messages.js +46 -0
  165. package/dist/core/scope-states.d.ts +50 -0
  166. package/dist/core/scope-states.js +222 -0
  167. package/dist/core/session-external.d.ts +24 -0
  168. package/dist/core/session-external.js +138 -0
  169. package/dist/core/session-lists.d.ts +12 -0
  170. package/dist/core/session-lists.js +60 -0
  171. package/dist/core/session-marks.d.ts +22 -0
  172. package/dist/core/session-marks.js +113 -0
  173. package/dist/core/session-parts.d.ts +26 -0
  174. package/dist/core/session-parts.js +91 -0
  175. package/dist/core/session-rows.d.ts +15 -0
  176. package/dist/core/session-rows.js +62 -0
  177. package/dist/core/session-types.d.ts +112 -0
  178. package/dist/core/session-types.js +1 -0
  179. package/dist/core/session.d.ts +5 -0
  180. package/dist/core/session.js +51 -0
  181. package/dist/core/state-evaluation.d.ts +10 -0
  182. package/dist/core/state-evaluation.js +69 -0
  183. package/dist/core/state-properties.d.ts +30 -0
  184. package/dist/core/state-properties.js +101 -0
  185. package/dist/core/states.d.ts +73 -0
  186. package/dist/core/states.js +180 -0
  187. package/dist/core/steps.d.ts +37 -0
  188. package/dist/core/steps.js +183 -0
  189. package/dist/core/subtype-types.d.ts +67 -0
  190. package/dist/core/subtype-types.js +1 -0
  191. package/dist/core/subtypes.d.ts +19 -0
  192. package/dist/core/subtypes.js +69 -0
  193. package/dist/core/tree.d.ts +44 -0
  194. package/dist/core/tree.js +119 -0
  195. package/dist/core/validation.d.ts +11 -0
  196. package/dist/core/validation.js +42 -0
  197. package/dist/core/views.d.ts +24 -0
  198. package/dist/core/views.js +161 -0
  199. package/dist/react/async.d.ts +7 -0
  200. package/dist/react/async.js +67 -0
  201. package/dist/react/binding.d.ts +15 -0
  202. package/dist/react/binding.js +51 -0
  203. package/dist/react/coverage.d.ts +21 -0
  204. package/dist/react/coverage.js +91 -0
  205. package/dist/react/field.d.ts +60 -0
  206. package/dist/react/field.js +194 -0
  207. package/dist/react/form-state.d.ts +14 -0
  208. package/dist/react/form-state.js +70 -0
  209. package/dist/react/index.d.ts +152 -0
  210. package/dist/react/index.js +6 -0
  211. package/dist/react/opening.d.ts +38 -0
  212. package/dist/react/opening.js +206 -0
  213. package/dist/react/outputs.d.ts +11 -0
  214. package/dist/react/outputs.js +137 -0
  215. package/dist/react/props.d.ts +48 -0
  216. package/dist/react/props.js +1 -0
  217. package/dist/react/query.d.ts +11 -0
  218. package/dist/react/query.js +21 -0
  219. package/dist/react/render.d.ts +49 -0
  220. package/dist/react/render.js +119 -0
  221. package/dist/react/rows.d.ts +2 -0
  222. package/dist/react/rows.js +25 -0
  223. package/dist/react/visible-messages.d.ts +8 -0
  224. package/dist/react/visible-messages.js +16 -0
  225. package/package.json +77 -4
@@ -0,0 +1,128 @@
1
+ // The handle of a row: the same object while its row exists, bound to its key
2
+ // and checked on every call. It has the members a binding uses of a session,
3
+ // and no reference to the session, so a draft could take its place.
4
+ import { fieldLabel, rowAddress, } from "./address.js";
5
+ import { rowIn } from "./commands.js";
6
+ import { describe } from "./data.js";
7
+ import { fieldAt, validateValue } from "./fields.js";
8
+ import { recordIn } from "./tree.js";
9
+ // The handles of a session: one per row, the same while the row lives. A reset
10
+ // or a commit brings back the very rows of its baseline, so their handles too;
11
+ // those of rows that leave go with them.
12
+ export const createHandles = (host) => {
13
+ const handles = new WeakMap();
14
+ return (row) => {
15
+ let found = handles.get(row);
16
+ if (found === undefined)
17
+ handles.set(row, (found = createHandle(host, row)));
18
+ return found;
19
+ };
20
+ };
21
+ const createHandle = (host, own) => {
22
+ const { collection, key } = own;
23
+ const rowOf = () => currentRow(host, collection, key);
24
+ return Object.freeze({
25
+ collection: collection.name,
26
+ key,
27
+ fields: collection.fields,
28
+ getSnapshot: snapshots(host, own),
29
+ subscribe: (listener) => host.subscribe(listener),
30
+ edit: (name, value) => {
31
+ host.guard("edit");
32
+ const row = rowOf();
33
+ validateValue(name, itemOf(collection, name), value, fieldLabel(name, row));
34
+ host.edit(new Map([[rowAddress(row, name), value]]));
35
+ },
36
+ // A new option list for one view of an async field of the row, which may
37
+ // be created while the session notifies, as those of the session.
38
+ list: (name) => {
39
+ const row = rowOf();
40
+ itemOf(collection, name);
41
+ return host.list(rowAddress(row, name));
42
+ },
43
+ // A command, as edit.
44
+ retry: (name) => {
45
+ host.guard("retry");
46
+ const row = rowOf();
47
+ if (typeof name !== "string" || !collection.queries.has(name))
48
+ throw new Error(`Field ${describe(name)} of "${collection.name}" has no query`);
49
+ host.retry(rowAddress(row, name));
50
+ },
51
+ // A command, as edit: the row, or one of its cells.
52
+ touch: (name) => {
53
+ host.guard("touch");
54
+ const row = rowOf();
55
+ host.touch(row, name === undefined ? undefined : cellOf(collection, row, "touch", name));
56
+ },
57
+ // A controller may be created while the session notifies, as a list.
58
+ input: (name) => host.input(cellOf(collection, rowOf(), "input", name)),
59
+ });
60
+ };
61
+ // A cell of a row, for touch and input, whose diagnostics name the command.
62
+ const cellOf = (collection, row, command, name) => {
63
+ if (fieldAt(collection.fields, name) === undefined)
64
+ throw new Error(`Collection "${collection.name}": ${command}: field ${describe(name)} does not exist`);
65
+ return rowAddress(row, name);
66
+ };
67
+ // What edit and list check first: that the row exists, and then that it has
68
+ // the field.
69
+ const currentRow = (host, collection, key) => {
70
+ const row = rowIn(host.published().store, collection, key);
71
+ if (row === undefined)
72
+ throw new Error(`Collection "${collection.name}": row "${key}" was removed`);
73
+ return row;
74
+ };
75
+ const itemOf = (collection, name) => {
76
+ const item = fieldAt(collection.fields, name);
77
+ if (item === undefined)
78
+ throw new Error(`Collection "${collection.name}": unknown field "${String(name)}"`);
79
+ return item;
80
+ };
81
+ // The same snapshot while the values, the states, the states of the queries,
82
+ // the scope and the reset version of its row do not change; undefined while
83
+ // its row does not exist. A scope keeps its object while its content does.
84
+ // A row is attempted when its sequence is within the frontier of the last attempt.
85
+ const snapshots = (host, row) => {
86
+ const { collection, key, seq } = row;
87
+ let cached;
88
+ let held;
89
+ // Whether the row is touched, read again only when the version changes.
90
+ let version = -1;
91
+ let touched = false;
92
+ return () => {
93
+ const { store, rowStates, rowScopes } = host.published();
94
+ const resetVersion = host.resetVersion();
95
+ const rows = store.collections.get(collection.name);
96
+ const values = rows && recordIn(rows.records, key);
97
+ const fieldStates = rowStates.get(key);
98
+ const scope = rowScopes.get(key);
99
+ if (values === undefined || fieldStates === undefined || scope === undefined)
100
+ return;
101
+ const queries = host.queries(collection, key);
102
+ const frontier = host.attempt();
103
+ const attempted = frontier !== undefined && seq <= frontier;
104
+ if (host.touchVersion() !== version) {
105
+ version = host.touchVersion();
106
+ touched = host.touched(row);
107
+ }
108
+ if (cached?.values !== values ||
109
+ cached.fieldStates !== fieldStates ||
110
+ cached.queries !== queries ||
111
+ cached.resetVersion !== resetVersion ||
112
+ cached.attempted !== attempted ||
113
+ cached.touched !== touched ||
114
+ held !== scope) {
115
+ held = scope;
116
+ cached = Object.freeze({
117
+ values,
118
+ fieldStates,
119
+ resetVersion,
120
+ queries,
121
+ ...scope,
122
+ attempted,
123
+ touched,
124
+ });
125
+ }
126
+ return cached;
127
+ };
128
+ };
@@ -0,0 +1,13 @@
1
+ export { defineForm } from "./compile.js";
2
+ export { defineRules } from "./rule-sets.js";
3
+ export type { RuleSet } from "./rule-set-types.js";
4
+ export { field, declareContext, type ContextDeclaration, type FormContext, type TextDefinition, type NumberDefinition, type CheckboxDefinition, type SelectOption, type SelectDefinition, type AsyncDefinition, type DateDefinition, type MultiSelectDefinition, type MultiAsyncDefinition, type FieldDefinition, type FieldValue, type FormDefinition, type FormValues, type FormOutputs, type FormQueries, type RowQueries, } from "./definition.js";
5
+ export type { CollectionName, FormInitial, FormRows, RowHandle, RowHandleOf, RowKey, RowSnapshot, RowStates, RowValues, } from "./row-types.js";
6
+ export type { QueryState } from "./queries.js";
7
+ export { createSession, type Issue, type Prepared, type Snapshot, type Session, } from "./session.js";
8
+ export type { DeepReadonly, Option } from "./data.js";
9
+ export { createForms, type Merged } from "./forms.js";
10
+ export { subtype } from "./subtypes.js";
11
+ export type { BoundDefineForm, BoundDefineRules, OptionKey, SubtypeDefinition, } from "./subtype-types.js";
12
+ export { endpoint, defineEndpoints, type Endpoint, type Registry, type BoundField, type Scope, type Loader, type LoadKey, } from "./endpoints.js";
13
+ export type { OptionList, OptionListSnapshot } from "./lists.js";
@@ -0,0 +1,7 @@
1
+ export { defineForm } from "./compile.js";
2
+ export { defineRules } from "./rule-sets.js";
3
+ export { field, declareContext, } from "./definition.js";
4
+ export { createSession, } from "./session.js";
5
+ export { createForms } from "./forms.js";
6
+ export { subtype } from "./subtypes.js";
7
+ export { endpoint, defineEndpoints, } from "./endpoints.js";
@@ -0,0 +1,30 @@
1
+ import type { Address, Field, Row } from "./address.js";
2
+ import type { Step } from "./cycles.js";
3
+ import { type Reads } from "./reads.js";
4
+ import type { FieldFormula, Observer } from "./rules.js";
5
+ export type Readers = {
6
+ readonly formulas: Map<Address, Set<FieldFormula>>;
7
+ readonly observers: Map<Address, Set<Observer>>;
8
+ readonly owned: WeakSet<object>;
9
+ };
10
+ export declare const noReaders: () => Readers;
11
+ export declare const copyReaders: (readers: Readers) => Readers;
12
+ export declare const track: <R>(index: Map<Address, Set<R>>, reader: R, before: Reads | undefined, after: Reads | undefined, owned?: WeakSet<object>) => void;
13
+ export declare const readersOf: <R extends object>(index: ReadonlyMap<Address, ReadonlySet<R>>, address: Address, holds: (row: Row) => boolean, visit: (reader: R) => void) => void;
14
+ export type Reach = {
15
+ readonly candidates: Set<Field>;
16
+ start(priority: readonly Step[]): Worklist;
17
+ widen(field: Field, at: number, settled: ReadonlyMap<Step, number>): number | undefined;
18
+ };
19
+ export type Placement = {
20
+ position(step: Step): number;
21
+ eventOf(field: Field): Step | undefined;
22
+ };
23
+ export declare const reachOf: (readers: Readers, roots: Iterable<Address>, placement: Placement, holds: (row: Row) => boolean) => Reach;
24
+ export type Worklist = {
25
+ push(step: Step, position?: number): void;
26
+ pop(): {
27
+ readonly step: Step;
28
+ readonly position: number;
29
+ } | undefined;
30
+ };
@@ -0,0 +1,161 @@
1
+ import { sameAddresses } from "./reads.js";
2
+ export const noReaders = () => ({
3
+ formulas: new Map(),
4
+ observers: new Map(),
5
+ owned: new WeakSet(),
6
+ });
7
+ export const copyReaders = (readers) => ({
8
+ formulas: new Map(readers.formulas),
9
+ observers: new Map(readers.observers),
10
+ owned: new WeakSet(),
11
+ });
12
+ // Replaces the reads of a reader in its index: it leaves the addresses it no
13
+ // longer reads and joins those it reads now. The same reads in the same order,
14
+ // the common case, change nothing. Without owned, every set is the index's own.
15
+ export const track = (index, reader, before, after, owned) => {
16
+ if (before !== undefined && after !== undefined && sameAddresses(before, after))
17
+ return;
18
+ const own = (address, readers) => {
19
+ if (owned === undefined || owned.has(readers))
20
+ return readers;
21
+ const copy = new Set(readers);
22
+ owned.add(copy);
23
+ index.set(address, copy);
24
+ return copy;
25
+ };
26
+ if (before !== undefined)
27
+ for (const address of before.keys()) {
28
+ const found = after?.has(address) === true ? undefined : index.get(address);
29
+ if (found === undefined)
30
+ continue;
31
+ if (found.size === 1 && found.has(reader))
32
+ index.delete(address);
33
+ else
34
+ own(address, found).delete(reader);
35
+ }
36
+ if (after !== undefined)
37
+ for (const address of after.keys()) {
38
+ if (before?.has(address) === true)
39
+ continue;
40
+ const found = index.get(address);
41
+ if (found === undefined) {
42
+ const readers = new Set([reader]);
43
+ owned?.add(readers);
44
+ index.set(address, readers);
45
+ }
46
+ else
47
+ own(address, found).add(reader);
48
+ }
49
+ };
50
+ // The readers of an address whose row is still held: those of a row the
51
+ // operation removes are still indexed until it commits, and leave with it. A
52
+ // reader of the header, or an output, has no row.
53
+ export const readersOf = (index, address, holds, visit) => {
54
+ for (const reader of index.get(address) ?? []) {
55
+ const { row } = reader;
56
+ if (row === undefined || holds(row))
57
+ visit(reader);
58
+ }
59
+ };
60
+ export const reachOf = (readers, roots, placement, holds) => {
61
+ const candidates = new Set();
62
+ let pending;
63
+ const join = (field) => {
64
+ pending.push(field);
65
+ const event = placement.eventOf(field);
66
+ if (event !== undefined)
67
+ pending.push(event);
68
+ };
69
+ spread(readers.formulas, roots, candidates, holds, () => { });
70
+ return {
71
+ candidates,
72
+ start: (priority) => {
73
+ pending = worklist(placement.position);
74
+ priority.forEach((event, index) => pending.push(event, index - priority.length));
75
+ for (const field of candidates)
76
+ join(field);
77
+ return pending;
78
+ },
79
+ widen: (field, at, settled) => {
80
+ if (candidates.has(field))
81
+ return;
82
+ let earliest;
83
+ spread(readers.formulas, [field], candidates, holds, (found) => {
84
+ join(found);
85
+ // A candidate may have read it after its own position.
86
+ const position = Math.min(settled.get(found) ?? Infinity, placement.position(found));
87
+ if (position <= at && (earliest === undefined || position < earliest))
88
+ earliest = position;
89
+ });
90
+ return earliest;
91
+ },
92
+ };
93
+ };
94
+ // Adds the roots that are fields, and the field of every formula that reads a
95
+ // candidate, transitively, each once; a structure is no field, but its readers
96
+ // are.
97
+ const spread = (formulas, roots, candidates, holds, found) => {
98
+ const queue = [];
99
+ const join = (address) => {
100
+ if (typeof address !== "string" && address.kind === "structure")
101
+ queue.push(address);
102
+ else if (!candidates.has(address)) {
103
+ candidates.add(address);
104
+ found(address);
105
+ queue.push(address);
106
+ }
107
+ };
108
+ for (const root of roots)
109
+ join(root);
110
+ for (let next = 0; next < queue.length; next++)
111
+ readersOf(formulas, queue[next], holds, (formula) => join(formula.address));
112
+ };
113
+ const worklist = (position) => {
114
+ const steps = [];
115
+ const keys = [];
116
+ const swap = (a, b) => {
117
+ const step = steps[a];
118
+ const key = keys[a];
119
+ steps[a] = steps[b];
120
+ keys[a] = keys[b];
121
+ steps[b] = step;
122
+ keys[b] = key;
123
+ };
124
+ return {
125
+ push: (step, at = position(step)) => {
126
+ let index = steps.length;
127
+ steps.push(step);
128
+ keys.push(at);
129
+ while (index > 0) {
130
+ const parent = (index - 1) >> 1;
131
+ if (keys[parent] <= keys[index])
132
+ break;
133
+ swap(index, parent);
134
+ index = parent;
135
+ }
136
+ },
137
+ pop: () => {
138
+ if (steps.length === 0)
139
+ return;
140
+ const first = { step: steps[0], position: keys[0] };
141
+ const last = steps.length - 1;
142
+ swap(0, last);
143
+ steps.pop();
144
+ keys.pop();
145
+ for (let index = 0;;) {
146
+ const left = 2 * index + 1;
147
+ const right = left + 1;
148
+ let least = index;
149
+ if (left < last && keys[left] < keys[least])
150
+ least = left;
151
+ if (right < last && keys[right] < keys[least])
152
+ least = right;
153
+ if (least === index)
154
+ break;
155
+ swap(index, least);
156
+ index = least;
157
+ }
158
+ return first;
159
+ },
160
+ };
161
+ };
@@ -0,0 +1,22 @@
1
+ import { type Field, type RowKey } from "./address.js";
2
+ import type { Compiled } from "./compiled.js";
3
+ import type { RowMap } from "./row-maps.js";
4
+ import type { FormState, ScopeState } from "./scope-states.js";
5
+ import type { States } from "./states.js";
6
+ import type { Store } from "./tree.js";
7
+ export type Issue = {
8
+ readonly message: string;
9
+ readonly field?: string | undefined;
10
+ readonly collection?: string | undefined;
11
+ readonly key?: RowKey | undefined;
12
+ };
13
+ type Located = {
14
+ readonly store: Store;
15
+ readonly form: FormState;
16
+ readonly fieldStates: Readonly<Record<string, unknown>>;
17
+ readonly rowStates: RowMap<States>;
18
+ readonly rowScopes: RowMap<ScopeState>;
19
+ };
20
+ export type Failed = ReadonlyMap<Field, string>;
21
+ export declare const issuesOf: (compiled: Compiled, published: Located, kind: "errors" | "warnings", failed: Failed) => readonly Issue[];
22
+ export {};
@@ -0,0 +1,60 @@
1
+ // The problems of a session, as prepare gives them: each message with where it
2
+ // is, in visual order. The form first, then the fields of the header in
3
+ // declaration order and, per collection in declaration order, its own
4
+ // messages and its rows in their position, each row with its own messages
5
+ // before those of its fields. The counts prune the scopes without messages,
6
+ // so a form without problems visits nothing.
7
+ import { rowAddress } from "./address.js";
8
+ import { fieldNames } from "./derived-scope.js";
9
+ const none = Object.freeze([]);
10
+ const counts = { errors: "errorCount", warnings: "warningCount" };
11
+ export const issuesOf = (compiled, published, kind, failed) => {
12
+ const count = counts[kind];
13
+ const { form } = published;
14
+ if (form[count] === 0 && failed.size === 0)
15
+ return none;
16
+ const issues = [];
17
+ const push = (issue) => issues.push(Object.freeze(issue));
18
+ const header = published.fieldStates;
19
+ for (const message of form[kind])
20
+ push({ message });
21
+ for (const field of fieldNames(compiled.fields))
22
+ for (const message of messagesOf(header, field, kind, failed.get(field)))
23
+ push({ field, message });
24
+ const failing = failingRows(failed);
25
+ for (const [collection, compiledCollection] of compiled.collections) {
26
+ const scope = form.collections[collection];
27
+ if (scope[count] === 0 && !failing.has(collection))
28
+ continue;
29
+ for (const message of scope[kind])
30
+ push({ collection, message });
31
+ const fields = fieldNames(compiledCollection.fields);
32
+ const rows = published.store.collections.get(collection);
33
+ for (const key of rows?.order ?? []) {
34
+ const row = rows?.rows.get(key);
35
+ const scoped = published.rowScopes.get(key);
36
+ if (scoped[count] === 0 && !failing.has(row))
37
+ continue;
38
+ for (const message of scoped[kind])
39
+ push({ collection, key, message });
40
+ const states = published.rowStates.get(key);
41
+ for (const field of fields)
42
+ for (const message of messagesOf(states, field, kind, failed.get(rowAddress(row, field))))
43
+ push({ collection, key, field, message });
44
+ }
45
+ }
46
+ return Object.freeze(issues);
47
+ };
48
+ // The messages of a field, with the one of its failed query after them, once.
49
+ const messagesOf = (states, name, kind, failure) => {
50
+ const messages = states[name][kind];
51
+ return failure === undefined || messages.includes(failure) ? messages : [...messages, failure];
52
+ };
53
+ // The rows with a failed query, and their collections.
54
+ const failingRows = (failed) => {
55
+ const found = new Set();
56
+ for (const address of failed.keys())
57
+ if (typeof address !== "string")
58
+ found.add(address.row).add(address.row.collection.name);
59
+ return found;
60
+ };
@@ -0,0 +1,4 @@
1
+ import type { Row } from "./address.js";
2
+ export type KnownKeys = Map<string, number[]>;
3
+ export declare const remember: (known: KnownKeys, rows: Iterable<Row>) => void;
4
+ export declare const knows: (known: KnownKeys, collection: string, key: unknown) => boolean;
@@ -0,0 +1,22 @@
1
+ export const remember = (known, rows) => {
2
+ for (const { collection, seq } of rows) {
3
+ let ranges = known.get(collection.name);
4
+ if (ranges === undefined)
5
+ known.set(collection.name, (ranges = []));
6
+ if (ranges.at(-1) === seq - 1)
7
+ ranges[ranges.length - 1] = seq;
8
+ else
9
+ ranges.push(seq, seq);
10
+ }
11
+ };
12
+ // Whether the session gave the key to a row of the collection.
13
+ export const knows = (known, collection, key) => {
14
+ if (typeof key !== "string" || !/^r[1-9]\d*$/u.test(key))
15
+ return false;
16
+ const seq = Number(key.slice(1));
17
+ const ranges = known.get(collection) ?? [];
18
+ for (let at = 0; at < ranges.length; at += 2)
19
+ if (seq >= ranges[at] && seq <= ranges[at + 1])
20
+ return true;
21
+ return false;
22
+ };
@@ -0,0 +1,27 @@
1
+ import type { DeepReadonly, OptionShape } from "./data.js";
2
+ import { type Loader, type ResolvedEndpoint, type Scope } from "./endpoints.js";
3
+ export type OptionListSnapshot<O> = {
4
+ readonly status: "idle" | "suspended" | "loading" | "ready" | "error";
5
+ readonly text: string;
6
+ readonly options: readonly DeepReadonly<O>[];
7
+ readonly complete: boolean;
8
+ readonly error: Error | undefined;
9
+ };
10
+ export type OptionList<O> = {
11
+ getSnapshot(): OptionListSnapshot<O>;
12
+ subscribe(listener: () => void): () => void;
13
+ open(): void;
14
+ search(text: string): void;
15
+ more(): void;
16
+ retry(): void;
17
+ };
18
+ export type ListSource = {
19
+ readonly label: string;
20
+ readonly endpoint: ResolvedEndpoint;
21
+ readonly scope: Scope;
22
+ readonly loader: Loader;
23
+ params(): object | undefined;
24
+ watch(listener: () => void): () => void;
25
+ notify(listener: () => void): void;
26
+ };
27
+ export declare const createOptionList: (source: ListSource) => OptionList<OptionShape>;
@@ -0,0 +1,174 @@
1
+ import { requestOf, send, } from "./endpoints.js";
2
+ import { wrapError } from "./fields.js";
3
+ // The list belongs to its view and never writes to the session. Its actions
4
+ // change the view at any time, but only a view with subscribers requests
5
+ // pages and takes responses: it requests when its first subscriber comes, and
6
+ // its last one leaving drops the request in flight.
7
+ export const createOptionList = (source) => {
8
+ const params = source.params();
9
+ const view = {
10
+ source,
11
+ listeners: new Set(),
12
+ params,
13
+ snapshot: Object.freeze({
14
+ status: params === undefined ? "suspended" : "idle",
15
+ text: "",
16
+ options: none,
17
+ complete: false,
18
+ error: undefined,
19
+ }),
20
+ opened: false,
21
+ taken: 0,
22
+ whole: undefined,
23
+ attempt: undefined,
24
+ stopWatching: undefined,
25
+ };
26
+ return {
27
+ getSnapshot: () => view.snapshot,
28
+ subscribe: (listener) => subscribe(view, listener),
29
+ open: () => {
30
+ view.opened = true;
31
+ if (view.snapshot.status !== "suspended")
32
+ restart(view);
33
+ },
34
+ search: (text) => search(view, text),
35
+ more: () => {
36
+ if (view.snapshot.status !== "ready" || view.snapshot.complete)
37
+ return;
38
+ publish(view, { status: "loading" });
39
+ request(view);
40
+ },
41
+ retry: () => {
42
+ if (view.snapshot.status !== "error")
43
+ return;
44
+ publish(view, { status: "loading", error: undefined });
45
+ request(view);
46
+ },
47
+ };
48
+ };
49
+ const none = Object.freeze([]);
50
+ const subscribe = (view, listener) => {
51
+ view.listeners.add(listener);
52
+ if (view.listeners.size === 1) {
53
+ view.stopWatching = view.source.watch(() => sync(view));
54
+ sync(view);
55
+ request(view);
56
+ }
57
+ return () => {
58
+ if (!view.listeners.delete(listener) || view.listeners.size > 0)
59
+ return;
60
+ view.stopWatching?.();
61
+ view.stopWatching = undefined;
62
+ drop(view);
63
+ };
64
+ };
65
+ const search = (view, text) => {
66
+ if (typeof text !== "string")
67
+ throw new TypeError(`${view.source.label} list: search text must be a string`);
68
+ view.opened = true;
69
+ const { status } = view.snapshot;
70
+ if (status === "suspended")
71
+ publish(view, { text });
72
+ else if (view.source.endpoint.kind === "options")
73
+ restart(view, { text });
74
+ // A whole list is searched locally, once it is loaded.
75
+ else if (view.whole !== undefined)
76
+ publish(view, { text, options: filter(view.whole, text) });
77
+ else if (status === "loading")
78
+ publish(view, { text });
79
+ else
80
+ restart(view, { text });
81
+ };
82
+ const publish = (view, change) => {
83
+ const next = { ...view.snapshot, ...change };
84
+ const keys = Object.keys(next);
85
+ if (keys.every((key) => next[key] === view.snapshot[key]))
86
+ return;
87
+ view.snapshot = Object.freeze(next);
88
+ for (const listener of Array.from(view.listeners))
89
+ view.source.notify(listener);
90
+ };
91
+ const drop = (view) => {
92
+ view.attempt?.controller.abort();
93
+ view.attempt = undefined;
94
+ };
95
+ // Drops the pages and the request in flight.
96
+ const reset = (view, change) => {
97
+ drop(view);
98
+ view.taken = 0;
99
+ view.whole = undefined;
100
+ publish(view, { options: none, complete: false, error: undefined, ...change });
101
+ };
102
+ // Starts again from page 1.
103
+ const restart = (view, change = {}) => {
104
+ reset(view, { status: "loading", ...change });
105
+ request(view);
106
+ };
107
+ const request = (view) => {
108
+ const { source, snapshot, params } = view;
109
+ if (view.listeners.size === 0 || snapshot.status !== "loading" || view.attempt !== undefined)
110
+ return;
111
+ if (params === undefined)
112
+ return;
113
+ const attempt = { controller: new AbortController() };
114
+ view.attempt = attempt;
115
+ const asked = requestOf(source.endpoint, source.scope, params, snapshot.text, view.taken + 1);
116
+ send(source.loader, asked, attempt.controller.signal).then((result) => {
117
+ if (view.attempt === attempt)
118
+ take(view, result);
119
+ }, (error) => {
120
+ if (view.attempt !== attempt)
121
+ return;
122
+ view.attempt = undefined;
123
+ const failure = wrapError(`${source.label} list: request failed`, error);
124
+ publish(view, { status: "error", error: failure });
125
+ });
126
+ };
127
+ const take = (view, result) => {
128
+ view.attempt = undefined;
129
+ if (view.source.endpoint.kind === "list") {
130
+ view.whole = result;
131
+ publish(view, {
132
+ status: "ready",
133
+ options: filter(view.whole, view.snapshot.text),
134
+ complete: true,
135
+ });
136
+ return;
137
+ }
138
+ const page = result;
139
+ const seen = new Set(view.snapshot.options.map((option) => option.value));
140
+ const fresh = page.options.filter((option) => !seen.has(option.value));
141
+ view.taken += 1;
142
+ const options = Object.freeze([...view.snapshot.options, ...fresh]);
143
+ publish(view, { status: "ready", options, complete: !page.more });
144
+ };
145
+ // Params equal as data keep their object, so a new object is a change.
146
+ const sync = (view) => {
147
+ const next = view.source.params();
148
+ if (next === view.params)
149
+ return;
150
+ view.params = next;
151
+ if (next !== undefined && view.opened)
152
+ restart(view);
153
+ else
154
+ reset(view, { status: next === undefined ? "suspended" : "idle" });
155
+ };
156
+ // A whole list shows the options whose label contains the text, ignoring case
157
+ // and accents. Options are frozen copies that views and caches share, so each
158
+ // label is folded once.
159
+ const folded = new WeakMap();
160
+ const fold = (text) => text.normalize("NFD").replaceAll(/\p{M}/gu, "").toLowerCase();
161
+ const foldedLabel = (option) => {
162
+ let label = folded.get(option);
163
+ if (label === undefined) {
164
+ label = fold(option.label);
165
+ folded.set(option, label);
166
+ }
167
+ return label;
168
+ };
169
+ const filter = (options, text) => {
170
+ if (text === "")
171
+ return options;
172
+ const wanted = fold(text);
173
+ return Object.freeze(options.filter((option) => foldedLabel(option).includes(wanted)));
174
+ };
@@ -0,0 +1,4 @@
1
+ import type { Fields } from "./definition.js";
2
+ import type { CompiledCollection } from "./collections.js";
3
+ import type { StateFormula } from "./rules.js";
4
+ export declare const lock: (fields: Fields, collections: ReadonlyMap<string, CompiledCollection>, declared: unknown) => StateFormula[];