@azx-pbc/axiom-react 0.0.0-stage → 0.0.6
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 +604 -2
- package/dist/FormErrorBoundary.d.ts +37 -0
- package/dist/FormErrorBoundary.d.ts.map +1 -0
- package/dist/FormErrorBoundary.js +54 -0
- package/dist/FormErrorBoundary.js.map +1 -0
- package/dist/FormProvider.d.ts +197 -0
- package/dist/FormProvider.d.ts.map +1 -0
- package/dist/FormProvider.js +149 -0
- package/dist/FormProvider.js.map +1 -0
- package/dist/field-error.d.ts +154 -0
- package/dist/field-error.d.ts.map +1 -0
- package/dist/field-error.js +2 -0
- package/dist/field-error.js.map +1 -0
- package/dist/field-props.d.ts +362 -0
- package/dist/field-props.d.ts.map +1 -0
- package/dist/field-props.js +64 -0
- package/dist/field-props.js.map +1 -0
- package/dist/form-context.d.ts +31 -0
- package/dist/form-context.d.ts.map +1 -0
- package/dist/form-context.js +13 -0
- package/dist/form-context.js.map +1 -0
- package/dist/form-store.d.ts +784 -0
- package/dist/form-store.d.ts.map +1 -0
- package/dist/form-store.js +2179 -0
- package/dist/form-store.js.map +1 -0
- package/dist/hints.d.ts +60 -0
- package/dist/hints.d.ts.map +1 -0
- package/dist/hints.js +158 -0
- package/dist/hints.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/render/field-value.d.ts +92 -0
- package/dist/render/field-value.d.ts.map +1 -0
- package/dist/render/field-value.js +124 -0
- package/dist/render/field-value.js.map +1 -0
- package/dist/render/nodes.d.ts +303 -0
- package/dist/render/nodes.d.ts.map +1 -0
- package/dist/render/nodes.js +325 -0
- package/dist/render/nodes.js.map +1 -0
- package/dist/render/schema.d.ts +30 -0
- package/dist/render/schema.d.ts.map +1 -0
- package/dist/render/schema.js +40 -0
- package/dist/render/schema.js.map +1 -0
- package/dist/render/value-text.d.ts +59 -0
- package/dist/render/value-text.d.ts.map +1 -0
- package/dist/render/value-text.js +104 -0
- package/dist/render/value-text.js.map +1 -0
- package/dist/store/array-bindings.d.ts +110 -0
- package/dist/store/array-bindings.d.ts.map +1 -0
- package/dist/store/array-bindings.js +181 -0
- package/dist/store/array-bindings.js.map +1 -0
- package/dist/store/bindings.d.ts +12 -0
- package/dist/store/bindings.d.ts.map +1 -0
- package/dist/store/bindings.js +16 -0
- package/dist/store/bindings.js.map +1 -0
- package/dist/store/buffer-state.d.ts +42 -0
- package/dist/store/buffer-state.d.ts.map +1 -0
- package/dist/store/buffer-state.js +51 -0
- package/dist/store/buffer-state.js.map +1 -0
- package/dist/store/configure.d.ts +19 -0
- package/dist/store/configure.d.ts.map +1 -0
- package/dist/store/configure.js +28 -0
- package/dist/store/configure.js.map +1 -0
- package/dist/store/derive.d.ts +106 -0
- package/dist/store/derive.d.ts.map +1 -0
- package/dist/store/derive.js +417 -0
- package/dist/store/derive.js.map +1 -0
- package/dist/store/errors.d.ts +20 -0
- package/dist/store/errors.d.ts.map +1 -0
- package/dist/store/errors.js +101 -0
- package/dist/store/errors.js.map +1 -0
- package/dist/store/fields.d.ts +35 -0
- package/dist/store/fields.d.ts.map +1 -0
- package/dist/store/fields.js +81 -0
- package/dist/store/fields.js.map +1 -0
- package/dist/store/form-channel.d.ts +174 -0
- package/dist/store/form-channel.d.ts.map +1 -0
- package/dist/store/form-channel.js +411 -0
- package/dist/store/form-channel.js.map +1 -0
- package/dist/store/gates.d.ts +22 -0
- package/dist/store/gates.d.ts.map +1 -0
- package/dist/store/gates.js +44 -0
- package/dist/store/gates.js.map +1 -0
- package/dist/store/ids.d.ts +20 -0
- package/dist/store/ids.d.ts.map +1 -0
- package/dist/store/ids.js +28 -0
- package/dist/store/ids.js.map +1 -0
- package/dist/store/inputs.d.ts +114 -0
- package/dist/store/inputs.d.ts.map +1 -0
- package/dist/store/inputs.js +202 -0
- package/dist/store/inputs.js.map +1 -0
- package/dist/store/layout.d.ts +81 -0
- package/dist/store/layout.d.ts.map +1 -0
- package/dist/store/layout.js +285 -0
- package/dist/store/layout.js.map +1 -0
- package/dist/store/messages.d.ts +56 -0
- package/dist/store/messages.d.ts.map +1 -0
- package/dist/store/messages.js +157 -0
- package/dist/store/messages.js.map +1 -0
- package/dist/store/option-pages.d.ts +193 -0
- package/dist/store/option-pages.d.ts.map +1 -0
- package/dist/store/option-pages.js +598 -0
- package/dist/store/option-pages.js.map +1 -0
- package/dist/store/parse-print.d.ts +105 -0
- package/dist/store/parse-print.d.ts.map +1 -0
- package/dist/store/parse-print.js +415 -0
- package/dist/store/parse-print.js.map +1 -0
- package/dist/store/proposals.d.ts +114 -0
- package/dist/store/proposals.d.ts.map +1 -0
- package/dist/store/proposals.js +90 -0
- package/dist/store/proposals.js.map +1 -0
- package/dist/store/publish.d.ts +48 -0
- package/dist/store/publish.d.ts.map +1 -0
- package/dist/store/publish.js +96 -0
- package/dist/store/publish.js.map +1 -0
- package/dist/store/snapshots.d.ts +63 -0
- package/dist/store/snapshots.d.ts.map +1 -0
- package/dist/store/snapshots.js +154 -0
- package/dist/store/snapshots.js.map +1 -0
- package/dist/store/steps.d.ts +13 -0
- package/dist/store/steps.d.ts.map +1 -0
- package/dist/store/steps.js +31 -0
- package/dist/store/steps.js.map +1 -0
- package/dist/store/view.d.ts +124 -0
- package/dist/store/view.d.ts.map +1 -0
- package/dist/store/view.js +301 -0
- package/dist/store/view.js.map +1 -0
- package/dist/store/visibility.d.ts +68 -0
- package/dist/store/visibility.d.ts.map +1 -0
- package/dist/store/visibility.js +231 -0
- package/dist/store/visibility.js.map +1 -0
- package/dist/store/wizard-cursor.d.ts +43 -0
- package/dist/store/wizard-cursor.d.ts.map +1 -0
- package/dist/store/wizard-cursor.js +67 -0
- package/dist/store/wizard-cursor.js.map +1 -0
- package/dist/store/writes.d.ts +50 -0
- package/dist/store/writes.d.ts.map +1 -0
- package/dist/store/writes.js +91 -0
- package/dist/store/writes.js.map +1 -0
- package/dist/typed.d.ts +134 -0
- package/dist/typed.d.ts.map +1 -0
- package/dist/typed.js +74 -0
- package/dist/typed.js.map +1 -0
- package/dist/useAsyncOptions.d.ts +20 -0
- package/dist/useAsyncOptions.d.ts.map +1 -0
- package/dist/useAsyncOptions.js +70 -0
- package/dist/useAsyncOptions.js.map +1 -0
- package/dist/useBufferedInput.d.ts +69 -0
- package/dist/useBufferedInput.d.ts.map +1 -0
- package/dist/useBufferedInput.js +132 -0
- package/dist/useBufferedInput.js.map +1 -0
- package/dist/useField.d.ts +42 -0
- package/dist/useField.d.ts.map +1 -0
- package/dist/useField.js +63 -0
- package/dist/useField.js.map +1 -0
- package/dist/useFieldArray.d.ts +25 -0
- package/dist/useFieldArray.d.ts.map +1 -0
- package/dist/useFieldArray.js +18 -0
- package/dist/useFieldArray.js.map +1 -0
- package/dist/useForm.d.ts +20 -0
- package/dist/useForm.d.ts.map +1 -0
- package/dist/useForm.js +9 -0
- package/dist/useForm.js.map +1 -0
- package/dist/useFormState.d.ts +23 -0
- package/dist/useFormState.d.ts.map +1 -0
- package/dist/useFormState.js +11 -0
- package/dist/useFormState.js.map +1 -0
- package/dist/useGate.d.ts +15 -0
- package/dist/useGate.d.ts.map +1 -0
- package/dist/useGate.js +21 -0
- package/dist/useGate.js.map +1 -0
- package/dist/useLayout.d.ts +29 -0
- package/dist/useLayout.d.ts.map +1 -0
- package/dist/useLayout.js +59 -0
- package/dist/useLayout.js.map +1 -0
- package/dist/useMessage.d.ts +58 -0
- package/dist/useMessage.d.ts.map +1 -0
- package/dist/useMessage.js +62 -0
- package/dist/useMessage.js.map +1 -0
- package/dist/useWatch.d.ts +19 -0
- package/dist/useWatch.d.ts.map +1 -0
- package/dist/useWatch.js +17 -0
- package/dist/useWatch.js.map +1 -0
- package/dist/useWizard.d.ts +166 -0
- package/dist/useWizard.d.ts.map +1 -0
- package/dist/useWizard.js +554 -0
- package/dist/useWizard.js.map +1 -0
- package/package.json +62 -3
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
import { canonicalizeRestricted, isRecord, isWireValue } from '@azx-pbc/axiom-runtime';
|
|
2
|
+
import { bindingKey } from './bindings.js';
|
|
3
|
+
/**
|
|
4
|
+
* How long a settled search waits before the loader is asked for a page.
|
|
5
|
+
*
|
|
6
|
+
* Only the request is debounced: the renderer publishes each edit immediately, the machine
|
|
7
|
+
* drops the pages that answered the previous query and aborts the request in flight, and the
|
|
8
|
+
* loader is invoked once the reader has stopped typing for this long. The wait itself is the
|
|
9
|
+
* host's (`QueryDefer`): these bindings read no clock, and the same code renders on a server, in
|
|
10
|
+
* a browser and on a device.
|
|
11
|
+
*/
|
|
12
|
+
const OPTION_QUERY_DEBOUNCE_MS = 250;
|
|
13
|
+
/** The most code points a query may carry; text past this is truncated before a loader sees it. */
|
|
14
|
+
const OPTION_QUERY_MAX_CODE_POINTS = 200;
|
|
15
|
+
/**
|
|
16
|
+
* The reader's text as a loader receives it: trimmed, and truncated to the code-point bound.
|
|
17
|
+
*
|
|
18
|
+
* A code point is not a UTF-16 unit, so an astral character costs one: slicing the string at
|
|
19
|
+
* 200 units would cut a surrogate pair in half and hand the loader a lone surrogate.
|
|
20
|
+
*/
|
|
21
|
+
function normaliseOptionQuery(text) {
|
|
22
|
+
const trimmed = text.trim();
|
|
23
|
+
const points = Array.from(trimmed);
|
|
24
|
+
return points.length <= OPTION_QUERY_MAX_CODE_POINTS
|
|
25
|
+
? trimmed
|
|
26
|
+
: points.slice(0, OPTION_QUERY_MAX_CODE_POINTS).join('');
|
|
27
|
+
}
|
|
28
|
+
const EMPTY_ITEMS = Object.freeze([]);
|
|
29
|
+
const EMPTY_STATE = Object.freeze({
|
|
30
|
+
items: EMPTY_ITEMS,
|
|
31
|
+
offered: EMPTY_ITEMS,
|
|
32
|
+
loading: false,
|
|
33
|
+
resolved: false,
|
|
34
|
+
error: undefined,
|
|
35
|
+
hasMore: false,
|
|
36
|
+
cursor: null,
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* The page machine: one bounded, cancellable page list per array row or top-level field.
|
|
40
|
+
*
|
|
41
|
+
* The engine stores no option state, so everything a loaded domain needs lives here: the pages,
|
|
42
|
+
* the cursor, the error and the one signal that decides staleness. `begin()` on the binding's
|
|
43
|
+
* cancellation port cancels the previous request before returning, so a response that arrives
|
|
44
|
+
* after a newer request began — or after unmount — commits nothing on either arm.
|
|
45
|
+
*/
|
|
46
|
+
export class OptionPages {
|
|
47
|
+
loaders;
|
|
48
|
+
createPort;
|
|
49
|
+
defer;
|
|
50
|
+
entries = new Map();
|
|
51
|
+
listeners = new Set();
|
|
52
|
+
domainsCache;
|
|
53
|
+
unmounted = false;
|
|
54
|
+
constructor(loaders, createPort, defer) {
|
|
55
|
+
this.loaders = { ...loaders };
|
|
56
|
+
this.createPort = createPort;
|
|
57
|
+
this.defer = defer;
|
|
58
|
+
}
|
|
59
|
+
/** One binding's current page state; the same object until a fact it states changes. */
|
|
60
|
+
stateOf(key) {
|
|
61
|
+
const entry = this.entries.get(key);
|
|
62
|
+
if (entry === undefined)
|
|
63
|
+
return EMPTY_STATE;
|
|
64
|
+
entry.state ??= Object.freeze({
|
|
65
|
+
items: entry.items,
|
|
66
|
+
offered: entry.evidence,
|
|
67
|
+
loading: entry.loading,
|
|
68
|
+
resolved: entry.loaded,
|
|
69
|
+
error: entry.error,
|
|
70
|
+
hasMore: entry.nextCursor !== null,
|
|
71
|
+
cursor: entry.nextCursor,
|
|
72
|
+
});
|
|
73
|
+
return entry.state;
|
|
74
|
+
}
|
|
75
|
+
/** Registers a listener for every page-list change; the returned release is idempotent. */
|
|
76
|
+
subscribe(listener) {
|
|
77
|
+
this.listeners.add(listener);
|
|
78
|
+
return () => {
|
|
79
|
+
this.listeners.delete(listener);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/** Registers a consumer of one binding; the release cancels and evicts when it was the last. */
|
|
83
|
+
retain(key, fieldId, rowKey) {
|
|
84
|
+
const entry = this.entryFor(key, fieldId, rowKey);
|
|
85
|
+
entry.subscribers += 1;
|
|
86
|
+
return () => {
|
|
87
|
+
entry.subscribers -= 1;
|
|
88
|
+
if (entry.subscribers > 0)
|
|
89
|
+
return;
|
|
90
|
+
this.cancelWaiting(entry);
|
|
91
|
+
entry.signal?.abort();
|
|
92
|
+
this.entries.delete(key);
|
|
93
|
+
this.domainsCache = undefined;
|
|
94
|
+
this.notify();
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Keys one binding's pages by the basis they were loaded for, starting a load when it moved.
|
|
99
|
+
*
|
|
100
|
+
* A dependency the access answer withholds makes the basis unreadable: the pages are dropped
|
|
101
|
+
* and no request is made, because a stale domain must never stand in for a value this device
|
|
102
|
+
* may not read.
|
|
103
|
+
*
|
|
104
|
+
* A query edit is not this door's work: `search` has already dropped the pages that answered
|
|
105
|
+
* the previous text and scheduled the one request for the new text, so a render that follows
|
|
106
|
+
* an edit only records the basis it will be asked with. Every other member still starts its
|
|
107
|
+
* load at once.
|
|
108
|
+
*/
|
|
109
|
+
ensure(key, basis, pageSize) {
|
|
110
|
+
const entry = this.entryFor(key, basis.fieldId, basis.rowKey);
|
|
111
|
+
if (entry.basis?.signature === basis.signature &&
|
|
112
|
+
entry.basis.readable === basis.readable &&
|
|
113
|
+
entry.pageSize === pageSize) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
entry.basis = basis;
|
|
117
|
+
entry.pageSize = pageSize;
|
|
118
|
+
// A basis-driven retry keeps the previous failure on screen until this request settles, so a
|
|
119
|
+
// keystroke cannot unmount and re-insert the same alert. A basis that is not readable starts
|
|
120
|
+
// no request, so no failure survives it.
|
|
121
|
+
this.resetBasis(entry, basis.readable);
|
|
122
|
+
this.notify();
|
|
123
|
+
// StrictMode can replay a child's effect before the provider's remount effect. Record the
|
|
124
|
+
// basis now; `mount()` starts that pending list as soon as the session is live again.
|
|
125
|
+
if (!basis.readable || this.unmounted)
|
|
126
|
+
return;
|
|
127
|
+
this.load(entry, null);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Publishes the reader's search text, dropping the pages that answered the previous query.
|
|
131
|
+
*
|
|
132
|
+
* The edit lands at once and only the request waits: the aborted signal is what keeps a page
|
|
133
|
+
* that resolves after the edit from painting, and the pages are cleared so nothing states a
|
|
134
|
+
* match for text the reader has already changed. Two edits that trim to one query are one
|
|
135
|
+
* search, so surrounding whitespace never reaches a loader twice.
|
|
136
|
+
*/
|
|
137
|
+
search(key, text) {
|
|
138
|
+
const entry = this.entries.get(key);
|
|
139
|
+
if (entry === undefined)
|
|
140
|
+
return;
|
|
141
|
+
const query = normaliseOptionQuery(text);
|
|
142
|
+
if (entry.query === query)
|
|
143
|
+
return;
|
|
144
|
+
const answers = !this.unmounted && entry.basis !== undefined && entry.basis.readable;
|
|
145
|
+
const defer = this.defer;
|
|
146
|
+
if (defer === undefined) {
|
|
147
|
+
// Searching without the clock would issue one request per edit rather than one per settled
|
|
148
|
+
// query. Refuse the missing host capability before moving the pages or asking the loader.
|
|
149
|
+
throw new Error("option search requires FormProvider's `defer` timer so the loader call can wait 250 ms");
|
|
150
|
+
}
|
|
151
|
+
entry.query = query;
|
|
152
|
+
this.clearPages(entry, true);
|
|
153
|
+
this.cancelWaiting(entry);
|
|
154
|
+
if (answers && defer !== undefined) {
|
|
155
|
+
// The pages are gone and this binding is loading from the edit, which is the state the
|
|
156
|
+
// control paints as busy: the request that answers the text is scheduled, not yet started.
|
|
157
|
+
entry.loading = true;
|
|
158
|
+
entry.stopWaiting = defer(() => {
|
|
159
|
+
entry.stopWaiting = undefined;
|
|
160
|
+
if (this.unmounted || entry.basis === undefined || !entry.basis.readable) {
|
|
161
|
+
entry.loading = false;
|
|
162
|
+
this.invalidate(entry);
|
|
163
|
+
this.notify();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this.load(entry, null);
|
|
167
|
+
}, OPTION_QUERY_DEBOUNCE_MS);
|
|
168
|
+
}
|
|
169
|
+
this.invalidate(entry);
|
|
170
|
+
this.notify();
|
|
171
|
+
}
|
|
172
|
+
/** The stable controls one binding's consumers receive. */
|
|
173
|
+
controls(key) {
|
|
174
|
+
const entry = this.entries.get(key);
|
|
175
|
+
if (entry?.controls !== undefined)
|
|
176
|
+
return entry.controls;
|
|
177
|
+
const controls = Object.freeze({
|
|
178
|
+
loadMore: () => this.loadMore(key),
|
|
179
|
+
reload: () => this.reload(key),
|
|
180
|
+
search: (query) => this.search(key, query),
|
|
181
|
+
});
|
|
182
|
+
if (entry !== undefined)
|
|
183
|
+
entry.controls = controls;
|
|
184
|
+
return controls;
|
|
185
|
+
}
|
|
186
|
+
/** The domain every offered binding states, keyed by binding key; eligibility reads this. */
|
|
187
|
+
domains() {
|
|
188
|
+
this.domainsCache ??= new Map([...this.entries].flatMap(([key, entry]) => {
|
|
189
|
+
// A page is one window on the source, so the domain is everything the pages for this
|
|
190
|
+
// basis have offered: a query narrows what is painted, never what is offered, and a
|
|
191
|
+
// reader's search cannot make the value they hold ineligible.
|
|
192
|
+
if (entry.evidence.length > 0)
|
|
193
|
+
return [[key, entry.evidence]];
|
|
194
|
+
// A resolved empty page is an answer like any other — the source offers nothing — while
|
|
195
|
+
// a binding the source has not answered yet decides nothing, so a selection is not
|
|
196
|
+
// refused by a question the host has not answered.
|
|
197
|
+
return entry.answered ? [[key, EMPTY_ITEMS]] : [];
|
|
198
|
+
}));
|
|
199
|
+
return this.domainsCache;
|
|
200
|
+
}
|
|
201
|
+
/** Resumes after a remount, including a child effect that recorded its basis while unmounted. */
|
|
202
|
+
mount() {
|
|
203
|
+
this.unmounted = false;
|
|
204
|
+
for (const entry of this.entries.values()) {
|
|
205
|
+
if (entry.basis?.readable === true && !entry.loaded && !entry.loading) {
|
|
206
|
+
this.load(entry, null);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** Cancels every in-flight request; the page lists stay readable for a remount. */
|
|
211
|
+
cancelAll() {
|
|
212
|
+
for (const entry of this.entries.values()) {
|
|
213
|
+
this.cancelWaiting(entry);
|
|
214
|
+
entry.signal?.abort();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** Drops every entry; a provider that unmounts publishes nothing afterwards. */
|
|
218
|
+
unmount() {
|
|
219
|
+
this.unmounted = true;
|
|
220
|
+
this.cancelAll();
|
|
221
|
+
this.entries.clear();
|
|
222
|
+
// Listeners stay registered: a StrictMode remount resumes the same session, and the store's
|
|
223
|
+
// own domain observer must survive it.
|
|
224
|
+
this.domainsCache = undefined;
|
|
225
|
+
}
|
|
226
|
+
entryFor(keyValue, fieldId, rowKey) {
|
|
227
|
+
const existing = this.entries.get(keyValue);
|
|
228
|
+
if (existing !== undefined)
|
|
229
|
+
return existing;
|
|
230
|
+
const entry = {
|
|
231
|
+
key: keyValue,
|
|
232
|
+
fieldId,
|
|
233
|
+
...(rowKey === undefined ? {} : { rowKey }),
|
|
234
|
+
port: this.createPort(),
|
|
235
|
+
basis: undefined,
|
|
236
|
+
pageSize: 0,
|
|
237
|
+
query: '',
|
|
238
|
+
items: EMPTY_ITEMS,
|
|
239
|
+
evidence: EMPTY_ITEMS,
|
|
240
|
+
offeredKeys: new Set(),
|
|
241
|
+
loaded: false,
|
|
242
|
+
answered: false,
|
|
243
|
+
loading: false,
|
|
244
|
+
error: undefined,
|
|
245
|
+
nextCursor: null,
|
|
246
|
+
signal: undefined,
|
|
247
|
+
stopWaiting: undefined,
|
|
248
|
+
subscribers: 0,
|
|
249
|
+
state: undefined,
|
|
250
|
+
controls: undefined,
|
|
251
|
+
};
|
|
252
|
+
this.entries.set(keyValue, entry);
|
|
253
|
+
return entry;
|
|
254
|
+
}
|
|
255
|
+
loadMore(keyValue) {
|
|
256
|
+
const entry = this.entries.get(keyValue);
|
|
257
|
+
if (entry === undefined || entry.loading || entry.nextCursor === null)
|
|
258
|
+
return;
|
|
259
|
+
if (entry.basis === undefined || !entry.basis.readable)
|
|
260
|
+
return;
|
|
261
|
+
this.load(entry, entry.nextCursor);
|
|
262
|
+
}
|
|
263
|
+
reload(keyValue) {
|
|
264
|
+
const entry = this.entries.get(keyValue);
|
|
265
|
+
if (entry?.basis === undefined || !entry.basis.readable)
|
|
266
|
+
return;
|
|
267
|
+
// A manual reload is a new request the user asked for; its failure is a new announcement.
|
|
268
|
+
this.resetBasis(entry, false);
|
|
269
|
+
this.notify();
|
|
270
|
+
this.load(entry, null);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Drops this binding's pages and everything the pages prove about the domain.
|
|
274
|
+
*
|
|
275
|
+
* The domain evidence is only as good as the basis it was gathered under: a moved dependency,
|
|
276
|
+
* record date or access version makes it a statement about a superseded question, so it goes
|
|
277
|
+
* with the pages.
|
|
278
|
+
*/
|
|
279
|
+
resetBasis(entry, keepError) {
|
|
280
|
+
this.cancelWaiting(entry);
|
|
281
|
+
this.clearPages(entry, keepError);
|
|
282
|
+
entry.evidence = EMPTY_ITEMS;
|
|
283
|
+
entry.offeredKeys.clear();
|
|
284
|
+
entry.answered = false;
|
|
285
|
+
this.domainsCache = undefined;
|
|
286
|
+
}
|
|
287
|
+
/** Drops the painted pages and aborts the request that would replace them, evidence excepted. */
|
|
288
|
+
clearPages(entry, keepError) {
|
|
289
|
+
entry.signal?.abort();
|
|
290
|
+
entry.signal = undefined;
|
|
291
|
+
entry.items = EMPTY_ITEMS;
|
|
292
|
+
entry.loaded = false;
|
|
293
|
+
entry.loading = false;
|
|
294
|
+
if (!keepError)
|
|
295
|
+
entry.error = undefined;
|
|
296
|
+
entry.nextCursor = null;
|
|
297
|
+
this.invalidate(entry);
|
|
298
|
+
}
|
|
299
|
+
/** Stops a query waiting out the debounce; the text itself stays published. */
|
|
300
|
+
cancelWaiting(entry) {
|
|
301
|
+
entry.stopWaiting?.();
|
|
302
|
+
entry.stopWaiting = undefined;
|
|
303
|
+
}
|
|
304
|
+
load(entry, cursor) {
|
|
305
|
+
if (this.unmounted || entry.basis === undefined)
|
|
306
|
+
return;
|
|
307
|
+
const loader = Object.hasOwn(this.loaders, entry.basis.sourceId)
|
|
308
|
+
? this.loaders[entry.basis.sourceId]
|
|
309
|
+
: undefined;
|
|
310
|
+
if (loader === undefined) {
|
|
311
|
+
entry.loading = false;
|
|
312
|
+
entry.error = new Error(`no option loader is registered for source ${JSON.stringify(entry.basis.sourceId)}`);
|
|
313
|
+
this.invalidate(entry);
|
|
314
|
+
this.notify();
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
// `begin()` cancels the previous request for this binding before it hands out the next
|
|
318
|
+
// signal, so the signal alone answers "is this response stale?".
|
|
319
|
+
const signal = entry.port.begin();
|
|
320
|
+
entry.signal = signal;
|
|
321
|
+
entry.loading = true;
|
|
322
|
+
// The previous failure stays stated until this request settles: unmounting the alert and
|
|
323
|
+
// re-inserting the same text announces it again on a keystroke that changed no failure.
|
|
324
|
+
this.invalidate(entry);
|
|
325
|
+
this.notify();
|
|
326
|
+
const request = {
|
|
327
|
+
sourceId: entry.basis.sourceId,
|
|
328
|
+
fieldId: entry.fieldId,
|
|
329
|
+
...(entry.rowKey === undefined ? {} : { rowKey: entry.rowKey }),
|
|
330
|
+
dependsOn: entry.basis.dependsOn,
|
|
331
|
+
query: entry.query,
|
|
332
|
+
cursor,
|
|
333
|
+
pageSize: entry.pageSize,
|
|
334
|
+
signal,
|
|
335
|
+
};
|
|
336
|
+
// A loader that throws synchronously is a failure too, so it is started through the promise
|
|
337
|
+
// chain rather than beside it.
|
|
338
|
+
void Promise.resolve()
|
|
339
|
+
.then(() => (signal.aborted ? undefined : loader(request)))
|
|
340
|
+
.then((page) => {
|
|
341
|
+
if (signal.aborted || page === undefined)
|
|
342
|
+
return;
|
|
343
|
+
const checked = pageOf(page);
|
|
344
|
+
if (checked.kind === 'refused') {
|
|
345
|
+
entry.loading = false;
|
|
346
|
+
entry.error = new Error(checked.detail);
|
|
347
|
+
entry.nextCursor = null;
|
|
348
|
+
entry.signal = undefined;
|
|
349
|
+
this.invalidate(entry);
|
|
350
|
+
this.notify();
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
entry.loading = false;
|
|
354
|
+
entry.error = undefined;
|
|
355
|
+
entry.items =
|
|
356
|
+
cursor === null
|
|
357
|
+
? checked.page.items
|
|
358
|
+
: Object.freeze([...entry.items, ...checked.page.items]);
|
|
359
|
+
entry.loaded = true;
|
|
360
|
+
entry.answered = true;
|
|
361
|
+
entry.nextCursor = checked.page.nextCursor;
|
|
362
|
+
entry.signal = undefined;
|
|
363
|
+
this.offer(entry, checked.page.items);
|
|
364
|
+
// A page answers for the domain even when it offers nothing, and a resolved empty page
|
|
365
|
+
// is that answer: the map is rebuilt whether or not an item joined the evidence.
|
|
366
|
+
this.domainsCache = undefined;
|
|
367
|
+
this.invalidate(entry);
|
|
368
|
+
this.notify();
|
|
369
|
+
}, (cause) => {
|
|
370
|
+
// A rejected request on an aborted signal is the cancellation succeeding, not a
|
|
371
|
+
// failure the host must show or the console must report.
|
|
372
|
+
if (signal.aborted)
|
|
373
|
+
return;
|
|
374
|
+
entry.loading = false;
|
|
375
|
+
entry.error =
|
|
376
|
+
cause instanceof Error
|
|
377
|
+
? cause
|
|
378
|
+
: new Error('the option source rejected without an error', { cause });
|
|
379
|
+
entry.nextCursor = null;
|
|
380
|
+
entry.signal = undefined;
|
|
381
|
+
this.invalidate(entry);
|
|
382
|
+
this.notify();
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Adds what a page offered to the domain this basis states, a repeated code taking the answer
|
|
387
|
+
* the newest page gives.
|
|
388
|
+
*
|
|
389
|
+
* A page is a window on the source rather than the whole of it, so an item the reader has
|
|
390
|
+
* already seen keeps answering eligibility after a query stops painting it. Each value keeps
|
|
391
|
+
* the position it was first offered at, so the domain a control reads does not reorder itself
|
|
392
|
+
* as pages arrive.
|
|
393
|
+
*/
|
|
394
|
+
offer(entry, items) {
|
|
395
|
+
if (items.length === 0)
|
|
396
|
+
return;
|
|
397
|
+
const added = [];
|
|
398
|
+
const repeated = new Set();
|
|
399
|
+
for (const item of items) {
|
|
400
|
+
const key = canonicalizeRestricted(item.value);
|
|
401
|
+
// A code a later page repeats takes the newest answer; the domain keeps the position the
|
|
402
|
+
// value was first offered at.
|
|
403
|
+
if (entry.offeredKeys.has(key))
|
|
404
|
+
repeated.add(key);
|
|
405
|
+
else
|
|
406
|
+
added.push(item);
|
|
407
|
+
entry.offeredKeys.add(key);
|
|
408
|
+
}
|
|
409
|
+
if (added.length === items.length) {
|
|
410
|
+
entry.evidence = Object.freeze([...entry.evidence, ...added]);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const answer = new Map(items.map((item) => [canonicalizeRestricted(item.value), item]));
|
|
414
|
+
const replaced = entry.evidence.map((item) => {
|
|
415
|
+
const key = canonicalizeRestricted(item.value);
|
|
416
|
+
return repeated.has(key) ? (answer.get(key) ?? item) : item;
|
|
417
|
+
});
|
|
418
|
+
entry.evidence = Object.freeze([...replaced, ...added]);
|
|
419
|
+
}
|
|
420
|
+
invalidate(entry) {
|
|
421
|
+
entry.state = undefined;
|
|
422
|
+
}
|
|
423
|
+
notify() {
|
|
424
|
+
for (const listener of [...this.listeners])
|
|
425
|
+
listener();
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
function pageOf(value) {
|
|
429
|
+
if (!isRecord(value)) {
|
|
430
|
+
return { kind: 'refused', detail: 'an option loader must resolve to an option page' };
|
|
431
|
+
}
|
|
432
|
+
const items = value['items'];
|
|
433
|
+
const nextCursor = value['nextCursor'];
|
|
434
|
+
if (!Array.isArray(items)) {
|
|
435
|
+
return { kind: 'refused', detail: 'an option page must carry an items array' };
|
|
436
|
+
}
|
|
437
|
+
if (nextCursor !== null && typeof nextCursor !== 'string') {
|
|
438
|
+
return {
|
|
439
|
+
kind: 'refused',
|
|
440
|
+
detail: 'an option page states `nextCursor` as a string or null',
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
const checked = optionItemsOf(items);
|
|
444
|
+
if (checked.kind === 'refused')
|
|
445
|
+
return checked;
|
|
446
|
+
return { kind: 'ok', page: { items: checked.items, nextCursor } };
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* The page's items, as the host's loader typed them.
|
|
450
|
+
*
|
|
451
|
+
* The loader runs host code compiled against `OptionLoader`, so an item's `value` is already
|
|
452
|
+
* the field's wire type; what a JavaScript host can still get wrong is the page's shape, which
|
|
453
|
+
* the caller checks. The label key is checked here because a renderer would otherwise show
|
|
454
|
+
* `undefined` where a message key belongs.
|
|
455
|
+
*/
|
|
456
|
+
function optionItemsOf(items) {
|
|
457
|
+
const checked = [];
|
|
458
|
+
for (const raw of items) {
|
|
459
|
+
if (!isRecord(raw) || typeof raw['labelKey'] !== 'string' || !Object.hasOwn(raw, 'value')) {
|
|
460
|
+
return {
|
|
461
|
+
kind: 'refused',
|
|
462
|
+
detail: 'an option page item carries a `value` and a string `labelKey`',
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
const deprecated = raw['deprecated'];
|
|
466
|
+
if (deprecated !== undefined && typeof deprecated !== 'boolean') {
|
|
467
|
+
return {
|
|
468
|
+
kind: 'refused',
|
|
469
|
+
detail: 'an option item states `deprecated` as a boolean when it states it',
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
// A page answers availability for the codes it holds, `RULES-7.1.5`, so its answer and the
|
|
473
|
+
// sentence that explains it are part of the item rather than decoration: dropping them here
|
|
474
|
+
// would make the domain offer a code the host just said this record may not use.
|
|
475
|
+
const available = raw['available'];
|
|
476
|
+
if (available !== undefined && typeof available !== 'boolean') {
|
|
477
|
+
return {
|
|
478
|
+
kind: 'refused',
|
|
479
|
+
detail: 'an option item states `available` as a boolean when it states it',
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
const unavailableMessageKey = raw['unavailableMessageKey'];
|
|
483
|
+
if (unavailableMessageKey !== undefined && typeof unavailableMessageKey !== 'string') {
|
|
484
|
+
return {
|
|
485
|
+
kind: 'refused',
|
|
486
|
+
detail: 'an option item states `unavailableMessageKey` as a string when it states it',
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
const unavailableParams = optionParamsOf(raw['unavailableParams']);
|
|
490
|
+
if (unavailableParams.kind === 'refused') {
|
|
491
|
+
return {
|
|
492
|
+
kind: 'refused',
|
|
493
|
+
detail: 'an option item states `unavailableParams` as a record of wire values',
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
const value = copyOptionValue(raw['value']);
|
|
497
|
+
if (value === undefined) {
|
|
498
|
+
return {
|
|
499
|
+
kind: 'refused',
|
|
500
|
+
detail: 'an option item’s `value` is a wire value',
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
checked.push(Object.freeze({
|
|
504
|
+
value,
|
|
505
|
+
labelKey: raw['labelKey'],
|
|
506
|
+
...(deprecated === true ? { deprecated: true } : {}),
|
|
507
|
+
...(available === undefined ? {} : { available }),
|
|
508
|
+
...(unavailableMessageKey === undefined ? {} : { unavailableMessageKey }),
|
|
509
|
+
...(unavailableParams.kind === 'absent'
|
|
510
|
+
? {}
|
|
511
|
+
: { unavailableParams: unavailableParams.params }),
|
|
512
|
+
}));
|
|
513
|
+
}
|
|
514
|
+
return { kind: 'ok', items: Object.freeze(checked) };
|
|
515
|
+
}
|
|
516
|
+
/** Reads one option item's message arguments, or refuses a value that is not message payload. */
|
|
517
|
+
function optionParamsOf(raw) {
|
|
518
|
+
if (raw === undefined)
|
|
519
|
+
return { kind: 'absent' };
|
|
520
|
+
if (!isRecord(raw))
|
|
521
|
+
return { kind: 'refused' };
|
|
522
|
+
const params = {};
|
|
523
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
524
|
+
const copied = copyOptionValue(value);
|
|
525
|
+
if (copied === undefined)
|
|
526
|
+
return { kind: 'refused' };
|
|
527
|
+
setOwn(params, name, copied);
|
|
528
|
+
}
|
|
529
|
+
return { kind: 'params', params: Object.freeze(params) };
|
|
530
|
+
}
|
|
531
|
+
/** Stores a wire member without invoking the legacy `__proto__` setter on a plain object. */
|
|
532
|
+
function setOwn(target, key, value) {
|
|
533
|
+
Object.defineProperty(target, key, {
|
|
534
|
+
value,
|
|
535
|
+
enumerable: true,
|
|
536
|
+
configurable: true,
|
|
537
|
+
writable: true,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* A private copy of one option value, or `undefined` when it is not a wire value.
|
|
542
|
+
*
|
|
543
|
+
* The loader is host code and the store outlives the page it was handed, so keeping the
|
|
544
|
+
* caller's object would let a later mutation of their item change a domain the session has
|
|
545
|
+
* already derived. Every other host input crosses the wire reader; this copy is the same
|
|
546
|
+
* guarantee for a value whose field declaration the page machine does not receive. The copy
|
|
547
|
+
* is frozen, so the domain the store publishes cannot be edited in place either.
|
|
548
|
+
*/
|
|
549
|
+
function copyOptionValue(value) {
|
|
550
|
+
// The loader's answer is untrusted data. Share the runtime's profile walk so a cycle, a sparse
|
|
551
|
+
// list, an accessor or excessive depth is a page refusal rather than a recursive host failure.
|
|
552
|
+
if (!isWireValue(value))
|
|
553
|
+
return undefined;
|
|
554
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
555
|
+
return value;
|
|
556
|
+
if (typeof value === 'number') {
|
|
557
|
+
return Number.isSafeInteger(value) && !Object.is(value, -0) ? value : undefined;
|
|
558
|
+
}
|
|
559
|
+
if (Array.isArray(value)) {
|
|
560
|
+
const items = [];
|
|
561
|
+
for (const item of value) {
|
|
562
|
+
const copied = copyOptionValue(item);
|
|
563
|
+
if (copied === undefined)
|
|
564
|
+
return undefined;
|
|
565
|
+
items.push(copied);
|
|
566
|
+
}
|
|
567
|
+
return Object.freeze(items);
|
|
568
|
+
}
|
|
569
|
+
if (isRecord(value)) {
|
|
570
|
+
const copy = {};
|
|
571
|
+
for (const [key, member] of Object.entries(value)) {
|
|
572
|
+
const copied = copyOptionValue(member);
|
|
573
|
+
if (copied === undefined)
|
|
574
|
+
return undefined;
|
|
575
|
+
setOwn(copy, key, copied);
|
|
576
|
+
}
|
|
577
|
+
return Object.freeze(copy);
|
|
578
|
+
}
|
|
579
|
+
return undefined;
|
|
580
|
+
}
|
|
581
|
+
const COMPOSITE = new WeakMap();
|
|
582
|
+
export function asyncOptionListOf(state, controls, basisSignature, readable) {
|
|
583
|
+
const byControls = COMPOSITE.get(state) ?? new WeakMap();
|
|
584
|
+
const cached = byControls.get(controls);
|
|
585
|
+
if (cached !== undefined &&
|
|
586
|
+
cached.basisSignature === basisSignature &&
|
|
587
|
+
cached.readable === readable)
|
|
588
|
+
return cached;
|
|
589
|
+
const list = Object.freeze({ ...state, ...controls, basisSignature, readable });
|
|
590
|
+
byControls.set(controls, list);
|
|
591
|
+
COMPOSITE.set(state, byControls);
|
|
592
|
+
return list;
|
|
593
|
+
}
|
|
594
|
+
/** The binding key a page list is filed under; one row's pages never answer another's. */
|
|
595
|
+
export function optionPageKey(fieldId, rowKey) {
|
|
596
|
+
return bindingKey(fieldId, rowKey);
|
|
597
|
+
}
|
|
598
|
+
//# sourceMappingURL=option-pages.js.map
|