@gramlot/gramlot 0.2.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 +176 -0
- package/NOTICE +8 -0
- package/dist/gramlot.js +25616 -0
- package/dist/runtime-notices.json +34 -0
- package/package.json +47 -0
- package/src/adapters/file-host.js +93 -0
- package/src/adapters/host.js +128 -0
- package/src/adapters/index.js +6 -0
- package/src/adapters/page.js +32 -0
- package/src/adapters/resources.js +73 -0
- package/src/binding/inline.js +171 -0
- package/src/binding/installation.js +207 -0
- package/src/binding/logic.js +234 -0
- package/src/binding/providers.js +230 -0
- package/src/binding/router.js +190 -0
- package/src/binding/runtime.js +432 -0
- package/src/bootstrap.js +72 -0
- package/src/builder/binding.json +216 -0
- package/src/builder/gramlot-builder.js +181 -0
- package/src/builder/source.js +227 -0
- package/src/gramlot.js +151 -0
- package/src/index.js +9 -0
- package/src/references.js +43 -0
- package/src/renderer/attributes.js +120 -0
- package/src/renderer/gramlot-html-renderer.js +125 -0
- package/src/renderer/gramlot-renderer.js +618 -0
- package/src/renderer/gramlot-svg-renderer.js +53 -0
- package/src/transport.js +42 -0
- package/src/view/button.js +137 -0
- package/src/view/controls.js +469 -0
- package/src/view/events.js +64 -0
- package/src/view/html.js +154 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import {SourceBag} from '@genrojs/builders';
|
|
2
|
+
import {DataRouter} from './router.js';
|
|
3
|
+
import {DataInstaller} from './installation.js';
|
|
4
|
+
import {ControllerProvider, FormulaProvider} from './providers.js';
|
|
5
|
+
import {InlineCompiler} from './inline.js';
|
|
6
|
+
|
|
7
|
+
// The Provider class of each provider data-element (§4.8).
|
|
8
|
+
const PROVIDERS = {dataFormula: FormulaProvider, dataController: ControllerProvider};
|
|
9
|
+
|
|
10
|
+
// Attributes whose change moves the Data context of a whole branch (§4.4, upd_attrs row).
|
|
11
|
+
const CONTEXT_ATTRIBUTES = ['datapath', '_anchor', 'node_id', 'form', 'formId'];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Semantic lifetime of one Gramlot page (source plan §4.4).
|
|
15
|
+
*
|
|
16
|
+
* The runtime owns the one Data subscription of the page, on Builder's Data
|
|
17
|
+
* wrapper (`_dataroot` → `_root_` → `builder.data`), the DataRouter it feeds,
|
|
18
|
+
* the `node_id → node` map of the Source (R06) and one NodeBinding per Source
|
|
19
|
+
* node, created once per node identity. The DOM lifetime stays in the
|
|
20
|
+
* renderer records.
|
|
21
|
+
*/
|
|
22
|
+
export class BindingRuntime {
|
|
23
|
+
#gramlot;
|
|
24
|
+
#bindings = new Map();
|
|
25
|
+
#nodeIds = new Map();
|
|
26
|
+
#router;
|
|
27
|
+
#installer;
|
|
28
|
+
#inlineCompiler;
|
|
29
|
+
#subscription = `gramlot-data-${crypto.randomUUID()}`;
|
|
30
|
+
#disposed = false;
|
|
31
|
+
|
|
32
|
+
constructor(gramlot) {
|
|
33
|
+
this.#gramlot = gramlot;
|
|
34
|
+
this.#router = new DataRouter(this);
|
|
35
|
+
this.#installer = new DataInstaller(this);
|
|
36
|
+
this.#inlineCompiler = new InlineCompiler(this);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get gramlot() { return this.#gramlot; }
|
|
40
|
+
get builder() { return this.gramlot.builder; }
|
|
41
|
+
get renderer() { return this.gramlot.renderer; }
|
|
42
|
+
get logicRegistry() { return this.gramlot.logicRegistry; }
|
|
43
|
+
/** The document Data Bag. */
|
|
44
|
+
get data() { return this.builder.data; }
|
|
45
|
+
/** Builder's Data wrapper `_dataroot`: `root.getItem('_root_') === data`. */
|
|
46
|
+
get root() { return this.data.parent; }
|
|
47
|
+
/** Number of open NodeBindings. */
|
|
48
|
+
get size() { return this.#bindings.size; }
|
|
49
|
+
get disposed() { return this.#disposed; }
|
|
50
|
+
get router() { return this.#router; }
|
|
51
|
+
get installer() { return this.#installer; }
|
|
52
|
+
/** The compiler of the page's inline code (§4.10). */
|
|
53
|
+
get inlineCompiler() { return this.#inlineCompiler; }
|
|
54
|
+
/** `node_id → node` of the Source nodes with an open NodeBinding (R06). */
|
|
55
|
+
get nodeIds() { return this.#nodeIds; }
|
|
56
|
+
|
|
57
|
+
/** Subscribe once on Builder's Data wrapper; nothing is created. */
|
|
58
|
+
attach() {
|
|
59
|
+
this.root.subscribe(this.#subscription, {any: event => this.receiveData(event)});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Callback of the one Data subscription. */
|
|
63
|
+
receiveData(event) {
|
|
64
|
+
this.router.deliver(event);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The open NodeBinding of `node`, or null. */
|
|
68
|
+
bindingFor(node) {
|
|
69
|
+
return this.#bindings.get(node) ?? null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The NodeBinding of `node`, created once per node identity. */
|
|
73
|
+
openBinding(node) {
|
|
74
|
+
if (this.disposed) throw new Error('BindingRuntime is disposed');
|
|
75
|
+
let binding = this.bindingFor(node);
|
|
76
|
+
if (!binding) {
|
|
77
|
+
binding = new NodeBinding(this, node);
|
|
78
|
+
this.#bindings.set(node, binding);
|
|
79
|
+
}
|
|
80
|
+
return binding;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Close the NodeBinding of `node` and of all its descendants; errors are collected. */
|
|
84
|
+
closeBranch(node) {
|
|
85
|
+
const errors = [];
|
|
86
|
+
if (node.value instanceof SourceBag) {
|
|
87
|
+
for (const child of node.value.getNodes()) {
|
|
88
|
+
try { this.closeBranch(child); } catch (error) { errors.push(error); }
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const binding = this.bindingFor(node);
|
|
92
|
+
if (binding) {
|
|
93
|
+
this.#bindings.delete(node);
|
|
94
|
+
const nodeId = node.getAttr('node_id');
|
|
95
|
+
if (this.nodeIds.get(nodeId) === node) this.nodeIds.delete(nodeId);
|
|
96
|
+
try { binding.close(); } catch (error) { errors.push(error); }
|
|
97
|
+
}
|
|
98
|
+
if (errors.length) throw new AggregateError(errors, 'Source branch close failed');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Redo the registrations of `node` and of its descendants (change of Data context), then
|
|
103
|
+
* project the branch again: the elements already built follow the new paths.
|
|
104
|
+
*/
|
|
105
|
+
rebindBranch(node) {
|
|
106
|
+
const nodes = this.#branchNodes(node);
|
|
107
|
+
for (const each of nodes) this.bindingFor(each)?.rebind();
|
|
108
|
+
for (const each of nodes) this.renderer.project(each);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Semantic work of one Source event, called inside the renderer FIFO (§5.2).
|
|
113
|
+
*
|
|
114
|
+
* The rows of the event matrix: opening and closing of the NodeBindings,
|
|
115
|
+
* the `node_id` map, the rebinding, and the installation of the `ins` and
|
|
116
|
+
* new-branch rows (§5.1 steps 2-5).
|
|
117
|
+
*
|
|
118
|
+
* A failing disposer does not block the rest of the event: the errors of the
|
|
119
|
+
* closings are returned, so the renderer does its structural work before
|
|
120
|
+
* throwing them. Any other error is thrown, with the closing errors already
|
|
121
|
+
* collected.
|
|
122
|
+
*/
|
|
123
|
+
handleSourceEvent(event) {
|
|
124
|
+
const {evt, node} = event;
|
|
125
|
+
const errors = [];
|
|
126
|
+
const close = removed => {
|
|
127
|
+
try { this.closeBranch(removed); } catch (error) { errors.push(error); }
|
|
128
|
+
};
|
|
129
|
+
try {
|
|
130
|
+
if (evt === 'ins') {
|
|
131
|
+
this.#openBranch(node);
|
|
132
|
+
} else if (evt === 'del') {
|
|
133
|
+
for (const removed of Array.isArray(node) ? node : [node]) close(removed);
|
|
134
|
+
} else if (evt === 'upd_value' || evt === 'upd_value_attr') {
|
|
135
|
+
if (event.oldvalue instanceof SourceBag) for (const child of event.oldvalue.getNodes()) close(child);
|
|
136
|
+
// A `==` node value compiled for the old value is released (S09).
|
|
137
|
+
this.inlineCompiler.release(node, ['']);
|
|
138
|
+
// The pointer in the node value may have changed, appeared or disappeared (R05).
|
|
139
|
+
if (evt === 'upd_value_attr') this.#attributesChanged(node, event.attrs_diff);
|
|
140
|
+
else this.#installedBinding(node).rebind();
|
|
141
|
+
if (node.value instanceof SourceBag) this.#openBranch(node.value);
|
|
142
|
+
} else if (evt === 'upd_attrs') {
|
|
143
|
+
this.#attributesChanged(node, event.attrs_diff);
|
|
144
|
+
} else {
|
|
145
|
+
throw new Error(`Unsupported Source event: ${evt}`);
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (!errors.length) throw error;
|
|
149
|
+
throw new AggregateError([...errors, error], 'Source event failed');
|
|
150
|
+
}
|
|
151
|
+
return errors;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* §5.1 step 7, called by the renderer after a successful build of `node`: every node of the branch
|
|
156
|
+
* still in the Source and not built before is stamped `built`, the `_onBuilt` of its providers
|
|
157
|
+
* runs, then, when the page is started, the `_onStart` (step 8). A number is a delay in ms.
|
|
158
|
+
*/
|
|
159
|
+
branchBuilt(node) {
|
|
160
|
+
const built = [];
|
|
161
|
+
for (const each of this.#branchNodes(node)) {
|
|
162
|
+
const binding = this.bindingFor(each);
|
|
163
|
+
if (!binding || binding.hasStamp('built') || !this.renderer.attached(each)) continue;
|
|
164
|
+
binding.stamp('built');
|
|
165
|
+
built.push(binding);
|
|
166
|
+
}
|
|
167
|
+
// An `_onBuilt` may remove a later node of the list: each node is checked when its turn comes.
|
|
168
|
+
for (const binding of built) {
|
|
169
|
+
if (this.renderer.attached(binding.node)) this.#lifecycle(binding, 'built', '_onBuilt');
|
|
170
|
+
}
|
|
171
|
+
if (this.gramlot.state !== 'started') return;
|
|
172
|
+
for (const binding of built) {
|
|
173
|
+
if (this.renderer.attached(binding.node)) this.#start(binding);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** §5.1 step 8 for the initial Source, called by Gramlot when the page becomes `started`. */
|
|
178
|
+
pageStarted() {
|
|
179
|
+
for (const binding of [...this.#bindings.values()]) {
|
|
180
|
+
if (!binding.closed && binding.hasStamp('built')) this.#start(binding);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Close every NodeBinding, the `node_id` map and the Data subscription; a second call does nothing. */
|
|
185
|
+
dispose() {
|
|
186
|
+
if (this.disposed) return;
|
|
187
|
+
this.#disposed = true;
|
|
188
|
+
this.root.unsubscribe(this.#subscription, {any: true});
|
|
189
|
+
this.nodeIds.clear();
|
|
190
|
+
const errors = [];
|
|
191
|
+
for (const [node, binding] of [...this.#bindings.entries()].reverse()) {
|
|
192
|
+
this.#bindings.delete(node);
|
|
193
|
+
try { binding.close(); } catch (error) { errors.push(error); }
|
|
194
|
+
}
|
|
195
|
+
if (errors.length) throw new AggregateError(errors, 'Binding cleanup failed');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Steps 2-4 of §5.1 on `branch` (a SourceBagNode or a new SourceBag value). The `node_id` map is
|
|
200
|
+
* complete, and a duplicate refused, before the first write or registration, so a `#<node_id>`
|
|
201
|
+
* of the branch translates; then the installation (`dataSetter`s and defaults), then one
|
|
202
|
+
* NodeBinding per node and the registrations, providers included. On an error the NodeBindings
|
|
203
|
+
* opened here are closed and the map entries they brought are removed; the Data is not rolled
|
|
204
|
+
* back. Then `_init` (step 5), once per node: its error propagates and the NodeBindings stay
|
|
205
|
+
* open (P12, no rollback).
|
|
206
|
+
*/
|
|
207
|
+
#openBranch(branch) {
|
|
208
|
+
// A node already open came in with its ancestor, whose event was processed first (P4).
|
|
209
|
+
if (!(branch instanceof SourceBag) && this.bindingFor(branch)) return;
|
|
210
|
+
const nodes = branch instanceof SourceBag
|
|
211
|
+
? branch.getNodes().flatMap(root => this.#branchNodes(root)) : this.#branchNodes(branch);
|
|
212
|
+
const known = new Set(nodes.filter(node => this.bindingFor(node)));
|
|
213
|
+
const entering = new Map();
|
|
214
|
+
for (const node of nodes) {
|
|
215
|
+
const nodeId = node.getAttr('node_id');
|
|
216
|
+
if (nodeId == null) continue;
|
|
217
|
+
if (entering.has(nodeId) || (this.nodeIds.has(nodeId) && this.nodeIds.get(nodeId) !== node)) {
|
|
218
|
+
throw new Error(`Duplicate node_id '${nodeId}'`);
|
|
219
|
+
}
|
|
220
|
+
entering.set(nodeId, node);
|
|
221
|
+
}
|
|
222
|
+
for (const [nodeId, node] of entering) this.nodeIds.set(nodeId, node);
|
|
223
|
+
try {
|
|
224
|
+
this.installer.install(branch);
|
|
225
|
+
for (const node of nodes) this.openBinding(node);
|
|
226
|
+
for (const node of nodes) this.bindingFor(node).registerPointers();
|
|
227
|
+
} catch (error) {
|
|
228
|
+
const errors = [error];
|
|
229
|
+
for (const node of nodes.filter(node => !known.has(node)).reverse()) {
|
|
230
|
+
const nodeId = node.getAttr('node_id');
|
|
231
|
+
if (entering.get(nodeId) === node && this.nodeIds.get(nodeId) === node) this.nodeIds.delete(nodeId);
|
|
232
|
+
const binding = this.bindingFor(node);
|
|
233
|
+
if (!binding) continue;
|
|
234
|
+
this.#bindings.delete(node);
|
|
235
|
+
try { binding.close(); } catch (closeError) { errors.push(closeError); }
|
|
236
|
+
}
|
|
237
|
+
if (errors.length > 1) throw new AggregateError(errors, 'Source branch installation failed');
|
|
238
|
+
throw error;
|
|
239
|
+
}
|
|
240
|
+
// An `_init` may remove a later node of the branch: each node is checked when its turn comes.
|
|
241
|
+
for (const node of nodes) {
|
|
242
|
+
const binding = this.bindingFor(node);
|
|
243
|
+
if (!binding || binding.hasStamp('init') || !this.renderer.attached(node)) continue;
|
|
244
|
+
binding.stamp('init');
|
|
245
|
+
if (isOn(node.getAttr('_init'))) {
|
|
246
|
+
for (const provider of binding.providers) provider.invoke({kind: 'init', change: null});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The `_onStart` of a built node, once. */
|
|
252
|
+
#start(binding) {
|
|
253
|
+
if (binding.hasStamp('start')) return;
|
|
254
|
+
binding.stamp('start');
|
|
255
|
+
this.#lifecycle(binding, 'start', '_onStart');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Invoke the providers of `binding` for the lifecycle trigger `kind` when `attribute` is on; a number delays it. */
|
|
259
|
+
#lifecycle(binding, kind, attribute) {
|
|
260
|
+
const value = binding.node.getAttr(attribute);
|
|
261
|
+
if (!isOn(value)) return;
|
|
262
|
+
for (const provider of binding.providers) {
|
|
263
|
+
if (typeof value !== 'number' || value <= 0) {
|
|
264
|
+
provider.invoke({kind, change: null});
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
let release = null;
|
|
268
|
+
const timer = setTimeout(() => {
|
|
269
|
+
release();
|
|
270
|
+
provider.invoke({kind, change: null});
|
|
271
|
+
}, value);
|
|
272
|
+
release = binding.track(() => clearTimeout(timer));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* An attribute change: the NodeBinding of the node is required first (GC-210 §090), then the inline
|
|
278
|
+
* declarations of the changed attributes are released (S09), then the `node_id` map, then the
|
|
279
|
+
* rebinding of the node, or of its branch for a context attribute.
|
|
280
|
+
*/
|
|
281
|
+
#attributesChanged(node, diff) {
|
|
282
|
+
const binding = this.#installedBinding(node);
|
|
283
|
+
this.inlineCompiler.release(node, Object.keys(diff));
|
|
284
|
+
if (Object.hasOwn(diff, 'node_id')) {
|
|
285
|
+
const {old: previous, new: current} = diff.node_id;
|
|
286
|
+
if (current != null && this.nodeIds.has(current) && this.nodeIds.get(current) !== node) {
|
|
287
|
+
throw new Error(`Duplicate node_id '${current}'`);
|
|
288
|
+
}
|
|
289
|
+
if (this.nodeIds.get(previous) === node) this.nodeIds.delete(previous);
|
|
290
|
+
if (current != null) this.nodeIds.set(current, node);
|
|
291
|
+
}
|
|
292
|
+
if (CONTEXT_ATTRIBUTES.some(name => Object.hasOwn(diff, name))) this.rebindBranch(node);
|
|
293
|
+
else binding.rebind();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* The NodeBinding of a node that an update event reached; a node without one is a branch whose
|
|
298
|
+
* installation failed and that stayed in the Source: an error naming node and tag.
|
|
299
|
+
*/
|
|
300
|
+
#installedBinding(node) {
|
|
301
|
+
const binding = this.bindingFor(node);
|
|
302
|
+
if (!binding) {
|
|
303
|
+
throw new Error(`${node.nodeTag ?? 'fragment'} '${node.label}': the node has no NodeBinding `
|
|
304
|
+
+ '(its installation failed); remove it from the Source and insert it again');
|
|
305
|
+
}
|
|
306
|
+
return binding;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** `node` and its descendants, depth first. */
|
|
310
|
+
#branchNodes(node) {
|
|
311
|
+
const nodes = [node];
|
|
312
|
+
if (node.value instanceof SourceBag) {
|
|
313
|
+
for (const child of node.value.getNodes()) nodes.push(...this.#branchNodes(child));
|
|
314
|
+
}
|
|
315
|
+
return nodes;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** A lifecycle attribute is on when it is true or a number (P22: `0` counts as `true`). */
|
|
320
|
+
function isOn(value) {
|
|
321
|
+
return value === true || typeof value === 'number';
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Semantic owner of one Source node: the resources that live as long as the node is in the Source. */
|
|
325
|
+
export class NodeBinding {
|
|
326
|
+
#runtime;
|
|
327
|
+
#node;
|
|
328
|
+
#closed = false;
|
|
329
|
+
#disposers = [];
|
|
330
|
+
#registrations = [];
|
|
331
|
+
#stamps = new Set();
|
|
332
|
+
#provider = null;
|
|
333
|
+
// The clicks of a button, counted for its whole semantic life: a rebuild keeps them (P9, S12).
|
|
334
|
+
#clickCount = 0;
|
|
335
|
+
// The registration of the node's own variable datapath, read in the parent's context.
|
|
336
|
+
#datapathRegistration = null;
|
|
337
|
+
|
|
338
|
+
constructor(runtime, node) {
|
|
339
|
+
this.#runtime = runtime;
|
|
340
|
+
this.#node = node;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
get runtime() { return this.#runtime; }
|
|
344
|
+
get node() { return this.#node; }
|
|
345
|
+
get closed() { return this.#closed; }
|
|
346
|
+
/** The active DataRegistrations of the node. */
|
|
347
|
+
get registrations() { return [...this.#registrations]; }
|
|
348
|
+
/** The Provider of a `dataFormula` or `dataController`, as a list of zero or one. */
|
|
349
|
+
get providers() { return this.#provider ? [this.#provider] : []; }
|
|
350
|
+
|
|
351
|
+
/** The clicks counted by the node's ButtonBinding since the node entered the Source (P9). */
|
|
352
|
+
get clickCount() { return this.#clickCount; }
|
|
353
|
+
|
|
354
|
+
/** Count one click of the node's button and return the new count. */
|
|
355
|
+
countClick() {
|
|
356
|
+
return ++this.#clickCount;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Whether the lifecycle step `name` ('installed', 'init', 'built', 'start') ran for the node. */
|
|
360
|
+
hasStamp(name) {
|
|
361
|
+
return this.#stamps.has(name);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Record that the lifecycle step `name` ran for the node. */
|
|
365
|
+
stamp(name) {
|
|
366
|
+
this.#stamps.add(name);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Register the `^` pointers of `node.pointers()` with the router: `=` is not registered, a
|
|
371
|
+
* pointer on a null path (Q7) is not registered. The `datapath` pointer is resolved in the
|
|
372
|
+
* parent's context. A `dataSetter` registers nothing; a `dataFormula` or `dataController`
|
|
373
|
+
* registers its `datapath` here and the rest through its Provider, created once (§5.1 step 4).
|
|
374
|
+
*/
|
|
375
|
+
registerPointers() {
|
|
376
|
+
const node = this.node;
|
|
377
|
+
const dataElement = node._getMeta('data_element');
|
|
378
|
+
if (dataElement && !PROVIDERS[node.nodeTag]) return;
|
|
379
|
+
for (const [name, pointer] of node.pointers()) {
|
|
380
|
+
if (dataElement && name !== 'datapath') continue;
|
|
381
|
+
const context = name === 'datapath' ? node.parentNode : node;
|
|
382
|
+
const absolute = context.absDatapath(pointer);
|
|
383
|
+
if (absolute === null) continue;
|
|
384
|
+
const [path, attr = null] = absolute.split('?', 2);
|
|
385
|
+
const registration = this.runtime.router.register({path, attr, recipient: this});
|
|
386
|
+
this.#registrations.push(registration);
|
|
387
|
+
if (name === 'datapath') this.#datapathRegistration = registration;
|
|
388
|
+
}
|
|
389
|
+
if (!dataElement) return;
|
|
390
|
+
this.#provider ??= new PROVIDERS[node.nodeTag](this);
|
|
391
|
+
this.#provider.register();
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** Close the registrations and make them again from the current pointers, the provider's included (D1). */
|
|
395
|
+
rebind() {
|
|
396
|
+
this.#closeRegistrations();
|
|
397
|
+
this.registerPointers();
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Router recipient: the node's datapath rebinds its branch; any other registration projects the node. */
|
|
401
|
+
receive(change) {
|
|
402
|
+
if (change.registration === this.#datapathRegistration) this.runtime.rebindBranch(this.node);
|
|
403
|
+
else this.runtime.renderer.project(this.node, change);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** Own a semantic resource until close; the returned function releases it without running it. */
|
|
407
|
+
track(disposer) {
|
|
408
|
+
if (this.closed) throw new Error(`NodeBinding of '${this.node.label}' is closed`);
|
|
409
|
+
this.#disposers.push(disposer);
|
|
410
|
+
return () => {
|
|
411
|
+
const index = this.#disposers.indexOf(disposer);
|
|
412
|
+
if (index !== -1) this.#disposers.splice(index, 1);
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** Close the registrations, then run the disposers in reverse order and collect their errors. */
|
|
417
|
+
close() {
|
|
418
|
+
if (this.closed) return;
|
|
419
|
+
this.#closed = true;
|
|
420
|
+
this.#closeRegistrations();
|
|
421
|
+
const errors = [];
|
|
422
|
+
for (const disposer of this.#disposers.splice(0).reverse()) {
|
|
423
|
+
try { disposer(); } catch (error) { errors.push(error); }
|
|
424
|
+
}
|
|
425
|
+
if (errors.length) throw new AggregateError(errors, `NodeBinding of '${this.node.label}' cleanup failed`);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
#closeRegistrations() {
|
|
429
|
+
for (const registration of this.#registrations.splice(0)) registration.close();
|
|
430
|
+
this.#datapathRegistration = null;
|
|
431
|
+
}
|
|
432
|
+
}
|
package/src/bootstrap.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {Gramlot} from './gramlot.js';
|
|
2
|
+
import {LogicRegistry} from './binding/logic.js';
|
|
3
|
+
import {MainTransport} from './transport.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The browser start of one page (source plan §4.12). The host writes
|
|
7
|
+
* `await new PageBootstrap({config, resources}).run()` into the bootstrap HTML;
|
|
8
|
+
* `resources` is `{css: [url], js: [{url, group}]}`, already in load order and
|
|
9
|
+
* with the mount prefix.
|
|
10
|
+
*/
|
|
11
|
+
export class PageBootstrap {
|
|
12
|
+
constructor({config, resources, document = globalThis.document}) {
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.resources = resources;
|
|
15
|
+
this.document = document;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* CSS links, all JS imports awaited, the Logic check (2bis), `new Gramlot`,
|
|
20
|
+
* logic registration in received order, `start()`. Resolves with the app, or
|
|
21
|
+
* with null when the page closed during the imports. A failed import or check
|
|
22
|
+
* rejects naming the resource; the CSS links stay and the server page is closed.
|
|
23
|
+
*/
|
|
24
|
+
async run() {
|
|
25
|
+
const {config, resources, document} = this;
|
|
26
|
+
const window = document.defaultView;
|
|
27
|
+
for (const url of resources.css) {
|
|
28
|
+
const link = document.createElement('link');
|
|
29
|
+
link.rel = 'stylesheet';
|
|
30
|
+
link.href = url;
|
|
31
|
+
document.head.append(link);
|
|
32
|
+
}
|
|
33
|
+
let closed = false;
|
|
34
|
+
const pagehide = event => {
|
|
35
|
+
if (event.persisted || closed) return;
|
|
36
|
+
closed = true;
|
|
37
|
+
this.closePage();
|
|
38
|
+
};
|
|
39
|
+
window.addEventListener('pagehide', pagehide);
|
|
40
|
+
let entries;
|
|
41
|
+
try {
|
|
42
|
+
// A relative module URL is resolved against the document, as in HTML (D8).
|
|
43
|
+
const settled = await Promise.allSettled(resources.js.map(({url}) => import(new URL(url, document.baseURI).href)));
|
|
44
|
+
if (closed) return null;
|
|
45
|
+
entries = resources.js.map(({url, group}, index) => {
|
|
46
|
+
const outcome = settled[index];
|
|
47
|
+
if (outcome.status === 'rejected') {
|
|
48
|
+
throw new Error(`${url}: import failed: ${outcome.reason?.message ?? outcome.reason}`, {cause: outcome.reason});
|
|
49
|
+
}
|
|
50
|
+
return {logicClass: outcome.value.Logic, group, resource: url};
|
|
51
|
+
});
|
|
52
|
+
LogicRegistry.check(entries);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
this.closePage();
|
|
55
|
+
throw error;
|
|
56
|
+
} finally {
|
|
57
|
+
window.removeEventListener('pagehide', pagehide);
|
|
58
|
+
}
|
|
59
|
+
const app = new Gramlot({...config, document});
|
|
60
|
+
for (const {logicClass, group, resource} of entries) app.logicRegistry.register(logicClass, {group, resource});
|
|
61
|
+
window.gramlot = app;
|
|
62
|
+
await app.start();
|
|
63
|
+
return app;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The close request of a page that never got its Gramlot instance: a beacon, as `Gramlot` sends on pagehide. */
|
|
67
|
+
closePage() {
|
|
68
|
+
const {mainUrl, sourceUrl, closeUrl, pageId} = this.config;
|
|
69
|
+
new MainTransport(mainUrl, undefined, sourceUrl, closeUrl, this.document.defaultView.navigator)
|
|
70
|
+
.close(pageId, {beacon: true});
|
|
71
|
+
}
|
|
72
|
+
}
|