@gramlot/gramlot 0.2.8 → 0.2.10

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/src/gramlot.d.ts CHANGED
@@ -3,14 +3,13 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- import type {DomElement, DomDocument, DomNode} from './dom.d.ts';
6
+ import type {DomElement, DomDocument} from './dom.d.ts';
7
7
  import type {Bag} from '@genrojs/bag';
8
- import type {SourceBag, SourceBagNode} from '@genrojs/builders';
9
- import type {GramlotBuilder} from './builder/gramlot-builder.js';
10
- import type {GramlotRenderer} from './renderer/gramlot-renderer.js';
11
- import type {InOut} from './inout.js';
12
- import type {SourceReference} from './references.js';
13
8
  import type {MainTransport} from './transport.js';
9
+ import type {SourceHandler} from './handlers/source-handler.js';
10
+ import type {RpcHandler} from './handlers/rpc-handler.js';
11
+ import type {DomHandler} from './handlers/dom-handler.js';
12
+ import type {UtilitiesHandler} from './handlers/utilities-handler.js';
14
13
 
15
14
  /** The options of a `Gramlot` instance. */
16
15
  export interface GramlotOptions {
@@ -37,66 +36,38 @@ export interface GramlotOptions {
37
36
  /** The lifecycle state of a `Gramlot` instance. */
38
37
  export type GramlotState = 'ready' | 'loading' | 'started' | 'failed' | 'disposed';
39
38
 
40
- /**
41
- * One Gramlot page. The Data root is the builder's, the binding runtime subscribes to it before
42
- * the renderer observes the Source.
43
- */
39
+ /** One Gramlot page: lifecycle, Data and the four proxies `src`, `rpc`, `dom`, `utl`. */
44
40
  export class Gramlot {
45
41
  /** The id of the server page. */
46
42
  pageId: string | undefined;
47
- /** The registry of the Logic classes of the page. */
48
- logicRegistry: unknown;
49
- /** The builder that owns the Source and the Data. */
50
- builder: GramlotBuilder;
51
- /** The binding runtime of the page. */
52
- binding: unknown;
43
+ /** The root group of the named logic of the page. */
44
+ logic: unknown;
45
+ /** The Source and every change of it. */
46
+ src: SourceHandler;
53
47
  /** The Data Bag of the page. */
54
48
  data: Bag;
55
- /** The `gramlot.inout` object: send, receive, save and download Data branches. */
56
- inout: InOut;
57
- /** The Source Bag observed by the renderer. */
58
- source: SourceBag;
59
- /** The live renderer of the page. */
60
- renderer: GramlotRenderer;
61
- /** The server transport, or null. */
62
- transport: MainTransport | null;
63
- /** The remote Source requests in progress, by target node. */
64
- remoteRequests: Map<SourceBagNode, {controller: AbortController}>;
49
+ /** The exchange with the host. */
50
+ rpc: RpcHandler;
51
+ /** The DOM and its correspondence with the Source. */
52
+ dom: DomHandler;
53
+ /** Page utilities grouped by theme. */
54
+ utl: UtilitiesHandler;
65
55
  /** The lifecycle state. */
66
56
  state: GramlotState;
67
57
  /** The controller aborted on dispose. */
68
58
  abort: AbortController;
69
59
  /** The pending `start` promise, while loading. */
70
60
  loading?: Promise<Gramlot> | null;
61
+ /** The window whose `pagehide` disposes the page; set only with the default `MainTransport`. */
62
+ window?: Window;
63
+ /** The `pagehide` listener that disposes the page with a beacon; set only with the default `MainTransport`. */
64
+ pagehide?: (event: PageTransitionEvent) => void;
71
65
  /** Create the page instance; the construction order is the one of the source plan. */
72
66
  constructor(options?: GramlotOptions);
73
67
  /** Load `main` from the server and mount it; resolves with the instance. */
74
68
  start(): Promise<Gramlot>;
75
69
  /** Request `main` from the transport and mount it. */
76
70
  loadMain(): Promise<Gramlot>;
77
- /** Start from an embedded typed Source, with the same observed insertion as `main`. */
78
- startSource(wire: SourceBag | string): Gramlot;
79
- /** Validate and insert `main` in the Source. */
80
- mountMainSource(wire: SourceBag | string): Gramlot;
81
- /**
82
- * A Source from a SourceBag or a TYTX wire; only Gramlot Source Bags are accepted, before any
83
- * effect. The received Source activates its inline code.
84
- */
85
- prepareSource(wire: SourceBag | string, builder?: GramlotBuilder): SourceBag;
86
- /**
87
- * Replace the children of one Source node with a remote Source, only after the incoming Source
88
- * validates. Resolves with true when applied, false when superseded or cancelled.
89
- *
90
- * Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
91
- * they arrive together with the `remote` grammar attribute and `@endpoint`.
92
- */
93
- remoteSource(target: SourceBagNode, method: string, params?: Record<string, unknown>): Promise<boolean>;
94
- /** Resolve a node or DOM reference descriptor. */
95
- reference(descriptor: SourceReference): SourceBagNode | DomElement;
96
- /** The Source node of the nearest element generated by the renderer. */
97
- getBaseSourceNode(domNode: DomNode): SourceBagNode | null;
98
- /** The element built from a Source node, or null. */
99
- getDomNode(sourceNode: SourceBagNode): DomElement | null;
100
71
  /** Dispose the page: abort requests, close bindings and the renderer, and close the server page. */
101
72
  dispose(options?: {beacon?: boolean}): void;
102
73
  }
package/src/gramlot.js CHANGED
@@ -4,40 +4,28 @@
4
4
  *
5
5
  * @module
6
6
  */
7
- import {SourceBag, sourceBagFromTytx} from '@genrojs/builders';
8
- import {GramlotBuilder} from './builder/gramlot-builder.js';
9
- import {GramlotBuilderBag} from './builder/source.js';
10
- import {GramlotRenderer} from './renderer/gramlot-renderer.js';
11
- import {BindingRuntime} from './binding/runtime.js';
12
- import {LogicRegistry} from './binding/logic.js';
13
7
  import {MainTransport} from './transport.js';
14
- import {InOut} from './inout.js';
8
+ import {SourceHandler} from './handlers/source-handler.js';
9
+ import {RpcHandler} from './handlers/rpc-handler.js';
10
+ import {DomHandler} from './handlers/dom-handler.js';
11
+ import {UtilitiesHandler} from './handlers/utilities-handler.js';
15
12
 
16
13
  /**
17
- * One Gramlot page. The construction order is the one of source plan §4.3: the Data
18
- * root is Builder's (`_dataroot` → `_root_` → `builder.data`) and the BindingRuntime
19
- * subscribes to it before the renderer observes the Source.
14
+ * One Gramlot page: lifecycle, Data and the four proxies `src`, `rpc`, `dom`, `utl`.
15
+ * `SourceHandler` creates the builder, the binding runtime and the renderer in the order of source plan §4.3.
20
16
  */
21
17
  export class Gramlot {
22
18
  constructor({pageId, mainUrl = '/gramlot/main', sourceUrl = '/gramlot/source', closeUrl = '/gramlot/close', rootId = 'gramlot-root',
23
19
  element = null, document = globalThis.document, transport = null, collections = []} = {}) {
24
20
  this.pageId = pageId;
25
- this.logicRegistry = new LogicRegistry(this);
26
- this.builder = new GramlotBuilder(null, {collections});
27
- this.binding = new BindingRuntime(this);
28
- this.builder.binding = this.binding;
29
- this.data = this.builder.data;
30
- this.inout = new InOut(this, document);
31
- this.binding.attach();
32
- this.source = this.builder.source;
33
- const destination = element ?? document.getElementById(rootId);
34
- this.renderer = new GramlotRenderer(this.builder, this.source, destination, {binding: this.binding});
35
- this.transport = transport === false ? null : transport ??
36
- new MainTransport(mainUrl, undefined, sourceUrl, closeUrl, document.defaultView.navigator);
37
- this.remoteRequests = new Map();
21
+ this.src = new SourceHandler(this, {collections, destination: element ?? document.getElementById(rootId)});
22
+ this.data = this.src.builder.data;
23
+ this.rpc = new RpcHandler(this, {mainUrl, sourceUrl, closeUrl, document, transport});
24
+ this.dom = new DomHandler(this);
25
+ this.utl = new UtilitiesHandler(this, document);
38
26
  this.state = 'ready';
39
27
  this.abort = new AbortController();
40
- if (this.transport instanceof MainTransport) {
28
+ if (this.rpc.transport instanceof MainTransport) {
41
29
  this.pagehide = event => { if (!event.persisted) this.dispose({beacon: true}); };
42
30
  document.defaultView.addEventListener('pagehide', this.pagehide);
43
31
  this.window = document.defaultView;
@@ -45,7 +33,7 @@ export class Gramlot {
45
33
  }
46
34
  start() {
47
35
  if (this.state === 'disposed') return Promise.reject(new Error('Gramlot is disposed'));
48
- if (!this.transport) return Promise.reject(new Error('This Gramlot instance has no server transport'));
36
+ if (!this.rpc.transport) return Promise.reject(new Error('This Gramlot instance has no server transport'));
49
37
  if (this.loading) return this.loading;
50
38
  if (this.state === 'started') return Promise.resolve(this);
51
39
  this.state = 'loading';
@@ -54,113 +42,27 @@ export class Gramlot {
54
42
  }
55
43
  async loadMain() {
56
44
  try {
57
- const wire = await this.transport.main(this.pageId, this.abort.signal);
45
+ const wire = await this.rpc.transport.main(this.pageId, this.abort.signal);
58
46
  if (this.state === 'disposed') throw new Error('Gramlot was disposed during main');
59
- return this.mountMainSource(wire);
47
+ return this.src.mountMainSource(wire);
60
48
  } catch (error) {
61
49
  if (this.state !== 'disposed') this.state = 'failed';
62
50
  throw error;
63
51
  }
64
52
  }
65
- /** Start from an embedded typed Source using the same observed insertion as main. */
66
- startSource(wire) {
67
- if (!['ready', 'failed'].includes(this.state)) {
68
- throw new Error(`Cannot start embedded Source while Gramlot is ${this.state}`);
69
- }
70
- try { return this.mountMainSource(wire); }
71
- catch (error) { this.state = 'failed'; throw error; }
72
- }
73
- /**
74
- * Validate and insert `main` (§5.1). When its installation fails after the insertion (its
75
- * NodeBindings closed again), `main` leaves the Source, so `start` or `startSource` can mount a
76
- * new one; the Data is not rolled back. A later error (`_init`, DOM) keeps `main` and its
77
- * NodeBindings (P12, no rollback).
78
- */
79
- mountMainSource(wire) {
80
- const source = this.prepareSource(wire);
81
- this.renderer.validateCandidate(source);
82
- try {
83
- this.source.setItem('main', source);
84
- } catch (error) {
85
- if (!this.binding.bindingFor(this.source.getNode('main'))) this.source.popNode('main');
86
- throw error;
87
- }
88
- this.state = 'started';
89
- this.binding.pageStarted();
90
- return this;
91
- }
92
- /**
93
- * A Source from a SourceBag or a TYTX wire; only GramlotBuilderBag is accepted, before any effect (R07).
94
- * The received Source activates its inline code: the only inline code the page runs. Activation is
95
- * here and not in the TYTX decoding, because a GramlotBuilderBag can also arrive inside Data.
96
- */
97
- prepareSource(wire, builder = this.builder) {
98
- if (wire instanceof SourceBag) {
99
- if (!(wire instanceof GramlotBuilderBag)) throw new TypeError('Source values must be GramlotBuilderBags');
100
- return wire.bindBuilder(builder).activateCode();
101
- }
102
- const source = sourceBagFromTytx(wire, builder);
103
- if (!(source instanceof GramlotBuilderBag)) throw new TypeError('Source values must be GramlotBuilderBags');
104
- return source.activateCode();
105
- }
106
-
107
- /**
108
- * Replace one Source node's children only after incoming Source validation.
109
- * The request lives as long as the target's NodeBinding (P24): a DOM rebuild
110
- * or a freeze keeps it, the removal of the target cancels it.
111
- *
112
- * Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
113
- * they arrive together with the `remote` grammar attribute and `@endpoint`.
114
- */
115
- async remoteSource(target, method, params = {}) {
116
- if (!this.transport?.source) throw new Error('Remote Source is unavailable without a server transport');
117
- const binding = this.binding.bindingFor(target);
118
- if (this.state !== 'started' || !binding) throw new Error('Remote Source target is not mounted');
119
- this.remoteRequests.get(target)?.controller.abort();
120
- const request = {controller: new AbortController()};
121
- this.remoteRequests.set(target, request);
122
- const unregister = binding.track(() => request.controller.abort());
123
- let applying = false;
124
- const current = () => this.state !== 'disposed' && !request.controller.signal.aborted &&
125
- this.remoteRequests.get(target) === request && !binding.closed;
126
- try {
127
- const wire = await this.transport.source(this.pageId, method, params, request.controller.signal);
128
- if (!current()) return false;
129
- const prepared = this.prepareSource(wire, target.builder);
130
- if (!current()) return false;
131
- this.renderer.validateCandidate(prepared, {replacingNode: target});
132
- applying = true;
133
- unregister();
134
- // The target's own prefix text belongs to the old body as well.
135
- target.setValue(prepared, true, {_text: null}, true);
136
- return true;
137
- } catch (error) {
138
- if (!applying && !current()) return false;
139
- throw error;
140
- } finally {
141
- unregister();
142
- if (this.remoteRequests.get(target) === request) this.remoteRequests.delete(target);
143
- }
144
- }
145
-
146
- reference(descriptor) { return this.renderer.references.resolve(descriptor); }
147
- /** The Source node of the nearest element generated by the renderer (§4.11bis). */
148
- getBaseSourceNode(domNode) { return this.renderer.getBaseSourceNode(domNode); }
149
- /** The element built from a Source node, or null (§4.11bis). */
150
- getDomNode(sourceNode) { return this.renderer.getDomNode(sourceNode); }
151
53
  dispose({beacon = false} = {}) {
152
54
  if (this.state === 'disposed') return;
153
55
  this.state = 'disposed';
154
56
  if (this.pagehide) this.window.removeEventListener('pagehide', this.pagehide);
155
57
  this.abort.abort();
156
- for (const request of this.remoteRequests.values()) request.controller.abort();
157
- this.remoteRequests.clear();
58
+ for (const request of this.rpc.remoteRequests.values()) request.controller.abort();
59
+ this.rpc.remoteRequests.clear();
158
60
  try {
159
- try { this.binding.dispose(); }
160
- finally { this.renderer.dispose(); }
61
+ try { this.src.binding.dispose(); }
62
+ finally { this.src.renderer.dispose(); }
161
63
  } finally {
162
- if (this.transport instanceof MainTransport) this.transport.close(this.pageId, {beacon});
163
- this.transport?.dispose?.();
64
+ if (this.rpc.transport instanceof MainTransport) this.rpc.transport.close(this.pageId, {beacon});
65
+ this.rpc.transport?.dispose?.();
164
66
  }
165
67
  }
166
68
  }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The DOM of one Gramlot page and its correspondence with the Source (`gramlot.dom`).
3
+ *
4
+ * @module
5
+ */
6
+ import type {SourceBagNode} from '@genrojs/builders';
7
+ import type {DomElement, DomNode} from '../dom.d.ts';
8
+ import type {SourceReference} from '../references.js';
9
+ import {Handler} from './handler.js';
10
+
11
+ /** The DOM and its correspondence with the Source. */
12
+ export class DomHandler extends Handler {
13
+ /** Resolve a node or DOM reference descriptor. */
14
+ reference(descriptor: SourceReference): SourceBagNode | DomElement;
15
+ /** The Source node of the nearest element generated by the renderer. */
16
+ getBaseSourceNode(domNode: DomNode): SourceBagNode | null;
17
+ /** The element built from a Source node, or null. */
18
+ getDomNode(sourceNode: SourceBagNode): DomElement | null;
19
+ }
@@ -0,0 +1,16 @@
1
+ /* @ts-self-types="./dom-handler.d.ts" */
2
+ /**
3
+ * The DOM of one Gramlot page and its correspondence with the Source (`gramlot.dom`).
4
+ *
5
+ * @module
6
+ */
7
+ import {Handler} from './handler.js';
8
+
9
+ /** The DOM and its correspondence with the Source. */
10
+ export class DomHandler extends Handler {
11
+ reference(descriptor) { return this.gramlot.src.renderer.references.resolve(descriptor); }
12
+ /** The Source node of the nearest element generated by the renderer (§4.11bis). */
13
+ getBaseSourceNode(domNode) { return this.gramlot.src.renderer.getBaseSourceNode(domNode); }
14
+ /** The element built from a Source node, or null (§4.11bis). */
15
+ getDomNode(sourceNode) { return this.gramlot.src.renderer.getDomNode(sourceNode); }
16
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The base class of the handlers of a `Gramlot` instance.
3
+ *
4
+ * @module
5
+ */
6
+ import type {Gramlot} from '../gramlot.js';
7
+
8
+ /** A themed proxy of one `Gramlot` instance, holding the reference to it. */
9
+ export class Handler {
10
+ /** The page this handler belongs to. */
11
+ gramlot: Gramlot;
12
+ /** Create the handler of `gramlot`. */
13
+ constructor(gramlot: Gramlot);
14
+ }
@@ -0,0 +1,13 @@
1
+ /* @ts-self-types="./handler.d.ts" */
2
+ /**
3
+ * The base class of the handlers of a `Gramlot` instance.
4
+ *
5
+ * @module
6
+ */
7
+
8
+ /** A themed proxy of one `Gramlot` instance, holding the reference to it. */
9
+ export class Handler {
10
+ constructor(gramlot) {
11
+ this.gramlot = gramlot;
12
+ }
13
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * The exchange of one Gramlot page with the host (`gramlot.rpc`).
3
+ *
4
+ * @module
5
+ */
6
+ import type {SourceBagNode} from '@genrojs/builders';
7
+ import type {DomDocument} from '../dom.d.ts';
8
+ import type {Gramlot} from '../gramlot.js';
9
+ import type {MainTransport} from '../transport.js';
10
+ import {Handler} from './handler.js';
11
+
12
+ /** The exchange with the host. */
13
+ export class RpcHandler extends Handler {
14
+ /** The server transport, or null. */
15
+ transport: MainTransport | null;
16
+ /** The remote Source requests in progress, by target node. */
17
+ remoteRequests: Map<SourceBagNode, {controller: AbortController}>;
18
+ /** Create the transport of `gramlot`: the given one, none for `false`, else a `MainTransport` on the URLs. */
19
+ constructor(gramlot: Gramlot, options: {mainUrl: string; sourceUrl: string; closeUrl: string; document: DomDocument;
20
+ transport?: MainTransport | false | null});
21
+ }
@@ -0,0 +1,18 @@
1
+ /* @ts-self-types="./rpc-handler.d.ts" */
2
+ /**
3
+ * The exchange of one Gramlot page with the host (`gramlot.rpc`).
4
+ *
5
+ * @module
6
+ */
7
+ import {Handler} from './handler.js';
8
+ import {MainTransport} from '../transport.js';
9
+
10
+ /** The exchange with the host. */
11
+ export class RpcHandler extends Handler {
12
+ constructor(gramlot, {mainUrl, sourceUrl, closeUrl, document, transport}) {
13
+ super(gramlot);
14
+ this.transport = transport === false ? null : transport ??
15
+ new MainTransport(mainUrl, undefined, sourceUrl, closeUrl, document.defaultView.navigator);
16
+ this.remoteRequests = new Map();
17
+ }
18
+ }
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The Source of one Gramlot page and every change of it (`gramlot.src`).
3
+ *
4
+ * @module
5
+ */
6
+ import type {SourceBag, SourceBagNode} from '@genrojs/builders';
7
+ import type {DomElement} from '../dom.d.ts';
8
+ import type {Gramlot} from '../gramlot.js';
9
+ import type {GramlotBuilder} from '../builder/gramlot-builder.js';
10
+ import type {GramlotRenderer} from '../renderer/gramlot-renderer.js';
11
+ import {Handler} from './handler.js';
12
+
13
+ /**
14
+ * The Source and every change of it. The Data root is the builder's, the binding runtime subscribes
15
+ * to it before the renderer observes the Source.
16
+ */
17
+ export class SourceHandler extends Handler {
18
+ /** The registry of the Logic classes of the page. */
19
+ logicRegistry: unknown;
20
+ /** The builder that owns the Source and the Data. */
21
+ builder: GramlotBuilder;
22
+ /** The binding runtime of the page. */
23
+ binding: unknown;
24
+ /** The Source Bag observed by the renderer. */
25
+ source: SourceBag;
26
+ /** The live renderer of the page. */
27
+ renderer: GramlotRenderer;
28
+ /** Create the builder, the binding runtime and the renderer of `gramlot`, in the order of the source plan. */
29
+ constructor(gramlot: Gramlot, options: {collections: unknown[]; destination: DomElement | null});
30
+ /** Start from an embedded typed Source, with the same observed insertion as `main`. */
31
+ startSource(wire: SourceBag | string): Gramlot;
32
+ /** Validate and insert `main` in the Source. */
33
+ mountMainSource(wire: SourceBag | string): Gramlot;
34
+ /**
35
+ * A Source from a SourceBag or a TYTX wire; only Gramlot Source Bags are accepted, before any
36
+ * effect. The received Source activates its inline code.
37
+ */
38
+ prepareSource(wire: SourceBag | string, builder?: GramlotBuilder): SourceBag;
39
+ /**
40
+ * Replace the children of one Source node with a remote Source, only after the incoming Source
41
+ * validates. Resolves with true when applied, false when superseded or cancelled.
42
+ *
43
+ * Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
44
+ * they arrive together with the `remote` grammar attribute and `@endpoint`.
45
+ */
46
+ remoteSource(target: SourceBagNode, method: string, params?: Record<string, unknown>): Promise<boolean>;
47
+ }
@@ -0,0 +1,114 @@
1
+ /* @ts-self-types="./source-handler.d.ts" */
2
+ /**
3
+ * The Source of one Gramlot page and every change of it (`gramlot.src`).
4
+ *
5
+ * @module
6
+ */
7
+ import {SourceBag, sourceBagFromTytx} from '@genrojs/builders';
8
+ import {Handler} from './handler.js';
9
+ import {GramlotBuilder} from '../builder/gramlot-builder.js';
10
+ import {GramlotBuilderBag} from '../builder/source.js';
11
+ import {GramlotRenderer} from '../renderer/gramlot-renderer.js';
12
+ import {BindingRuntime} from '../binding/runtime.js';
13
+ import {LogicRegistry} from '../binding/logic.js';
14
+
15
+ /**
16
+ * The Source and every change of it. The construction order is the one of source plan §4.3: the Data
17
+ * root is Builder's (`_dataroot` → `_root_` → `builder.data`) and the BindingRuntime subscribes to it
18
+ * before the renderer observes the Source.
19
+ */
20
+ export class SourceHandler extends Handler {
21
+ constructor(gramlot, {collections, destination}) {
22
+ super(gramlot);
23
+ // The BindingRuntime reaches the builder through gramlot.src while it attaches.
24
+ gramlot.src = this;
25
+ this.logicRegistry = new LogicRegistry(gramlot);
26
+ this.builder = new GramlotBuilder(null, {collections});
27
+ this.binding = new BindingRuntime(gramlot);
28
+ this.builder.binding = this.binding;
29
+ this.binding.attach();
30
+ this.source = this.builder.source;
31
+ this.renderer = new GramlotRenderer(this.builder, this.source, destination, {binding: this.binding});
32
+ }
33
+ /** Start from an embedded typed Source using the same observed insertion as main. */
34
+ startSource(wire) {
35
+ if (!['ready', 'failed'].includes(this.gramlot.state)) {
36
+ throw new Error(`Cannot start embedded Source while Gramlot is ${this.gramlot.state}`);
37
+ }
38
+ try { return this.mountMainSource(wire); }
39
+ catch (error) { this.gramlot.state = 'failed'; throw error; }
40
+ }
41
+ /**
42
+ * Validate and insert `main` (§5.1). When its installation fails after the insertion (its
43
+ * NodeBindings closed again), `main` leaves the Source, so `start` or `startSource` can mount a
44
+ * new one; the Data is not rolled back. A later error (`_init`, DOM) keeps `main` and its
45
+ * NodeBindings (P12, no rollback).
46
+ */
47
+ mountMainSource(wire) {
48
+ const source = this.prepareSource(wire);
49
+ this.renderer.validateCandidate(source);
50
+ try {
51
+ this.source.setItem('main', source);
52
+ } catch (error) {
53
+ if (!this.binding.bindingFor(this.source.getNode('main'))) this.source.popNode('main');
54
+ throw error;
55
+ }
56
+ this.gramlot.state = 'started';
57
+ this.binding.pageStarted();
58
+ return this.gramlot;
59
+ }
60
+ /**
61
+ * A Source from a SourceBag or a TYTX wire; only GramlotBuilderBag is accepted, before any effect (R07).
62
+ * The received Source activates its inline code: the only inline code the page runs. Activation is
63
+ * here and not in the TYTX decoding, because a GramlotBuilderBag can also arrive inside Data.
64
+ */
65
+ prepareSource(wire, builder = this.builder) {
66
+ if (wire instanceof SourceBag) {
67
+ if (!(wire instanceof GramlotBuilderBag)) throw new TypeError('Source values must be GramlotBuilderBags');
68
+ return wire.bindBuilder(builder).activateCode();
69
+ }
70
+ const source = sourceBagFromTytx(wire, builder);
71
+ if (!(source instanceof GramlotBuilderBag)) throw new TypeError('Source values must be GramlotBuilderBags');
72
+ return source.activateCode();
73
+ }
74
+
75
+ /**
76
+ * Replace one Source node's children only after incoming Source validation.
77
+ * The request lives as long as the target's NodeBinding (P24): a DOM rebuild
78
+ * or a freeze keeps it, the removal of the target cancels it.
79
+ *
80
+ * Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
81
+ * they arrive together with the `remote` grammar attribute and `@endpoint`.
82
+ */
83
+ async remoteSource(target, method, params = {}) {
84
+ const rpc = this.gramlot.rpc;
85
+ if (!rpc.transport?.source) throw new Error('Remote Source is unavailable without a server transport');
86
+ const binding = this.binding.bindingFor(target);
87
+ if (this.gramlot.state !== 'started' || !binding) throw new Error('Remote Source target is not mounted');
88
+ rpc.remoteRequests.get(target)?.controller.abort();
89
+ const request = {controller: new AbortController()};
90
+ rpc.remoteRequests.set(target, request);
91
+ const unregister = binding.track(() => request.controller.abort());
92
+ let applying = false;
93
+ const current = () => this.gramlot.state !== 'disposed' && !request.controller.signal.aborted &&
94
+ rpc.remoteRequests.get(target) === request && !binding.closed;
95
+ try {
96
+ const wire = await rpc.transport.source(this.gramlot.pageId, method, params, request.controller.signal);
97
+ if (!current()) return false;
98
+ const prepared = this.prepareSource(wire, target.builder);
99
+ if (!current()) return false;
100
+ this.renderer.validateCandidate(prepared, {replacingNode: target});
101
+ applying = true;
102
+ unregister();
103
+ // The target's own prefix text belongs to the old body as well.
104
+ target.setValue(prepared, true, {_text: null}, true);
105
+ return true;
106
+ } catch (error) {
107
+ if (!applying && !current()) return false;
108
+ throw error;
109
+ } finally {
110
+ unregister();
111
+ if (rpc.remoteRequests.get(target) === request) rpc.remoteRequests.delete(target);
112
+ }
113
+ }
114
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * The page utilities of one Gramlot page, grouped by theme (`gramlot.utl`).
3
+ *
4
+ * @module
5
+ */
6
+ import type {DomDocument} from '../dom.d.ts';
7
+ import type {Gramlot} from '../gramlot.js';
8
+ import type {InOut} from '../inout.js';
9
+ import {Handler} from './handler.js';
10
+
11
+ /** Page utilities grouped by theme. */
12
+ export class UtilitiesHandler extends Handler {
13
+ /** The `gramlot.utl.inout` object: send, receive, save and download Data branches. */
14
+ inout: InOut;
15
+ /** Create the utilities of `gramlot`. */
16
+ constructor(gramlot: Gramlot, document: DomDocument);
17
+ }
@@ -0,0 +1,16 @@
1
+ /* @ts-self-types="./utilities-handler.d.ts" */
2
+ /**
3
+ * The page utilities of one Gramlot page, grouped by theme (`gramlot.utl`).
4
+ *
5
+ * @module
6
+ */
7
+ import {Handler} from './handler.js';
8
+ import {InOut} from '../inout.js';
9
+
10
+ /** Page utilities grouped by theme. */
11
+ export class UtilitiesHandler extends Handler {
12
+ constructor(gramlot, document) {
13
+ super(gramlot);
14
+ this.inout = new InOut(gramlot, document);
15
+ }
16
+ }
package/src/index.d.ts CHANGED
@@ -1,11 +1,16 @@
1
1
  /**
2
2
  * The browser entry point of Gramlot: declarative HTML and SVG interfaces with live data binding.
3
3
  * It exports the page instance (`Gramlot`), the bootstrap that starts it, the builder and the
4
- * renderers, the transport and `gramlot.inout`, and re-exports `Bag` and `BagNode` from the Bag library.
4
+ * renderers, the transport, the four handlers and `gramlot.utl.inout`, and re-exports `Bag` and `BagNode` from the Bag library.
5
5
  *
6
6
  * @module
7
7
  */
8
8
  export {Gramlot} from './gramlot.js';
9
+ export {Handler} from './handlers/handler.js';
10
+ export {SourceHandler} from './handlers/source-handler.js';
11
+ export {RpcHandler} from './handlers/rpc-handler.js';
12
+ export {DomHandler} from './handlers/dom-handler.js';
13
+ export {UtilitiesHandler} from './handlers/utilities-handler.js';
9
14
  export type {GramlotOptions, GramlotState} from './gramlot.js';
10
15
  export {PageBootstrap} from './bootstrap.js';
11
16
  export type {PageBootstrapOptions, PageResources} from './bootstrap.js';
package/src/index.js CHANGED
@@ -2,11 +2,16 @@
2
2
  /**
3
3
  * The browser entry point of Gramlot: declarative HTML and SVG interfaces with live data binding.
4
4
  * It exports the page instance (`Gramlot`), the bootstrap that starts it, the builder and the
5
- * renderers, the transport and `gramlot.inout`, and re-exports `Bag` and `BagNode` from the Bag library.
5
+ * renderers, the transport, the four handlers and `gramlot.utl.inout`, and re-exports `Bag` and `BagNode` from the Bag library.
6
6
  *
7
7
  * @module
8
8
  */
9
9
  export {Gramlot} from './gramlot.js';
10
+ export {Handler} from './handlers/handler.js';
11
+ export {SourceHandler} from './handlers/source-handler.js';
12
+ export {RpcHandler} from './handlers/rpc-handler.js';
13
+ export {DomHandler} from './handlers/dom-handler.js';
14
+ export {UtilitiesHandler} from './handlers/utilities-handler.js';
10
15
  export {PageBootstrap} from './bootstrap.js';
11
16
  // A page module served for its Logic imports @gramlot/gramlot/page; the bootstrap import map points it here.
12
17
  export {Page, source} from './adapters/page.js';
package/src/inout.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * What the page sends, receives, saves and downloads (`gramlot.inout`).
2
+ * What the page sends, receives, saves and downloads (`gramlot.utl.inout`).
3
3
  *
4
4
  * @module
5
5
  */
@@ -11,7 +11,7 @@ import type {Gramlot} from './gramlot.js';
11
11
  export const MAILTO_LIMIT: number;
12
12
 
13
13
  /**
14
- * What the page sends, receives, saves and downloads: `gramlot.inout`.
14
+ * What the page sends, receives, saves and downloads: `gramlot.utl.inout`.
15
15
  * Every function takes the Data path of a Bag branch; a missing path or a value that is not a Bag
16
16
  * raises an Error.
17
17
  */