trama-forms 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/core/acceptance.d.ts +15 -0
- package/dist/core/acceptance.js +37 -0
- package/dist/core/address.d.ts +45 -0
- package/dist/core/address.js +68 -0
- package/dist/core/authoring-checks.d.ts +39 -0
- package/dist/core/authoring-checks.js +1 -0
- package/dist/core/authoring-form.d.ts +23 -0
- package/dist/core/authoring-form.js +1 -0
- package/dist/core/authoring-params.d.ts +37 -0
- package/dist/core/authoring-params.js +1 -0
- package/dist/core/authoring-relations.d.ts +46 -0
- package/dist/core/authoring-relations.js +1 -0
- package/dist/core/authoring-rows.d.ts +109 -0
- package/dist/core/authoring-rows.js +1 -0
- package/dist/core/authoring-values.d.ts +36 -0
- package/dist/core/authoring-values.js +1 -0
- package/dist/core/authoring.d.ts +117 -0
- package/dist/core/authoring.js +1 -0
- package/dist/core/baseline.d.ts +12 -0
- package/dist/core/baseline.js +66 -0
- package/dist/core/candidates.d.ts +26 -0
- package/dist/core/candidates.js +155 -0
- package/dist/core/collections.d.ts +59 -0
- package/dist/core/collections.js +138 -0
- package/dist/core/commands.d.ts +15 -0
- package/dist/core/commands.js +64 -0
- package/dist/core/compile.d.ts +18 -0
- package/dist/core/compile.js +60 -0
- package/dist/core/compiled.d.ts +34 -0
- package/dist/core/compiled.js +12 -0
- package/dist/core/config.d.ts +103 -0
- package/dist/core/config.js +1 -0
- package/dist/core/cycles.d.ts +14 -0
- package/dist/core/cycles.js +39 -0
- package/dist/core/data.d.ts +40 -0
- package/dist/core/data.js +236 -0
- package/dist/core/declarations.d.ts +61 -0
- package/dist/core/declarations.js +202 -0
- package/dist/core/definition.d.ts +54 -0
- package/dist/core/definition.js +3 -0
- package/dist/core/derived-fields.d.ts +37 -0
- package/dist/core/derived-fields.js +121 -0
- package/dist/core/derived-scope.d.ts +55 -0
- package/dist/core/derived-scope.js +105 -0
- package/dist/core/derived.d.ts +76 -0
- package/dist/core/derived.js +180 -0
- package/dist/core/endpoints.d.ts +94 -0
- package/dist/core/endpoints.js +95 -0
- package/dist/core/engine-types.d.ts +43 -0
- package/dist/core/engine-types.js +1 -0
- package/dist/core/engine.d.ts +9 -0
- package/dist/core/engine.js +140 -0
- package/dist/core/evaluation.d.ts +20 -0
- package/dist/core/evaluation.js +62 -0
- package/dist/core/external.d.ts +13 -0
- package/dist/core/external.js +49 -0
- package/dist/core/field-definitions.d.ts +103 -0
- package/dist/core/field-definitions.js +20 -0
- package/dist/core/field-traits.d.ts +29 -0
- package/dist/core/field-traits.js +149 -0
- package/dist/core/fields.d.ts +26 -0
- package/dist/core/fields.js +89 -0
- package/dist/core/forms.d.ts +23 -0
- package/dist/core/forms.js +46 -0
- package/dist/core/handles.d.ts +56 -0
- package/dist/core/handles.js +128 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +7 -0
- package/dist/core/invalidation.d.ts +30 -0
- package/dist/core/invalidation.js +161 -0
- package/dist/core/issues.d.ts +22 -0
- package/dist/core/issues.js +60 -0
- package/dist/core/known-keys.d.ts +4 -0
- package/dist/core/known-keys.js +22 -0
- package/dist/core/lists.d.ts +27 -0
- package/dist/core/lists.js +174 -0
- package/dist/core/lock.d.ts +4 -0
- package/dist/core/lock.js +70 -0
- package/dist/core/marking.d.ts +16 -0
- package/dist/core/marking.js +28 -0
- package/dist/core/marks.d.ts +32 -0
- package/dist/core/marks.js +203 -0
- package/dist/core/memory.d.ts +79 -0
- package/dist/core/memory.js +127 -0
- package/dist/core/messages.d.ts +20 -0
- package/dist/core/messages.js +46 -0
- package/dist/core/multiple.d.ts +12 -0
- package/dist/core/multiple.js +123 -0
- package/dist/core/notify.d.ts +32 -0
- package/dist/core/notify.js +160 -0
- package/dist/core/observation.d.ts +7 -0
- package/dist/core/observation.js +80 -0
- package/dist/core/observe.d.ts +19 -0
- package/dist/core/observe.js +59 -0
- package/dist/core/operation.d.ts +8 -0
- package/dist/core/operation.js +48 -0
- package/dist/core/options.d.ts +40 -0
- package/dist/core/options.js +32 -0
- package/dist/core/outputs.d.ts +58 -0
- package/dist/core/outputs.js +149 -0
- package/dist/core/param-memory.d.ts +13 -0
- package/dist/core/param-memory.js +17 -0
- package/dist/core/params.d.ts +37 -0
- package/dist/core/params.js +70 -0
- package/dist/core/pass.d.ts +6 -0
- package/dist/core/pass.js +141 -0
- package/dist/core/patches.d.ts +15 -0
- package/dist/core/patches.js +106 -0
- package/dist/core/prepare.d.ts +19 -0
- package/dist/core/prepare.js +105 -0
- package/dist/core/prepared.d.ts +10 -0
- package/dist/core/prepared.js +14 -0
- package/dist/core/program.d.ts +43 -0
- package/dist/core/program.js +100 -0
- package/dist/core/publication.d.ts +20 -0
- package/dist/core/publication.js +56 -0
- package/dist/core/published.d.ts +18 -0
- package/dist/core/published.js +83 -0
- package/dist/core/queries.d.ts +10 -0
- package/dist/core/queries.js +148 -0
- package/dist/core/query-batch.d.ts +2 -0
- package/dist/core/query-batch.js +67 -0
- package/dist/core/query-entries.d.ts +78 -0
- package/dist/core/query-entries.js +139 -0
- package/dist/core/query-response.d.ts +2 -0
- package/dist/core/query-response.js +110 -0
- package/dist/core/reads.d.ts +30 -0
- package/dist/core/reads.js +88 -0
- package/dist/core/rebase.d.ts +16 -0
- package/dist/core/rebase.js +40 -0
- package/dist/core/relations.d.ts +10 -0
- package/dist/core/relations.js +84 -0
- package/dist/core/remote-fields.d.ts +6 -0
- package/dist/core/remote-fields.js +29 -0
- package/dist/core/resolution.d.ts +12 -0
- package/dist/core/resolution.js +157 -0
- package/dist/core/restarts.d.ts +17 -0
- package/dist/core/restarts.js +42 -0
- package/dist/core/revisions.d.ts +11 -0
- package/dist/core/revisions.js +57 -0
- package/dist/core/row-arrays.d.ts +12 -0
- package/dist/core/row-arrays.js +47 -0
- package/dist/core/row-maps.d.ts +16 -0
- package/dist/core/row-maps.js +52 -0
- package/dist/core/row-states.d.ts +68 -0
- package/dist/core/row-states.js +75 -0
- package/dist/core/row-types.d.ts +76 -0
- package/dist/core/row-types.js +1 -0
- package/dist/core/rule-set-config.d.ts +56 -0
- package/dist/core/rule-set-config.js +1 -0
- package/dist/core/rule-set-types.d.ts +91 -0
- package/dist/core/rule-set-types.js +1 -0
- package/dist/core/rule-sets.d.ts +15 -0
- package/dist/core/rule-sets.js +107 -0
- package/dist/core/rules.d.ts +67 -0
- package/dist/core/rules.js +144 -0
- package/dist/core/saving.d.ts +55 -0
- package/dist/core/saving.js +132 -0
- package/dist/core/scope-declarations.d.ts +13 -0
- package/dist/core/scope-declarations.js +18 -0
- package/dist/core/scope-messages.d.ts +9 -0
- package/dist/core/scope-messages.js +46 -0
- package/dist/core/scope-states.d.ts +50 -0
- package/dist/core/scope-states.js +222 -0
- package/dist/core/session-external.d.ts +24 -0
- package/dist/core/session-external.js +138 -0
- package/dist/core/session-lists.d.ts +12 -0
- package/dist/core/session-lists.js +60 -0
- package/dist/core/session-marks.d.ts +22 -0
- package/dist/core/session-marks.js +113 -0
- package/dist/core/session-parts.d.ts +26 -0
- package/dist/core/session-parts.js +91 -0
- package/dist/core/session-rows.d.ts +15 -0
- package/dist/core/session-rows.js +62 -0
- package/dist/core/session-types.d.ts +112 -0
- package/dist/core/session-types.js +1 -0
- package/dist/core/session.d.ts +5 -0
- package/dist/core/session.js +51 -0
- package/dist/core/state-evaluation.d.ts +10 -0
- package/dist/core/state-evaluation.js +69 -0
- package/dist/core/state-properties.d.ts +30 -0
- package/dist/core/state-properties.js +101 -0
- package/dist/core/states.d.ts +73 -0
- package/dist/core/states.js +180 -0
- package/dist/core/steps.d.ts +37 -0
- package/dist/core/steps.js +183 -0
- package/dist/core/subtype-types.d.ts +67 -0
- package/dist/core/subtype-types.js +1 -0
- package/dist/core/subtypes.d.ts +19 -0
- package/dist/core/subtypes.js +69 -0
- package/dist/core/tree.d.ts +44 -0
- package/dist/core/tree.js +119 -0
- package/dist/core/validation.d.ts +11 -0
- package/dist/core/validation.js +42 -0
- package/dist/core/views.d.ts +24 -0
- package/dist/core/views.js +161 -0
- package/dist/react/async.d.ts +7 -0
- package/dist/react/async.js +67 -0
- package/dist/react/binding.d.ts +15 -0
- package/dist/react/binding.js +51 -0
- package/dist/react/coverage.d.ts +21 -0
- package/dist/react/coverage.js +91 -0
- package/dist/react/field.d.ts +60 -0
- package/dist/react/field.js +194 -0
- package/dist/react/form-state.d.ts +14 -0
- package/dist/react/form-state.js +70 -0
- package/dist/react/index.d.ts +152 -0
- package/dist/react/index.js +6 -0
- package/dist/react/opening.d.ts +38 -0
- package/dist/react/opening.js +206 -0
- package/dist/react/outputs.d.ts +11 -0
- package/dist/react/outputs.js +137 -0
- package/dist/react/props.d.ts +48 -0
- package/dist/react/props.js +1 -0
- package/dist/react/query.d.ts +11 -0
- package/dist/react/query.js +21 -0
- package/dist/react/render.d.ts +49 -0
- package/dist/react/render.js +119 -0
- package/dist/react/rows.d.ts +2 -0
- package/dist/react/rows.js +25 -0
- package/dist/react/visible-messages.d.ts +8 -0
- package/dist/react/visible-messages.js +16 -0
- package/package.json +77 -4
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { HeaderRelation, RelationMessages } from "./authoring-relations.js";
|
|
2
|
+
import type { FieldValue, ParamsOf, RemoteDefinition, WithPlaceholder } from "./fields.js";
|
|
3
|
+
import type { ContextTools, Fields, FieldsIn } from "./definition.js";
|
|
4
|
+
import type { RowKey } from "./address.js";
|
|
5
|
+
import type { EditConflict, At, Checked, EmptyMessage, EmptyReason, EventMessages, Everywhere, Items, Keys, Lacks, Limit, LimitMessages, Member, Reported, Result, StateMessages, Verdict } from "./authoring-checks.js";
|
|
6
|
+
import type { CheckCollection, CollectionRules, RequiredEach, Row } from "./authoring-rows.js";
|
|
7
|
+
import type { Given } from "./rule-set-types.js";
|
|
8
|
+
import type { RemainingLayers } from "./authoring-params.js";
|
|
9
|
+
import type { QueryMessages, QueryRule } from "./authoring-values.js";
|
|
10
|
+
type Flat<T> = {
|
|
11
|
+
[P in keyof T]: T[P];
|
|
12
|
+
};
|
|
13
|
+
export type Values<F extends Fields, K = {}> = Flat<{
|
|
14
|
+
readonly [X in keyof F]: FieldValue<F[X]>;
|
|
15
|
+
} & {
|
|
16
|
+
readonly [N in keyof K]: readonly Row<FieldsIn<K[N]>>[];
|
|
17
|
+
}>;
|
|
18
|
+
type Formula<F extends Fields, K, C, T> = (values: Values<F, K>, tools: {
|
|
19
|
+
readonly peek: Values<F, K>;
|
|
20
|
+
} & ContextTools<C>) => T;
|
|
21
|
+
export type Many<T> = T | readonly T[];
|
|
22
|
+
export type Reason = (string & {}) | "";
|
|
23
|
+
type Flag<F extends Fields, K, C> = Formula<F, K, C, boolean | Reason | undefined>;
|
|
24
|
+
type Toggle<F extends Fields, K, C> = Formula<F, K, C, boolean | undefined>;
|
|
25
|
+
type Message<F extends Fields, K, C> = Formula<F, K, C, Reason | false | undefined>;
|
|
26
|
+
type Text<F extends Fields, K, C> = Formula<F, K, C, string | undefined>;
|
|
27
|
+
export type Bound = number | string | null | undefined;
|
|
28
|
+
type StateProperties<D> = {
|
|
29
|
+
readonly disabled?: boolean | Reason | undefined;
|
|
30
|
+
readonly readOnly?: boolean | Reason | undefined;
|
|
31
|
+
readonly hidden?: boolean | undefined;
|
|
32
|
+
readonly required?: boolean | Reason | undefined;
|
|
33
|
+
readonly label?: string | undefined;
|
|
34
|
+
readonly decimals?: number | undefined;
|
|
35
|
+
readonly min?: Bound;
|
|
36
|
+
readonly max?: Bound;
|
|
37
|
+
readonly error?: Reason | false | undefined;
|
|
38
|
+
readonly warning?: Reason | false | undefined;
|
|
39
|
+
} & (D extends WithPlaceholder ? {
|
|
40
|
+
readonly placeholder?: string | undefined;
|
|
41
|
+
} : {});
|
|
42
|
+
export type StatePatch<F extends Fields> = {
|
|
43
|
+
readonly [X in keyof F]?: StateProperties<F[X]> | undefined;
|
|
44
|
+
};
|
|
45
|
+
export type Patch<F extends Fields> = {
|
|
46
|
+
readonly [X in keyof F]?: FieldValue<F[X]> | undefined;
|
|
47
|
+
};
|
|
48
|
+
type FormPatch<F extends Fields, K> = Patch<F> & {
|
|
49
|
+
readonly [N in keyof K]?: {
|
|
50
|
+
readonly [key: RowKey]: Patch<FieldsIn<K[N]>> | undefined;
|
|
51
|
+
} | undefined;
|
|
52
|
+
};
|
|
53
|
+
export type ToDefaults<F extends Fields> = <const N extends keyof F & string>(...names: N[]) => {
|
|
54
|
+
readonly [P in N]: FieldValue<F[P]>;
|
|
55
|
+
};
|
|
56
|
+
type Rows<K> = {
|
|
57
|
+
readonly [N in keyof K]: readonly RowKey<N & string>[];
|
|
58
|
+
};
|
|
59
|
+
type Event<F extends Fields, K, C> = (values: Values<F, K>, tools: {
|
|
60
|
+
readonly toDefaults: ToDefaults<F>;
|
|
61
|
+
readonly rows: Rows<K>;
|
|
62
|
+
readonly before: Values<F, K>;
|
|
63
|
+
} & ContextTools<C>) => FormPatch<F, K> | undefined;
|
|
64
|
+
type NeedsParams<D> = D extends RemoteDefinition ? ({} extends ParamsOf<D> ? never : true) : never;
|
|
65
|
+
type Entry<F extends Fields, K, C, X extends keyof F, M> = {
|
|
66
|
+
readonly value?: Formula<F, K, C, FieldValue<F[X]> | undefined> | undefined;
|
|
67
|
+
readonly disabled?: Many<Flag<F, K, C>> | undefined;
|
|
68
|
+
readonly readOnly?: Many<Flag<F, K, C>> | undefined;
|
|
69
|
+
readonly hidden?: Many<Toggle<F, K, C>> | undefined;
|
|
70
|
+
readonly required?: Many<Flag<F, K, C>> | undefined;
|
|
71
|
+
readonly error?: Many<Message<F, K, C>> | undefined;
|
|
72
|
+
readonly warning?: Many<Message<F, K, C>> | undefined;
|
|
73
|
+
readonly label?: Text<F, K, C> | undefined;
|
|
74
|
+
readonly decimals?: Formula<F, K, C, number | undefined> | undefined;
|
|
75
|
+
readonly min?: Formula<F, K, C, Bound> | undefined;
|
|
76
|
+
readonly max?: Formula<F, K, C, Bound> | undefined;
|
|
77
|
+
readonly states?: Formula<F, K, C, StatePatch<F> | undefined> | undefined;
|
|
78
|
+
readonly onChange?: Event<F, K, C> | undefined;
|
|
79
|
+
readonly relation?: HeaderRelation<F, K, C, X> | undefined;
|
|
80
|
+
readonly onEdit?: Event<F, K, C> | undefined;
|
|
81
|
+
readonly query?: QueryRule<F, K, C, M> | undefined;
|
|
82
|
+
} & (F[X] extends RemoteDefinition ? {
|
|
83
|
+
readonly placeholder?: Text<F, K, C> | undefined;
|
|
84
|
+
readonly params?: Formula<F, K, C, ParamsOf<F[X]> | undefined> | undefined;
|
|
85
|
+
} : F[X] extends WithPlaceholder ? {
|
|
86
|
+
readonly placeholder?: Text<F, K, C> | undefined;
|
|
87
|
+
} : {});
|
|
88
|
+
export type RequiredEntries<F extends Fields> = {
|
|
89
|
+
[X in keyof F]: F[X] extends RemoteDefinition ? [NeedsParams<F[X]>] extends [never] ? never : X : never;
|
|
90
|
+
}[keyof F];
|
|
91
|
+
export type RequiredRows<R> = R extends Fields ? RequiredEntries<R> : never;
|
|
92
|
+
export type RequiredCollections<K> = K extends unknown ? {
|
|
93
|
+
[N in keyof K]-?: [RequiredRows<FieldsIn<K[N]>>] extends [never] ? never : N;
|
|
94
|
+
}[keyof K] : never;
|
|
95
|
+
type FieldRules<F extends Fields, K, C, M, I> = {
|
|
96
|
+
readonly [X in keyof F]?: Entry<F, K, C, X, M> | undefined;
|
|
97
|
+
} & {
|
|
98
|
+
readonly [N in keyof K as string extends N ? never : N]?: CollectionRules<F, K, C, N, M> | undefined;
|
|
99
|
+
} & ([I] extends [undefined] ? RequiredLayers<F, K, C, M> : I extends "set" ? unknown : RemainingLayers<F, K, C, M, Given<I>>);
|
|
100
|
+
type RequiredLayers<F extends Fields, K, C, M> = ([RequiredEntries<F>] extends [never] ? unknown : {
|
|
101
|
+
readonly [X in RequiredEntries<F>]: {
|
|
102
|
+
readonly params: Formula<F, K, C, ParamsOf<F[X]> | undefined>;
|
|
103
|
+
};
|
|
104
|
+
}) & ([RequiredCollections<K>] extends [never] ? unknown : {
|
|
105
|
+
readonly [N in RequiredCollections<K>]: RequiredEach<F, K, C, N & keyof K, M>;
|
|
106
|
+
});
|
|
107
|
+
type CheckProperty<F extends Fields, K, X extends keyof F, Q, V, M, E, P> = Q extends "value" | "hidden" | "label" ? unknown : Q extends "disabled" | "readOnly" ? Reported<V, EmptyReason<Result<Items<V>>>> : Q extends "required" | "error" | "warning" ? Reported<V, EmptyMessage<Q, Result<Items<V>>>> : Q extends "placeholder" ? Verdict<Lacks<F, X, WithPlaceholder, "placeholder">> : Q extends Limit ? Verdict<LimitMessages<F, X, Q, Result<V>>> : Q extends "params" ? Verdict<Lacks<F, X, RemoteDefinition, "params">> : Q extends "states" ? Verdict<StateMessages<F, V>> : Q extends "onChange" ? Verdict<EventMessages<F, V, K>> : Q extends "onEdit" ? Verdict<EventMessages<F, V, K> | EditConflict<E, `field "${X & string}"`>> : Q extends "query" ? Verdict<QueryMessages<F, X, V, M>> : Q extends "relation" ? Verdict<RelationMessages<F, X, P>> : Everywhere<V, `rule property "${Q & string}" does not exist`>;
|
|
108
|
+
type CheckRules<F extends Fields, K, P, M> = {
|
|
109
|
+
[X in Keys<P>]?: X extends keyof F ? [Extract<Member<P, X>, (...args: never) => unknown>] extends [never] ? {
|
|
110
|
+
[Q in Keys<Member<P, X>>]?: CheckProperty<F, K, X, Q, At<Member<P, X>, Q>, M, Member<P, X>, P>;
|
|
111
|
+
} | undefined : `rules of field "${X & string}" must be an object of properties` : X extends keyof K ? CheckCollection<K, X, Member<P, X>, M> | undefined : `field "${X & string}" does not exist`;
|
|
112
|
+
};
|
|
113
|
+
export type PropertyRules<F extends Fields, K, C, P, M, I = undefined> = FieldRules<F, K, C, M, I> & CheckRules<F, K, Checked<P>, M> & {
|
|
114
|
+
readonly [Symbol.hasInstance]?: "rules must be an object keyed by field";
|
|
115
|
+
};
|
|
116
|
+
export type { Formula };
|
|
117
|
+
export type { DefaultsRules } from "./authoring-values.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Compiled } from "./compiled.js";
|
|
2
|
+
import type { Context, Given } from "./reads.js";
|
|
3
|
+
export type { Context, Given } from "./reads.js";
|
|
4
|
+
import { type NewRow } from "./commands.js";
|
|
5
|
+
type Values = Record<string, unknown>;
|
|
6
|
+
export declare const defaultsOf: (compiled: Compiled, context: Context) => Readonly<Values>;
|
|
7
|
+
export declare const createBaseline: (compiled: Compiled, defaults: Readonly<Values>, initial: unknown) => {
|
|
8
|
+
values: Values;
|
|
9
|
+
rows: Map<string, readonly NewRow[]>;
|
|
10
|
+
supplied: Set<string>;
|
|
11
|
+
};
|
|
12
|
+
export declare const givenOf: (compiled: Compiled, context: Context) => Given;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// The starting values of a session: the default value of each field, which
|
|
2
|
+
// the defaults of its form may give from the context, and initial over them.
|
|
3
|
+
import { accepts, adopt, fieldAt, isPlainRecord, isRecord, validateValue, wrapError, } from "./fields.js";
|
|
4
|
+
import { createDeriver } from "./derived-fields.js";
|
|
5
|
+
import { describe, ownEntries } from "./data.js";
|
|
6
|
+
import { incompatibleValue } from "./patches.js";
|
|
7
|
+
import { newRows } from "./commands.js";
|
|
8
|
+
// The default value of each field in a session: the value the defaults of the
|
|
9
|
+
// form give from its context, or the default of the field, which every
|
|
10
|
+
// session shares. An option is a frozen copy, apart from the object the
|
|
11
|
+
// defaults returned.
|
|
12
|
+
export const defaultsOf = (compiled, context) => {
|
|
13
|
+
if (compiled.defaults === undefined)
|
|
14
|
+
return compiled.fieldDefaults;
|
|
15
|
+
const values = { ...compiled.fieldDefaults };
|
|
16
|
+
let result;
|
|
17
|
+
let found = [];
|
|
18
|
+
try {
|
|
19
|
+
result = compiled.defaults(context);
|
|
20
|
+
if (isPlainRecord(result))
|
|
21
|
+
found = ownEntries(result);
|
|
22
|
+
}
|
|
23
|
+
catch (cause) {
|
|
24
|
+
throw wrapError("Form defaults: formula failed", cause);
|
|
25
|
+
}
|
|
26
|
+
if (!isPlainRecord(result))
|
|
27
|
+
throw new Error(`Form defaults: expected an object of values, got ${describe(result)}`);
|
|
28
|
+
for (const [key, value] of found) {
|
|
29
|
+
const item = fieldAt(compiled.fields, key);
|
|
30
|
+
if (item === undefined)
|
|
31
|
+
throw new Error(`Form defaults: unknown field ${describe(key)}`);
|
|
32
|
+
if (!accepts(item, value))
|
|
33
|
+
throw incompatibleValue("Form defaults", item, key, value);
|
|
34
|
+
values[key] = adopt(item, value, []);
|
|
35
|
+
}
|
|
36
|
+
return values;
|
|
37
|
+
};
|
|
38
|
+
// The baseline takes the default values of the session, and initial over them;
|
|
39
|
+
// the rows of each collection come from initial, none if it gives none.
|
|
40
|
+
export const createBaseline = (compiled, defaults, initial) => {
|
|
41
|
+
const values = { ...defaults };
|
|
42
|
+
const rows = new Map();
|
|
43
|
+
if (initial !== undefined && !isRecord(initial))
|
|
44
|
+
throw new Error("Initial values must be an object");
|
|
45
|
+
const supplied = Object.entries(initial ?? {}).filter(([name]) => !compiled.collections.has(name));
|
|
46
|
+
for (const [name, value] of supplied)
|
|
47
|
+
validateValue(name, fieldAt(compiled.fields, name), value);
|
|
48
|
+
for (const [name, collection] of compiled.collections) {
|
|
49
|
+
const given = initial?.[name];
|
|
50
|
+
rows.set(name, given === undefined ? [] : newRows(collection, `Collection "${name}" initial`, given));
|
|
51
|
+
}
|
|
52
|
+
// A supplied option is a frozen copy, apart from the author's object.
|
|
53
|
+
for (const [name, value] of supplied)
|
|
54
|
+
values[name] = adopt(compiled.fields[name], value, []);
|
|
55
|
+
// A field of initial prevails over its formula when initial has it as its
|
|
56
|
+
// own property, also one that is not enumerable and gives no value.
|
|
57
|
+
const given = Object.keys(compiled.fields).filter((name) => initial !== undefined && Object.hasOwn(initial, name));
|
|
58
|
+
return { values, rows, supplied: new Set(given) };
|
|
59
|
+
};
|
|
60
|
+
// What a session is given: its context, the defaults of its form with it, and
|
|
61
|
+
// the deriver of the states it publishes, with the messages of its form.
|
|
62
|
+
export const givenOf = (compiled, context) => ({
|
|
63
|
+
context,
|
|
64
|
+
defaults: defaultsOf(compiled, context),
|
|
65
|
+
deriver: createDeriver(compiled.messages),
|
|
66
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Address, type Field, type Row } from "./address.js";
|
|
2
|
+
import { type CompiledCollection } from "./collections.js";
|
|
3
|
+
import type { NewRow } from "./commands.js";
|
|
4
|
+
import type { Compiled } from "./compiled.js";
|
|
5
|
+
import type { Given } from "./reads.js";
|
|
6
|
+
import { type Candidate, type Store } from "./tree.js";
|
|
7
|
+
export declare const opening: (compiled: Compiled, given: Given, initial: unknown) => {
|
|
8
|
+
candidate: Candidate;
|
|
9
|
+
supplied: (address: Field) => boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare const editsOf: (compiled: Compiled) => Map<Field, unknown> | undefined;
|
|
12
|
+
export declare const edited: (compiled: Compiled, store: Store, name: PropertyKey, value: unknown) => Candidate | undefined;
|
|
13
|
+
export declare const patched: (compiled: Compiled, store: Store, writes: ReadonlyMap<Field, unknown>, edits?: Map<Field, unknown>) => Candidate | undefined;
|
|
14
|
+
export declare const added: (store: Store, collection: CompiledCollection, rows: readonly NewRow[], at: number) => {
|
|
15
|
+
candidate: undefined;
|
|
16
|
+
created: never[];
|
|
17
|
+
supplied: undefined;
|
|
18
|
+
roots: never[];
|
|
19
|
+
} | {
|
|
20
|
+
candidate: Candidate;
|
|
21
|
+
created: Row[];
|
|
22
|
+
supplied: (address: Field) => boolean;
|
|
23
|
+
roots: Address[];
|
|
24
|
+
};
|
|
25
|
+
export declare const removed: (store: Store, collection: CompiledCollection, rows: readonly Row[]) => Candidate | undefined;
|
|
26
|
+
export declare const moved: (store: Store, collection: CompiledCollection, row: Row, to: number) => Candidate | undefined;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// The candidate of each operation of the engine: a copy of the store with the
|
|
2
|
+
// fields it writes or the rows it adds, removes or moves.
|
|
3
|
+
import { createRow, definitionAt, rowAddress, } from "./address.js";
|
|
4
|
+
import { adopt, fieldAt, validateValue } from "./fields.js";
|
|
5
|
+
import { createBaseline } from "./baseline.js";
|
|
6
|
+
import { unknownCollection } from "./collections.js";
|
|
7
|
+
import { candidateOf, valueAt, writeAt } from "./tree.js";
|
|
8
|
+
const suppliedIn = (supplied) => (address) => typeof address !== "string" && supplied.get(address.row)?.has(address.field) === true;
|
|
9
|
+
// New rows take their keys and go at their position, after every row in
|
|
10
|
+
// creation order.
|
|
11
|
+
const withRows = (draft, collection, rows, at, supplied) => {
|
|
12
|
+
const created = rows.map((row) => {
|
|
13
|
+
const found = createRow(collection);
|
|
14
|
+
supplied.set(found, row.supplied);
|
|
15
|
+
return found;
|
|
16
|
+
});
|
|
17
|
+
const map = new Map(draft.rows);
|
|
18
|
+
for (const row of created)
|
|
19
|
+
map.set(row.key, row);
|
|
20
|
+
// A batch of any size: its keys are no arguments of a call.
|
|
21
|
+
const before = [...draft.order];
|
|
22
|
+
const order = before.slice(0, at);
|
|
23
|
+
for (const row of created)
|
|
24
|
+
order.push(row.key);
|
|
25
|
+
for (let index = at; index < before.length; index++)
|
|
26
|
+
order.push(before[index]);
|
|
27
|
+
return {
|
|
28
|
+
created,
|
|
29
|
+
draft: {
|
|
30
|
+
...draft,
|
|
31
|
+
order: Object.freeze(order),
|
|
32
|
+
created: [...draft.created, ...created],
|
|
33
|
+
rows: map,
|
|
34
|
+
written: new Map(created.map((row, index) => [row.key, { ...rows[index]?.values }])),
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
// The opening values: the baseline, whose rows get their keys in its order,
|
|
39
|
+
// and the formulas of every field, whose supplied values prevail.
|
|
40
|
+
export const opening = (compiled, given, initial) => {
|
|
41
|
+
const baseline = createBaseline(compiled, given.defaults, initial);
|
|
42
|
+
const candidate = { values: { ...baseline.values }, collections: new Map() };
|
|
43
|
+
const supplied = new Map();
|
|
44
|
+
for (const [name, rows] of baseline.rows) {
|
|
45
|
+
const collection = compiled.collections.get(name);
|
|
46
|
+
candidate.collections.set(name, withRows(empty(), collection, rows, 0, supplied).draft);
|
|
47
|
+
}
|
|
48
|
+
// The engine keeps what each field and row was given, never the values.
|
|
49
|
+
const rowSupplied = suppliedIn(supplied);
|
|
50
|
+
const header = baseline.supplied;
|
|
51
|
+
return {
|
|
52
|
+
candidate,
|
|
53
|
+
supplied: (address) => typeof address === "string" ? header.has(address) : rowSupplied(address),
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
const empty = () => ({
|
|
57
|
+
order: [],
|
|
58
|
+
created: [],
|
|
59
|
+
rows: new Map(),
|
|
60
|
+
records: { published: [], at: new Map() },
|
|
61
|
+
written: new Map(),
|
|
62
|
+
});
|
|
63
|
+
// Where a command keeps its direct edits: only a form with onEdit does.
|
|
64
|
+
export const editsOf = (compiled) => compiled.hasEdits ? new Map() : undefined;
|
|
65
|
+
// An option with the value held is the held one, so it is no difference. An
|
|
66
|
+
// edit without difference runs no formula and no event.
|
|
67
|
+
export const edited = (compiled, store, name, value) => {
|
|
68
|
+
const field = String(name);
|
|
69
|
+
const item = fieldAt(compiled.fields, name);
|
|
70
|
+
if (item === undefined && typeof name === "string" && compiled.collections.has(name))
|
|
71
|
+
throw new Error(`Collection "${name}" cannot be edited as a field; use row or patch`);
|
|
72
|
+
validateValue(field, item, value);
|
|
73
|
+
const previous = store.values;
|
|
74
|
+
const written = adopt(item, value, [previous[field]]);
|
|
75
|
+
if (Object.is(previous[field], written))
|
|
76
|
+
return;
|
|
77
|
+
const candidate = candidateOf(store);
|
|
78
|
+
candidate.values[field] = written;
|
|
79
|
+
return candidate;
|
|
80
|
+
};
|
|
81
|
+
// A patch writes its fields, of the header or of rows, whose options are
|
|
82
|
+
// adopted as in an edit, each against the value at its own address, and runs
|
|
83
|
+
// as an edit of those fields. One that changes nothing runs nothing. Given
|
|
84
|
+
// edits, it keeps there each field it changed with the value it stores: the
|
|
85
|
+
// adopted value is the one held when it is equal by its type, so a change is
|
|
86
|
+
// a difference by that equality.
|
|
87
|
+
export const patched = (compiled, store, writes, edits) => {
|
|
88
|
+
const candidate = candidateOf(store);
|
|
89
|
+
let differs = false;
|
|
90
|
+
for (const [address, value] of writes) {
|
|
91
|
+
const previous = valueAt(store.values, store.collections, address);
|
|
92
|
+
const written = adopt(definitionAt(compiled.fields, address), value, [previous]);
|
|
93
|
+
writeAt(candidate, address, written);
|
|
94
|
+
if (Object.is(previous, written))
|
|
95
|
+
continue;
|
|
96
|
+
differs = true;
|
|
97
|
+
edits?.set(address, written);
|
|
98
|
+
}
|
|
99
|
+
return differs ? candidate : undefined;
|
|
100
|
+
};
|
|
101
|
+
// Adding no row changes nothing. The roots of an add are the structure of its
|
|
102
|
+
// collection and every field of the rows it creates.
|
|
103
|
+
export const added = (store, collection, rows, at) => {
|
|
104
|
+
if (rows.length === 0)
|
|
105
|
+
return { candidate: undefined, created: [], supplied: undefined, roots: [] };
|
|
106
|
+
const candidate = candidateOf(store);
|
|
107
|
+
const supplied = new Map();
|
|
108
|
+
const { draft, created } = withRows(draftOf(candidate, collection), collection, rows, at, supplied);
|
|
109
|
+
candidate.collections.set(collection.name, draft);
|
|
110
|
+
const roots = [collection.structure];
|
|
111
|
+
for (const row of created)
|
|
112
|
+
for (const name of Object.keys(collection.fields))
|
|
113
|
+
roots.push(rowAddress(row, name));
|
|
114
|
+
return { candidate, created, supplied: suppliedIn(supplied), roots };
|
|
115
|
+
};
|
|
116
|
+
// Removing no row changes nothing.
|
|
117
|
+
export const removed = (store, collection, rows) => {
|
|
118
|
+
if (rows.length === 0)
|
|
119
|
+
return;
|
|
120
|
+
const candidate = candidateOf(store);
|
|
121
|
+
const draft = draftOf(candidate, collection);
|
|
122
|
+
const gone = new Set(rows);
|
|
123
|
+
const map = new Map(draft.rows);
|
|
124
|
+
for (const row of rows)
|
|
125
|
+
map.delete(row.key);
|
|
126
|
+
const order = [];
|
|
127
|
+
for (const key of draft.order)
|
|
128
|
+
if (map.has(key))
|
|
129
|
+
order.push(key);
|
|
130
|
+
candidate.collections.set(collection.name, {
|
|
131
|
+
...draft,
|
|
132
|
+
order: Object.freeze(order),
|
|
133
|
+
created: draft.created.filter((row) => !gone.has(row)),
|
|
134
|
+
rows: map,
|
|
135
|
+
});
|
|
136
|
+
return candidate;
|
|
137
|
+
};
|
|
138
|
+
// A row moved to its own position moves nothing.
|
|
139
|
+
export const moved = (store, collection, row, to) => {
|
|
140
|
+
const candidate = candidateOf(store);
|
|
141
|
+
const draft = draftOf(candidate, collection);
|
|
142
|
+
if (draft.order[to] === row.key)
|
|
143
|
+
return;
|
|
144
|
+
const order = [...draft.order];
|
|
145
|
+
order.splice(order.indexOf(row.key), 1);
|
|
146
|
+
order.splice(to, 0, row.key);
|
|
147
|
+
candidate.collections.set(collection.name, { ...draft, order: Object.freeze(order) });
|
|
148
|
+
return candidate;
|
|
149
|
+
};
|
|
150
|
+
const draftOf = (candidate, collection) => {
|
|
151
|
+
const draft = candidate.collections.get(collection.name);
|
|
152
|
+
if (draft === undefined)
|
|
153
|
+
throw unknownCollection(collection.name);
|
|
154
|
+
return draft;
|
|
155
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type FieldDefinition } from "./fields.js";
|
|
2
|
+
import { type Row, type StructureAddress } from "./address.js";
|
|
3
|
+
import type { EndpointTable } from "./compiled.js";
|
|
4
|
+
import type { CompiledQuery, ParamsFormula } from "./params.js";
|
|
5
|
+
import { type Owner } from "./remote-fields.js";
|
|
6
|
+
import { type Fields } from "./definition.js";
|
|
7
|
+
import type { FieldEvent, FieldFormula, StateFormula } from "./rules.js";
|
|
8
|
+
import { type States } from "./states.js";
|
|
9
|
+
export type CompiledCollection = {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly fields: Readonly<Record<string, FieldDefinition>>;
|
|
12
|
+
readonly structure: StructureAddress;
|
|
13
|
+
readonly index: number;
|
|
14
|
+
readonly positions: ReadonlyMap<string, number>;
|
|
15
|
+
readonly width: number;
|
|
16
|
+
readonly shape: object;
|
|
17
|
+
readonly fieldDefaults: Readonly<Record<string, unknown>>;
|
|
18
|
+
readonly declared: States;
|
|
19
|
+
readonly endpoints: EndpointTable;
|
|
20
|
+
readonly formulas: Map<string, FieldFormula>;
|
|
21
|
+
readonly events: Map<string, FieldEvent>;
|
|
22
|
+
readonly states: StateFormula[];
|
|
23
|
+
readonly own: StateFormula[];
|
|
24
|
+
readonly params: ParamsFormula[];
|
|
25
|
+
readonly queries: Map<string, CompiledQuery>;
|
|
26
|
+
};
|
|
27
|
+
export declare const checkDeclared: (owner: Owner, fields: unknown, collections: unknown, context: unknown, table: EndpointTable | undefined) => {
|
|
28
|
+
fields: Fields;
|
|
29
|
+
endpoints: EndpointTable;
|
|
30
|
+
collections: {
|
|
31
|
+
checked: Map<string, Checked>;
|
|
32
|
+
definition: Readonly<{}>;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
type Checked = {
|
|
36
|
+
readonly name: string;
|
|
37
|
+
readonly fields: Fields;
|
|
38
|
+
readonly endpoints: EndpointTable;
|
|
39
|
+
};
|
|
40
|
+
export declare const compileCollections: (checked: ReadonlyMap<string, Checked>) => Map<string, CompiledCollection>;
|
|
41
|
+
export declare const shapeOf: (names: Iterable<string>) => {};
|
|
42
|
+
export declare const positionsOf: (names: readonly string[]) => Map<string, number>;
|
|
43
|
+
export declare const formulaOf: (row: Row, field: string) => Readonly<FieldFormula & {
|
|
44
|
+
address: import("./address.js").RowAddress;
|
|
45
|
+
} & {
|
|
46
|
+
label: string;
|
|
47
|
+
row: Row;
|
|
48
|
+
}> | undefined;
|
|
49
|
+
export declare const eventOf: (row: Row, field: string) => Readonly<FieldEvent & {
|
|
50
|
+
address: import("./address.js").RowAddress;
|
|
51
|
+
} & {
|
|
52
|
+
label: string;
|
|
53
|
+
row: Row;
|
|
54
|
+
}> | undefined;
|
|
55
|
+
export declare const paramsOf: (row: Row, template: ParamsFormula) => ParamsFormula;
|
|
56
|
+
export declare const stateOf: (row: Row, template: StateFormula) => StateFormula;
|
|
57
|
+
export declare const rowLabel: (row: Row, name: string, label: string) => string;
|
|
58
|
+
export declare const unknownCollection: (name: unknown) => Error;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// The collections of a form: each one declares the fields of its rows, which
|
|
2
|
+
// compile with the functions of the fields of the form, and the each of its
|
|
3
|
+
// rules compiles to templates that every row instantiates when it needs them.
|
|
4
|
+
// The async fields of its rows take their endpoints from the same registry as
|
|
5
|
+
// those of the header.
|
|
6
|
+
import { defineOwn, describe } from "./data.js";
|
|
7
|
+
import { defaultsOf, fieldAt, isPlainRecord, isRecord, checkField, checkOrder, } from "./fields.js";
|
|
8
|
+
import { fieldLabel, instanceOf, rowAddress, templateLabel, } from "./address.js";
|
|
9
|
+
import { resolveEndpoints } from "./remote-fields.js";
|
|
10
|
+
import { admit } from "./subtypes.js";
|
|
11
|
+
import { declaration } from "./definition.js";
|
|
12
|
+
import { declaredStates } from "./states.js";
|
|
13
|
+
// What a form or a rule set declares, checked in this order: its fields, each
|
|
14
|
+
// one and its endpoint, its collections and the mark of its context. owner
|
|
15
|
+
// names it in the diagnostics.
|
|
16
|
+
export const checkDeclared = (owner, fields, collections, context, table) => {
|
|
17
|
+
if (!isRecord(fields))
|
|
18
|
+
throw new Error(`${owner} fields must be an object`);
|
|
19
|
+
// Each field is checked, then admitted in the forms of table, under label.
|
|
20
|
+
const check = (name, item, label) => admit(checkField(name, item, label), table, label, owner);
|
|
21
|
+
const copy = Object.fromEntries(Object.entries(fields).map(([name, item]) => [name, check(name, item, fieldLabel(name))]));
|
|
22
|
+
const endpoints = resolveEndpoints(copy, table, fieldLabel, owner);
|
|
23
|
+
const checked = checkCollections(collections, copy, table, check, owner);
|
|
24
|
+
if (context !== undefined && context !== declaration)
|
|
25
|
+
throw new Error(`${owner} context must be declared with declareContext<T>()`);
|
|
26
|
+
return { fields: copy, endpoints, collections: checked };
|
|
27
|
+
};
|
|
28
|
+
// A section of collections, as defineForm or defineRules receives it: its
|
|
29
|
+
// definition, frozen, and each collection checked. Fields and collections
|
|
30
|
+
// never share a name.
|
|
31
|
+
const checkCollections = (declared, fields, table, check, owner) => {
|
|
32
|
+
const checked = new Map();
|
|
33
|
+
if (declared === undefined)
|
|
34
|
+
return { checked, definition: Object.freeze({}) };
|
|
35
|
+
if (!isPlainRecord(declared))
|
|
36
|
+
throw new Error(`${owner} collections must be an object keyed by collection`);
|
|
37
|
+
const definition = {};
|
|
38
|
+
for (const [name, entry] of Object.entries(declared)) {
|
|
39
|
+
const copy = compileFields(name, entry, fields, check);
|
|
40
|
+
const label = (field) => templateLabel(field, name);
|
|
41
|
+
checked.set(name, {
|
|
42
|
+
name,
|
|
43
|
+
fields: copy,
|
|
44
|
+
endpoints: resolveEndpoints(copy, table, label, owner),
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(definition, name, {
|
|
47
|
+
value: Object.freeze({ fields: copy }),
|
|
48
|
+
enumerable: true,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return { checked, definition: Object.freeze(definition) };
|
|
52
|
+
};
|
|
53
|
+
// The collections of a form compiled, in declaration order.
|
|
54
|
+
export const compileCollections = (checked) => new Map(Array.from(checked.values(), ({ name, fields, endpoints }, index) => [
|
|
55
|
+
name,
|
|
56
|
+
compileCollection(name, fields, endpoints, index),
|
|
57
|
+
]));
|
|
58
|
+
const compileFields = (name, entry, fields, check) => {
|
|
59
|
+
const prefix = `Collection "${name}"`;
|
|
60
|
+
checkOrder(prefix, name);
|
|
61
|
+
if (fieldAt(fields, name) !== undefined)
|
|
62
|
+
throw new Error(`Name "${name}" is declared in fields and collections`);
|
|
63
|
+
if (!isPlainRecord(entry))
|
|
64
|
+
throw new Error(`${prefix}: expected { fields }`);
|
|
65
|
+
for (const key of Object.keys(entry))
|
|
66
|
+
if (key !== "fields")
|
|
67
|
+
throw new Error(`${prefix}: unknown property "${key}"`);
|
|
68
|
+
const declared = entry.fields;
|
|
69
|
+
if (declared === undefined)
|
|
70
|
+
throw new Error(`${prefix} must declare fields`);
|
|
71
|
+
if (!isRecord(declared))
|
|
72
|
+
throw new Error(`${prefix}: fields must be an object`);
|
|
73
|
+
const copy = {};
|
|
74
|
+
for (const [field, item] of Object.entries(declared))
|
|
75
|
+
Object.defineProperty(copy, field, {
|
|
76
|
+
value: check(field, item, templateLabel(field, name)),
|
|
77
|
+
enumerable: true,
|
|
78
|
+
});
|
|
79
|
+
return Object.freeze(copy);
|
|
80
|
+
};
|
|
81
|
+
const compileCollection = (name, fields, endpoints, index) => {
|
|
82
|
+
const names = Object.keys(fields);
|
|
83
|
+
const collection = {
|
|
84
|
+
name,
|
|
85
|
+
fields,
|
|
86
|
+
index,
|
|
87
|
+
positions: positionsOf(names),
|
|
88
|
+
width: 2 * names.length,
|
|
89
|
+
shape: shapeOf(names),
|
|
90
|
+
fieldDefaults: defaultsOf(fields),
|
|
91
|
+
declared: declaredStates(fields),
|
|
92
|
+
endpoints,
|
|
93
|
+
formulas: new Map(),
|
|
94
|
+
events: new Map(),
|
|
95
|
+
states: [],
|
|
96
|
+
own: [],
|
|
97
|
+
params: [],
|
|
98
|
+
queries: new Map(),
|
|
99
|
+
};
|
|
100
|
+
collection.structure = Object.freeze({
|
|
101
|
+
kind: "structure",
|
|
102
|
+
collection: collection,
|
|
103
|
+
});
|
|
104
|
+
return collection;
|
|
105
|
+
};
|
|
106
|
+
// An object whose own keys are the names given, which a view answers with
|
|
107
|
+
// values. It is never frozen, so a view may answer them with anything.
|
|
108
|
+
export const shapeOf = (names) => {
|
|
109
|
+
const shape = {};
|
|
110
|
+
for (const name of names)
|
|
111
|
+
defineOwn(shape, name);
|
|
112
|
+
return shape;
|
|
113
|
+
};
|
|
114
|
+
// The position of each field in the traversal, 2i for the field at index i in
|
|
115
|
+
// declaration order: its event goes right after it, at 2i + 1.
|
|
116
|
+
export const positionsOf = (names) => new Map(names.map((name, at) => [name, 2 * at]));
|
|
117
|
+
// The instances of a row: its formula of value, its event and its params of a
|
|
118
|
+
// field, which take the address of the field, and each of its formulas of
|
|
119
|
+
// state, with the label of the row.
|
|
120
|
+
const fieldInstance = (row, field, template) => template &&
|
|
121
|
+
instanceOf(row, template, () => located(row, field, { ...template, address: rowAddress(row, field) }));
|
|
122
|
+
export const formulaOf = (row, field) => fieldInstance(row, field, row.collection.formulas.get(field));
|
|
123
|
+
export const eventOf = (row, field) => fieldInstance(row, field, row.collection.events.get(field));
|
|
124
|
+
export const paramsOf = (row, template) => fieldInstance(row, template.name, template);
|
|
125
|
+
export const stateOf = (row, template) => instanceOf(row, template, () => template.own === undefined
|
|
126
|
+
? located(row, template.name, template)
|
|
127
|
+
: Object.freeze({ ...template, label: ownLabel(row, template.label), row }));
|
|
128
|
+
// A message of rows is labeled Row of "lines" error; its instance, with the key
|
|
129
|
+
// of its row after the collection.
|
|
130
|
+
const ownLabel = (row, label) => {
|
|
131
|
+
const scope = `Row of "${row.collection.name}"`;
|
|
132
|
+
return `${scope} row "${row.key}"${label.slice(scope.length)}`;
|
|
133
|
+
};
|
|
134
|
+
// A template is labeled Field "amount" of "lines" value; its instance, with
|
|
135
|
+
// the key of its row after the collection.
|
|
136
|
+
export const rowLabel = (row, name, label) => `${fieldLabel(name, row)}${label.slice(templateLabel(name, row.collection.name).length)}`;
|
|
137
|
+
const located = (row, name, template) => Object.freeze({ ...template, label: rowLabel(row, name, template.label), row });
|
|
138
|
+
export const unknownCollection = (name) => new Error(`Unknown collection ${describe(name)}`);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Row } from "./address.js";
|
|
2
|
+
import { type CompiledCollection } from "./collections.js";
|
|
3
|
+
import type { Store } from "./tree.js";
|
|
4
|
+
type Values = Record<string, unknown>;
|
|
5
|
+
export type NewRow = {
|
|
6
|
+
readonly values: Values;
|
|
7
|
+
readonly supplied: ReadonlySet<string>;
|
|
8
|
+
};
|
|
9
|
+
export declare const collectionOf: (collections: ReadonlyMap<string, CompiledCollection>, name: unknown) => CompiledCollection;
|
|
10
|
+
export declare const rowIn: (store: Store, collection: CompiledCollection, key: unknown) => Row | undefined;
|
|
11
|
+
export declare const rowOf: (store: Store, collection: CompiledCollection, key: unknown) => Row;
|
|
12
|
+
export declare const positionOf: (label: string, position: unknown, max: number) => number;
|
|
13
|
+
export declare const newRows: (collection: CompiledCollection, label: string, rows: unknown) => NewRow[];
|
|
14
|
+
export declare const insertionOf: (label: string, options: unknown, length: number) => number;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { unknownCollection } from "./collections.js";
|
|
2
|
+
import { describe, ownEntries } from "./data.js";
|
|
3
|
+
import { adopt, fieldAt, isPlainRecord } from "./fields.js";
|
|
4
|
+
import { incompatibleValue } from "./patches.js";
|
|
5
|
+
import { accepts } from "./fields.js";
|
|
6
|
+
export const collectionOf = (collections, name) => {
|
|
7
|
+
const found = typeof name === "string" ? collections.get(name) : undefined;
|
|
8
|
+
if (found === undefined)
|
|
9
|
+
throw unknownCollection(name);
|
|
10
|
+
return found;
|
|
11
|
+
};
|
|
12
|
+
// The current row of a key in a collection of the store, if any.
|
|
13
|
+
export const rowIn = (store, collection, key) => typeof key === "string"
|
|
14
|
+
? store.collections.get(collection.name)?.rows.get(key)
|
|
15
|
+
: undefined;
|
|
16
|
+
// A key of a current row of the collection; a key of another collection or of
|
|
17
|
+
// another session is unknown as well.
|
|
18
|
+
export const rowOf = (store, collection, key) => {
|
|
19
|
+
const row = rowIn(store, collection, key);
|
|
20
|
+
if (row === undefined)
|
|
21
|
+
throw new Error(`Collection "${collection.name}": unknown row key ${describe(key)}`);
|
|
22
|
+
return row;
|
|
23
|
+
};
|
|
24
|
+
// An integer from 0 to max.
|
|
25
|
+
export const positionOf = (label, position, max) => {
|
|
26
|
+
if (!Number.isInteger(position))
|
|
27
|
+
throw new Error(`${label}: position must be an integer, got ${describe(position)}`);
|
|
28
|
+
if (position < 0 || position > max)
|
|
29
|
+
throw new Error(`${label}: position ${position} is out of range 0..${max}`);
|
|
30
|
+
return position;
|
|
31
|
+
};
|
|
32
|
+
// The rows of an add or of the initial values, labeled with their index.
|
|
33
|
+
export const newRows = (collection, label, rows) => {
|
|
34
|
+
if (!Array.isArray(rows))
|
|
35
|
+
throw new Error(`${label}: rows must be an array`);
|
|
36
|
+
return Array.from(rows, (row, index) => newRow(collection, `${label}[${index}]`, row));
|
|
37
|
+
};
|
|
38
|
+
const newRow = (collection, label, row) => {
|
|
39
|
+
if (!isPlainRecord(row))
|
|
40
|
+
throw new Error(`${label}: expected an object of field values`);
|
|
41
|
+
const values = { ...collection.fieldDefaults };
|
|
42
|
+
const supplied = new Set();
|
|
43
|
+
for (const [key, value] of ownEntries(row)) {
|
|
44
|
+
const item = fieldAt(collection.fields, key);
|
|
45
|
+
if (item === undefined)
|
|
46
|
+
throw new Error(`${label}: unknown field ${describe(key)}`);
|
|
47
|
+
if (!accepts(item, value))
|
|
48
|
+
throw incompatibleValue(label, item, key, value);
|
|
49
|
+
values[key] = adopt(item, value, []);
|
|
50
|
+
supplied.add(key);
|
|
51
|
+
}
|
|
52
|
+
return { values, supplied };
|
|
53
|
+
};
|
|
54
|
+
// The options of an add: where its rows go, at the end by default.
|
|
55
|
+
export const insertionOf = (label, options, length) => {
|
|
56
|
+
if (options === undefined)
|
|
57
|
+
return length;
|
|
58
|
+
if (!isPlainRecord(options))
|
|
59
|
+
throw new Error(`${label}: options must be an object`);
|
|
60
|
+
for (const key of Object.keys(options))
|
|
61
|
+
if (key !== "at")
|
|
62
|
+
throw new Error(`${label}: unknown option "${key}"`);
|
|
63
|
+
return options.at === undefined ? length : positionOf(label, options.at, length);
|
|
64
|
+
};
|