@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/dist/gramlot.js +207 -165
- package/package.json +2 -3
- package/src/binding/inline.js +1 -1
- package/src/binding/runtime.js +3 -3
- package/src/bootstrap.js +1 -1
- package/src/builder/source.js +1 -1
- package/src/gramlot.d.ts +20 -49
- package/src/gramlot.js +21 -119
- package/src/handlers/dom-handler.d.ts +19 -0
- package/src/handlers/dom-handler.js +16 -0
- package/src/handlers/handler.d.ts +14 -0
- package/src/handlers/handler.js +13 -0
- package/src/handlers/rpc-handler.d.ts +21 -0
- package/src/handlers/rpc-handler.js +18 -0
- package/src/handlers/source-handler.d.ts +47 -0
- package/src/handlers/source-handler.js +114 -0
- package/src/handlers/utilities-handler.d.ts +17 -0
- package/src/handlers/utilities-handler.js +16 -0
- package/src/index.d.ts +6 -1
- package/src/index.js +6 -1
- package/src/inout.d.ts +2 -2
- package/src/inout.js +7 -7
- package/src/routes/README.md +0 -110
- package/src/routes/errors.d.ts +0 -12
- package/src/routes/errors.js +0 -15
- package/src/routes/index.d.ts +0 -14
- package/src/routes/index.js +0 -11
- package/src/routes/router-node.d.ts +0 -44
- package/src/routes/router-node.js +0 -117
- package/src/routes/router.d.ts +0 -68
- package/src/routes/router.js +0 -98
- package/src/routes/routing.d.ts +0 -56
- package/src/routes/routing.js +0 -75
- package/src/routes/signature.d.ts +0 -49
- package/src/routes/signature.js +0 -99
package/dist/gramlot.js
CHANGED
|
@@ -6519,6 +6519,55 @@ var require_lib = __commonJS({
|
|
|
6519
6519
|
}
|
|
6520
6520
|
});
|
|
6521
6521
|
|
|
6522
|
+
// src/transport.js
|
|
6523
|
+
var MainTransport = class {
|
|
6524
|
+
constructor(url, fetcher = globalThis.fetch?.bind(globalThis), sourceUrl = "/gramlot/source", closeUrl = "/gramlot/close", navigator = globalThis.navigator) {
|
|
6525
|
+
this.url = url;
|
|
6526
|
+
this.fetcher = fetcher;
|
|
6527
|
+
this.sourceUrl = sourceUrl;
|
|
6528
|
+
this.closeUrl = closeUrl;
|
|
6529
|
+
this.navigator = navigator;
|
|
6530
|
+
}
|
|
6531
|
+
async main(pageId, signal) {
|
|
6532
|
+
return this.request("main", this.url, { pageId }, signal);
|
|
6533
|
+
}
|
|
6534
|
+
async source(pageId, method, params, signal) {
|
|
6535
|
+
return this.request("source", this.sourceUrl, { pageId, method, params }, signal);
|
|
6536
|
+
}
|
|
6537
|
+
async request(operation, url, payload, signal) {
|
|
6538
|
+
const response = await this.fetcher(url, {
|
|
6539
|
+
method: "POST",
|
|
6540
|
+
credentials: "same-origin",
|
|
6541
|
+
signal,
|
|
6542
|
+
headers: { "Content-Type": "application/json" },
|
|
6543
|
+
body: JSON.stringify(payload)
|
|
6544
|
+
});
|
|
6545
|
+
if (!response.ok) throw new Error(`${operation} failed: HTTP ${response.status}`);
|
|
6546
|
+
return response.text();
|
|
6547
|
+
}
|
|
6548
|
+
close(pageId, { beacon = false } = {}) {
|
|
6549
|
+
const body = JSON.stringify({ pageId });
|
|
6550
|
+
if (beacon) {
|
|
6551
|
+
try {
|
|
6552
|
+
this.navigator.sendBeacon(this.closeUrl, new Blob([body], { type: "application/json" }));
|
|
6553
|
+
} catch {
|
|
6554
|
+
}
|
|
6555
|
+
} else {
|
|
6556
|
+
try {
|
|
6557
|
+
Promise.resolve(this.fetcher(this.closeUrl, {
|
|
6558
|
+
method: "POST",
|
|
6559
|
+
credentials: "same-origin",
|
|
6560
|
+
keepalive: true,
|
|
6561
|
+
headers: { "Content-Type": "application/json" },
|
|
6562
|
+
body
|
|
6563
|
+
})).catch(() => {
|
|
6564
|
+
});
|
|
6565
|
+
} catch {
|
|
6566
|
+
}
|
|
6567
|
+
}
|
|
6568
|
+
}
|
|
6569
|
+
};
|
|
6570
|
+
|
|
6522
6571
|
// node_modules/decimal.js/decimal.mjs
|
|
6523
6572
|
/*!
|
|
6524
6573
|
* decimal.js v10.6.0
|
|
@@ -21725,6 +21774,13 @@ var SvgBuilder = class extends BuilderBase {
|
|
|
21725
21774
|
};
|
|
21726
21775
|
BuilderBase.registerBuilder(SvgBuilder);
|
|
21727
21776
|
|
|
21777
|
+
// src/handlers/handler.js
|
|
21778
|
+
var Handler = class {
|
|
21779
|
+
constructor(gramlot) {
|
|
21780
|
+
this.gramlot = gramlot;
|
|
21781
|
+
}
|
|
21782
|
+
};
|
|
21783
|
+
|
|
21728
21784
|
// src/builder/source.js
|
|
21729
21785
|
var CODE_ATTRIBUTES = {
|
|
21730
21786
|
dataFormula: ["formula", "_if", "_else"],
|
|
@@ -21758,7 +21814,7 @@ var GramlotBuilderBagNode = class extends SourceBagNode {
|
|
|
21758
21814
|
}
|
|
21759
21815
|
/**
|
|
21760
21816
|
* Activate the inline code of this node: the current texts of its code attributes become the only
|
|
21761
|
-
* texts InlineCompiler runs for it. Called by GramlotBuilderBag.activateCode, from
|
|
21817
|
+
* texts InlineCompiler runs for it. Called by GramlotBuilderBag.activateCode, from SourceHandler.prepareSource:
|
|
21762
21818
|
* a text written later is not run. A code attribute holding a `^`/`=` pointer is an error, because
|
|
21763
21819
|
* its text would come from Data.
|
|
21764
21820
|
*/
|
|
@@ -24222,13 +24278,13 @@ var BindingRuntime = class {
|
|
|
24222
24278
|
return this.#gramlot;
|
|
24223
24279
|
}
|
|
24224
24280
|
get builder() {
|
|
24225
|
-
return this.gramlot.builder;
|
|
24281
|
+
return this.gramlot.src.builder;
|
|
24226
24282
|
}
|
|
24227
24283
|
get renderer() {
|
|
24228
|
-
return this.gramlot.renderer;
|
|
24284
|
+
return this.gramlot.src.renderer;
|
|
24229
24285
|
}
|
|
24230
24286
|
get logicRegistry() {
|
|
24231
|
-
return this.gramlot.logicRegistry;
|
|
24287
|
+
return this.gramlot.src.logicRegistry;
|
|
24232
24288
|
}
|
|
24233
24289
|
/** The document Data Bag. */
|
|
24234
24290
|
get data() {
|
|
@@ -25462,55 +25518,127 @@ var LogicRegistry = class {
|
|
|
25462
25518
|
}
|
|
25463
25519
|
};
|
|
25464
25520
|
|
|
25465
|
-
// src/
|
|
25466
|
-
var
|
|
25467
|
-
constructor(
|
|
25468
|
-
|
|
25469
|
-
|
|
25470
|
-
this.
|
|
25471
|
-
this.
|
|
25472
|
-
this.
|
|
25521
|
+
// src/handlers/source-handler.js
|
|
25522
|
+
var SourceHandler = class extends Handler {
|
|
25523
|
+
constructor(gramlot, { collections, destination }) {
|
|
25524
|
+
super(gramlot);
|
|
25525
|
+
gramlot.src = this;
|
|
25526
|
+
this.logicRegistry = new LogicRegistry(gramlot);
|
|
25527
|
+
this.builder = new GramlotBuilder(null, { collections });
|
|
25528
|
+
this.binding = new BindingRuntime(gramlot);
|
|
25529
|
+
this.builder.binding = this.binding;
|
|
25530
|
+
this.binding.attach();
|
|
25531
|
+
this.source = this.builder.source;
|
|
25532
|
+
this.renderer = new GramlotRenderer(this.builder, this.source, destination, { binding: this.binding });
|
|
25473
25533
|
}
|
|
25474
|
-
|
|
25475
|
-
|
|
25534
|
+
/** Start from an embedded typed Source using the same observed insertion as main. */
|
|
25535
|
+
startSource(wire) {
|
|
25536
|
+
if (!["ready", "failed"].includes(this.gramlot.state)) {
|
|
25537
|
+
throw new Error(`Cannot start embedded Source while Gramlot is ${this.gramlot.state}`);
|
|
25538
|
+
}
|
|
25539
|
+
try {
|
|
25540
|
+
return this.mountMainSource(wire);
|
|
25541
|
+
} catch (error) {
|
|
25542
|
+
this.gramlot.state = "failed";
|
|
25543
|
+
throw error;
|
|
25544
|
+
}
|
|
25476
25545
|
}
|
|
25477
|
-
|
|
25478
|
-
|
|
25546
|
+
/**
|
|
25547
|
+
* Validate and insert `main` (§5.1). When its installation fails after the insertion (its
|
|
25548
|
+
* NodeBindings closed again), `main` leaves the Source, so `start` or `startSource` can mount a
|
|
25549
|
+
* new one; the Data is not rolled back. A later error (`_init`, DOM) keeps `main` and its
|
|
25550
|
+
* NodeBindings (P12, no rollback).
|
|
25551
|
+
*/
|
|
25552
|
+
mountMainSource(wire) {
|
|
25553
|
+
const source2 = this.prepareSource(wire);
|
|
25554
|
+
this.renderer.validateCandidate(source2);
|
|
25555
|
+
try {
|
|
25556
|
+
this.source.setItem("main", source2);
|
|
25557
|
+
} catch (error) {
|
|
25558
|
+
if (!this.binding.bindingFor(this.source.getNode("main"))) this.source.popNode("main");
|
|
25559
|
+
throw error;
|
|
25560
|
+
}
|
|
25561
|
+
this.gramlot.state = "started";
|
|
25562
|
+
this.binding.pageStarted();
|
|
25563
|
+
return this.gramlot;
|
|
25479
25564
|
}
|
|
25480
|
-
|
|
25481
|
-
|
|
25482
|
-
|
|
25483
|
-
|
|
25484
|
-
|
|
25485
|
-
|
|
25486
|
-
|
|
25487
|
-
|
|
25488
|
-
|
|
25489
|
-
|
|
25565
|
+
/**
|
|
25566
|
+
* A Source from a SourceBag or a TYTX wire; only GramlotBuilderBag is accepted, before any effect (R07).
|
|
25567
|
+
* The received Source activates its inline code: the only inline code the page runs. Activation is
|
|
25568
|
+
* here and not in the TYTX decoding, because a GramlotBuilderBag can also arrive inside Data.
|
|
25569
|
+
*/
|
|
25570
|
+
prepareSource(wire, builder = this.builder) {
|
|
25571
|
+
if (wire instanceof SourceBag) {
|
|
25572
|
+
if (!(wire instanceof GramlotBuilderBag)) throw new TypeError("Source values must be GramlotBuilderBags");
|
|
25573
|
+
return wire.bindBuilder(builder).activateCode();
|
|
25574
|
+
}
|
|
25575
|
+
const source2 = sourceBagFromTytx(wire, builder);
|
|
25576
|
+
if (!(source2 instanceof GramlotBuilderBag)) throw new TypeError("Source values must be GramlotBuilderBags");
|
|
25577
|
+
return source2.activateCode();
|
|
25490
25578
|
}
|
|
25491
|
-
|
|
25492
|
-
|
|
25493
|
-
|
|
25494
|
-
|
|
25495
|
-
|
|
25496
|
-
|
|
25497
|
-
|
|
25498
|
-
|
|
25499
|
-
|
|
25500
|
-
|
|
25501
|
-
|
|
25502
|
-
|
|
25503
|
-
|
|
25504
|
-
|
|
25505
|
-
|
|
25506
|
-
|
|
25507
|
-
|
|
25508
|
-
|
|
25509
|
-
|
|
25579
|
+
/**
|
|
25580
|
+
* Replace one Source node's children only after incoming Source validation.
|
|
25581
|
+
* The request lives as long as the target's NodeBinding (P24): a DOM rebuild
|
|
25582
|
+
* or a freeze keeps it, the removal of the target cancels it.
|
|
25583
|
+
*
|
|
25584
|
+
* Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
|
|
25585
|
+
* they arrive together with the `remote` grammar attribute and `@endpoint`.
|
|
25586
|
+
*/
|
|
25587
|
+
async remoteSource(target, method, params = {}) {
|
|
25588
|
+
const rpc = this.gramlot.rpc;
|
|
25589
|
+
if (!rpc.transport?.source) throw new Error("Remote Source is unavailable without a server transport");
|
|
25590
|
+
const binding = this.binding.bindingFor(target);
|
|
25591
|
+
if (this.gramlot.state !== "started" || !binding) throw new Error("Remote Source target is not mounted");
|
|
25592
|
+
rpc.remoteRequests.get(target)?.controller.abort();
|
|
25593
|
+
const request = { controller: new AbortController() };
|
|
25594
|
+
rpc.remoteRequests.set(target, request);
|
|
25595
|
+
const unregister = binding.track(() => request.controller.abort());
|
|
25596
|
+
let applying = false;
|
|
25597
|
+
const current = () => this.gramlot.state !== "disposed" && !request.controller.signal.aborted && rpc.remoteRequests.get(target) === request && !binding.closed;
|
|
25598
|
+
try {
|
|
25599
|
+
const wire = await rpc.transport.source(this.gramlot.pageId, method, params, request.controller.signal);
|
|
25600
|
+
if (!current()) return false;
|
|
25601
|
+
const prepared = this.prepareSource(wire, target.builder);
|
|
25602
|
+
if (!current()) return false;
|
|
25603
|
+
this.renderer.validateCandidate(prepared, { replacingNode: target });
|
|
25604
|
+
applying = true;
|
|
25605
|
+
unregister();
|
|
25606
|
+
target.setValue(prepared, true, { _text: null }, true);
|
|
25607
|
+
return true;
|
|
25608
|
+
} catch (error) {
|
|
25609
|
+
if (!applying && !current()) return false;
|
|
25610
|
+
throw error;
|
|
25611
|
+
} finally {
|
|
25612
|
+
unregister();
|
|
25613
|
+
if (rpc.remoteRequests.get(target) === request) rpc.remoteRequests.delete(target);
|
|
25510
25614
|
}
|
|
25511
25615
|
}
|
|
25512
25616
|
};
|
|
25513
25617
|
|
|
25618
|
+
// src/handlers/rpc-handler.js
|
|
25619
|
+
var RpcHandler = class extends Handler {
|
|
25620
|
+
constructor(gramlot, { mainUrl, sourceUrl, closeUrl, document, transport }) {
|
|
25621
|
+
super(gramlot);
|
|
25622
|
+
this.transport = transport === false ? null : transport ?? new MainTransport(mainUrl, void 0, sourceUrl, closeUrl, document.defaultView.navigator);
|
|
25623
|
+
this.remoteRequests = /* @__PURE__ */ new Map();
|
|
25624
|
+
}
|
|
25625
|
+
};
|
|
25626
|
+
|
|
25627
|
+
// src/handlers/dom-handler.js
|
|
25628
|
+
var DomHandler = class extends Handler {
|
|
25629
|
+
reference(descriptor) {
|
|
25630
|
+
return this.gramlot.src.renderer.references.resolve(descriptor);
|
|
25631
|
+
}
|
|
25632
|
+
/** The Source node of the nearest element generated by the renderer (§4.11bis). */
|
|
25633
|
+
getBaseSourceNode(domNode) {
|
|
25634
|
+
return this.gramlot.src.renderer.getBaseSourceNode(domNode);
|
|
25635
|
+
}
|
|
25636
|
+
/** The element built from a Source node, or null (§4.11bis). */
|
|
25637
|
+
getDomNode(sourceNode) {
|
|
25638
|
+
return this.gramlot.src.renderer.getDomNode(sourceNode);
|
|
25639
|
+
}
|
|
25640
|
+
};
|
|
25641
|
+
|
|
25514
25642
|
// src/inout.js
|
|
25515
25643
|
var MAILTO_LIMIT = 2e3;
|
|
25516
25644
|
function leafText(value) {
|
|
@@ -25526,7 +25654,7 @@ var InOut = class {
|
|
|
25526
25654
|
/** The Bag at path in the Data. */
|
|
25527
25655
|
branch(path) {
|
|
25528
25656
|
const value = this.gramlot.data.getItem(path);
|
|
25529
|
-
if (!(value instanceof Bag)) throw new Error(`gramlot.inout: no data Bag at '${path}'`);
|
|
25657
|
+
if (!(value instanceof Bag)) throw new Error(`gramlot.utl.inout: no data Bag at '${path}'`);
|
|
25530
25658
|
return value;
|
|
25531
25659
|
}
|
|
25532
25660
|
/** One `relative.path: value` line per leaf of the branch at path. */
|
|
@@ -25546,7 +25674,7 @@ var InOut = class {
|
|
|
25546
25674
|
mailtoUrl(path, email) {
|
|
25547
25675
|
const url = `mailto:${encodeURIComponent(email)}?subject=${encodeURIComponent(this.document.title)}&body=${encodeURIComponent(this.mailText(path))}`;
|
|
25548
25676
|
if (url.length > MAILTO_LIMIT) {
|
|
25549
|
-
throw new Error(`gramlot.inout.sendMail: the email of '${path}' is ${url.length} characters long, over the mailto: limit of ${MAILTO_LIMIT}; send it with sendHttp or save it as a file`);
|
|
25677
|
+
throw new Error(`gramlot.utl.inout.sendMail: the email of '${path}' is ${url.length} characters long, over the mailto: limit of ${MAILTO_LIMIT}; send it with sendHttp or save it as a file`);
|
|
25550
25678
|
}
|
|
25551
25679
|
return url;
|
|
25552
25680
|
}
|
|
@@ -25562,7 +25690,7 @@ var InOut = class {
|
|
|
25562
25690
|
headers: { "Content-Type": "application/json" },
|
|
25563
25691
|
body
|
|
25564
25692
|
});
|
|
25565
|
-
if (!response.ok) throw new Error(`gramlot.inout.sendHttp: ${url} answered ${response.status}`);
|
|
25693
|
+
if (!response.ok) throw new Error(`gramlot.utl.inout.sendHttp: ${url} answered ${response.status}`);
|
|
25566
25694
|
return response;
|
|
25567
25695
|
}
|
|
25568
25696
|
/** Save the data at path as a TYTX file, which restore reads back with its exact types. */
|
|
@@ -25582,7 +25710,7 @@ var InOut = class {
|
|
|
25582
25710
|
resolve(bag);
|
|
25583
25711
|
} catch (error) {
|
|
25584
25712
|
reject(new Error(
|
|
25585
|
-
`gramlot.inout.restore: the file is not a saved Gramlot branch: ${error.message}`,
|
|
25713
|
+
`gramlot.utl.inout.restore: the file is not a saved Gramlot branch: ${error.message}`,
|
|
25586
25714
|
{ cause: error }
|
|
25587
25715
|
));
|
|
25588
25716
|
}
|
|
@@ -25594,7 +25722,7 @@ var InOut = class {
|
|
|
25594
25722
|
* after the last segment of path); types become text. */
|
|
25595
25723
|
download(path, filename, format) {
|
|
25596
25724
|
if (!["json", "xml"].includes(format)) {
|
|
25597
|
-
throw new Error(`gramlot.inout.download: format '${format}' is not 'json' or 'xml'`);
|
|
25725
|
+
throw new Error(`gramlot.utl.inout.download: format '${format}' is not 'json' or 'xml'`);
|
|
25598
25726
|
}
|
|
25599
25727
|
const bag = this.branch(path);
|
|
25600
25728
|
if (format === "json") {
|
|
@@ -25625,6 +25753,14 @@ var InOut = class {
|
|
|
25625
25753
|
}
|
|
25626
25754
|
};
|
|
25627
25755
|
|
|
25756
|
+
// src/handlers/utilities-handler.js
|
|
25757
|
+
var UtilitiesHandler = class extends Handler {
|
|
25758
|
+
constructor(gramlot, document) {
|
|
25759
|
+
super(gramlot);
|
|
25760
|
+
this.inout = new InOut(gramlot, document);
|
|
25761
|
+
}
|
|
25762
|
+
};
|
|
25763
|
+
|
|
25628
25764
|
// src/gramlot.js
|
|
25629
25765
|
var Gramlot = class {
|
|
25630
25766
|
constructor({
|
|
@@ -25639,21 +25775,14 @@ var Gramlot = class {
|
|
|
25639
25775
|
collections = []
|
|
25640
25776
|
} = {}) {
|
|
25641
25777
|
this.pageId = pageId;
|
|
25642
|
-
this.
|
|
25643
|
-
this.
|
|
25644
|
-
this.
|
|
25645
|
-
this.
|
|
25646
|
-
this.
|
|
25647
|
-
this.inout = new InOut(this, document);
|
|
25648
|
-
this.binding.attach();
|
|
25649
|
-
this.source = this.builder.source;
|
|
25650
|
-
const destination = element ?? document.getElementById(rootId);
|
|
25651
|
-
this.renderer = new GramlotRenderer(this.builder, this.source, destination, { binding: this.binding });
|
|
25652
|
-
this.transport = transport === false ? null : transport ?? new MainTransport(mainUrl, void 0, sourceUrl, closeUrl, document.defaultView.navigator);
|
|
25653
|
-
this.remoteRequests = /* @__PURE__ */ new Map();
|
|
25778
|
+
this.src = new SourceHandler(this, { collections, destination: element ?? document.getElementById(rootId) });
|
|
25779
|
+
this.data = this.src.builder.data;
|
|
25780
|
+
this.rpc = new RpcHandler(this, { mainUrl, sourceUrl, closeUrl, document, transport });
|
|
25781
|
+
this.dom = new DomHandler(this);
|
|
25782
|
+
this.utl = new UtilitiesHandler(this, document);
|
|
25654
25783
|
this.state = "ready";
|
|
25655
25784
|
this.abort = new AbortController();
|
|
25656
|
-
if (this.transport instanceof MainTransport) {
|
|
25785
|
+
if (this.rpc.transport instanceof MainTransport) {
|
|
25657
25786
|
this.pagehide = (event) => {
|
|
25658
25787
|
if (!event.persisted) this.dispose({ beacon: true });
|
|
25659
25788
|
};
|
|
@@ -25663,7 +25792,7 @@ var Gramlot = class {
|
|
|
25663
25792
|
}
|
|
25664
25793
|
start() {
|
|
25665
25794
|
if (this.state === "disposed") return Promise.reject(new Error("Gramlot is disposed"));
|
|
25666
|
-
if (!this.transport) return Promise.reject(new Error("This Gramlot instance has no server transport"));
|
|
25795
|
+
if (!this.rpc.transport) return Promise.reject(new Error("This Gramlot instance has no server transport"));
|
|
25667
25796
|
if (this.loading) return this.loading;
|
|
25668
25797
|
if (this.state === "started") return Promise.resolve(this);
|
|
25669
25798
|
this.state = "loading";
|
|
@@ -25674,122 +25803,30 @@ var Gramlot = class {
|
|
|
25674
25803
|
}
|
|
25675
25804
|
async loadMain() {
|
|
25676
25805
|
try {
|
|
25677
|
-
const wire = await this.transport.main(this.pageId, this.abort.signal);
|
|
25806
|
+
const wire = await this.rpc.transport.main(this.pageId, this.abort.signal);
|
|
25678
25807
|
if (this.state === "disposed") throw new Error("Gramlot was disposed during main");
|
|
25679
|
-
return this.mountMainSource(wire);
|
|
25808
|
+
return this.src.mountMainSource(wire);
|
|
25680
25809
|
} catch (error) {
|
|
25681
25810
|
if (this.state !== "disposed") this.state = "failed";
|
|
25682
25811
|
throw error;
|
|
25683
25812
|
}
|
|
25684
25813
|
}
|
|
25685
|
-
/** Start from an embedded typed Source using the same observed insertion as main. */
|
|
25686
|
-
startSource(wire) {
|
|
25687
|
-
if (!["ready", "failed"].includes(this.state)) {
|
|
25688
|
-
throw new Error(`Cannot start embedded Source while Gramlot is ${this.state}`);
|
|
25689
|
-
}
|
|
25690
|
-
try {
|
|
25691
|
-
return this.mountMainSource(wire);
|
|
25692
|
-
} catch (error) {
|
|
25693
|
-
this.state = "failed";
|
|
25694
|
-
throw error;
|
|
25695
|
-
}
|
|
25696
|
-
}
|
|
25697
|
-
/**
|
|
25698
|
-
* Validate and insert `main` (§5.1). When its installation fails after the insertion (its
|
|
25699
|
-
* NodeBindings closed again), `main` leaves the Source, so `start` or `startSource` can mount a
|
|
25700
|
-
* new one; the Data is not rolled back. A later error (`_init`, DOM) keeps `main` and its
|
|
25701
|
-
* NodeBindings (P12, no rollback).
|
|
25702
|
-
*/
|
|
25703
|
-
mountMainSource(wire) {
|
|
25704
|
-
const source2 = this.prepareSource(wire);
|
|
25705
|
-
this.renderer.validateCandidate(source2);
|
|
25706
|
-
try {
|
|
25707
|
-
this.source.setItem("main", source2);
|
|
25708
|
-
} catch (error) {
|
|
25709
|
-
if (!this.binding.bindingFor(this.source.getNode("main"))) this.source.popNode("main");
|
|
25710
|
-
throw error;
|
|
25711
|
-
}
|
|
25712
|
-
this.state = "started";
|
|
25713
|
-
this.binding.pageStarted();
|
|
25714
|
-
return this;
|
|
25715
|
-
}
|
|
25716
|
-
/**
|
|
25717
|
-
* A Source from a SourceBag or a TYTX wire; only GramlotBuilderBag is accepted, before any effect (R07).
|
|
25718
|
-
* The received Source activates its inline code: the only inline code the page runs. Activation is
|
|
25719
|
-
* here and not in the TYTX decoding, because a GramlotBuilderBag can also arrive inside Data.
|
|
25720
|
-
*/
|
|
25721
|
-
prepareSource(wire, builder = this.builder) {
|
|
25722
|
-
if (wire instanceof SourceBag) {
|
|
25723
|
-
if (!(wire instanceof GramlotBuilderBag)) throw new TypeError("Source values must be GramlotBuilderBags");
|
|
25724
|
-
return wire.bindBuilder(builder).activateCode();
|
|
25725
|
-
}
|
|
25726
|
-
const source2 = sourceBagFromTytx(wire, builder);
|
|
25727
|
-
if (!(source2 instanceof GramlotBuilderBag)) throw new TypeError("Source values must be GramlotBuilderBags");
|
|
25728
|
-
return source2.activateCode();
|
|
25729
|
-
}
|
|
25730
|
-
/**
|
|
25731
|
-
* Replace one Source node's children only after incoming Source validation.
|
|
25732
|
-
* The request lives as long as the target's NodeBinding (P24): a DOM rebuild
|
|
25733
|
-
* or a freeze keeps it, the removal of the target cancels it.
|
|
25734
|
-
*
|
|
25735
|
-
* Source methods (`@source`, `source(...)`, `remoteSource`) are not yet part of the page-writing API:
|
|
25736
|
-
* they arrive together with the `remote` grammar attribute and `@endpoint`.
|
|
25737
|
-
*/
|
|
25738
|
-
async remoteSource(target, method, params = {}) {
|
|
25739
|
-
if (!this.transport?.source) throw new Error("Remote Source is unavailable without a server transport");
|
|
25740
|
-
const binding = this.binding.bindingFor(target);
|
|
25741
|
-
if (this.state !== "started" || !binding) throw new Error("Remote Source target is not mounted");
|
|
25742
|
-
this.remoteRequests.get(target)?.controller.abort();
|
|
25743
|
-
const request = { controller: new AbortController() };
|
|
25744
|
-
this.remoteRequests.set(target, request);
|
|
25745
|
-
const unregister = binding.track(() => request.controller.abort());
|
|
25746
|
-
let applying = false;
|
|
25747
|
-
const current = () => this.state !== "disposed" && !request.controller.signal.aborted && this.remoteRequests.get(target) === request && !binding.closed;
|
|
25748
|
-
try {
|
|
25749
|
-
const wire = await this.transport.source(this.pageId, method, params, request.controller.signal);
|
|
25750
|
-
if (!current()) return false;
|
|
25751
|
-
const prepared = this.prepareSource(wire, target.builder);
|
|
25752
|
-
if (!current()) return false;
|
|
25753
|
-
this.renderer.validateCandidate(prepared, { replacingNode: target });
|
|
25754
|
-
applying = true;
|
|
25755
|
-
unregister();
|
|
25756
|
-
target.setValue(prepared, true, { _text: null }, true);
|
|
25757
|
-
return true;
|
|
25758
|
-
} catch (error) {
|
|
25759
|
-
if (!applying && !current()) return false;
|
|
25760
|
-
throw error;
|
|
25761
|
-
} finally {
|
|
25762
|
-
unregister();
|
|
25763
|
-
if (this.remoteRequests.get(target) === request) this.remoteRequests.delete(target);
|
|
25764
|
-
}
|
|
25765
|
-
}
|
|
25766
|
-
reference(descriptor) {
|
|
25767
|
-
return this.renderer.references.resolve(descriptor);
|
|
25768
|
-
}
|
|
25769
|
-
/** The Source node of the nearest element generated by the renderer (§4.11bis). */
|
|
25770
|
-
getBaseSourceNode(domNode) {
|
|
25771
|
-
return this.renderer.getBaseSourceNode(domNode);
|
|
25772
|
-
}
|
|
25773
|
-
/** The element built from a Source node, or null (§4.11bis). */
|
|
25774
|
-
getDomNode(sourceNode) {
|
|
25775
|
-
return this.renderer.getDomNode(sourceNode);
|
|
25776
|
-
}
|
|
25777
25814
|
dispose({ beacon = false } = {}) {
|
|
25778
25815
|
if (this.state === "disposed") return;
|
|
25779
25816
|
this.state = "disposed";
|
|
25780
25817
|
if (this.pagehide) this.window.removeEventListener("pagehide", this.pagehide);
|
|
25781
25818
|
this.abort.abort();
|
|
25782
|
-
for (const request of this.remoteRequests.values()) request.controller.abort();
|
|
25783
|
-
this.remoteRequests.clear();
|
|
25819
|
+
for (const request of this.rpc.remoteRequests.values()) request.controller.abort();
|
|
25820
|
+
this.rpc.remoteRequests.clear();
|
|
25784
25821
|
try {
|
|
25785
25822
|
try {
|
|
25786
|
-
this.binding.dispose();
|
|
25823
|
+
this.src.binding.dispose();
|
|
25787
25824
|
} finally {
|
|
25788
|
-
this.renderer.dispose();
|
|
25825
|
+
this.src.renderer.dispose();
|
|
25789
25826
|
}
|
|
25790
25827
|
} finally {
|
|
25791
|
-
if (this.transport instanceof MainTransport) this.transport.close(this.pageId, { beacon });
|
|
25792
|
-
this.transport?.dispose?.();
|
|
25828
|
+
if (this.rpc.transport instanceof MainTransport) this.rpc.transport.close(this.pageId, { beacon });
|
|
25829
|
+
this.rpc.transport?.dispose?.();
|
|
25793
25830
|
}
|
|
25794
25831
|
}
|
|
25795
25832
|
};
|
|
@@ -25842,7 +25879,7 @@ var PageBootstrap = class {
|
|
|
25842
25879
|
window2.removeEventListener("pagehide", pagehide);
|
|
25843
25880
|
}
|
|
25844
25881
|
const app = new Gramlot({ ...config2, document });
|
|
25845
|
-
for (const { logicClass, group, resource } of entries) app.logicRegistry.register(logicClass, { group, resource });
|
|
25882
|
+
for (const { logicClass, group, resource } of entries) app.src.logicRegistry.register(logicClass, { group, resource });
|
|
25846
25883
|
window2.gramlot = app;
|
|
25847
25884
|
await app.start();
|
|
25848
25885
|
return app;
|
|
@@ -25874,15 +25911,20 @@ var Page = class {
|
|
|
25874
25911
|
export {
|
|
25875
25912
|
Bag,
|
|
25876
25913
|
BagNode,
|
|
25914
|
+
DomHandler,
|
|
25877
25915
|
Gramlot,
|
|
25878
25916
|
GramlotBuilder,
|
|
25879
25917
|
GramlotHtmlRenderer,
|
|
25880
25918
|
GramlotRenderer,
|
|
25881
25919
|
GramlotSvgRenderer,
|
|
25920
|
+
Handler,
|
|
25882
25921
|
InOut,
|
|
25883
25922
|
MainTransport,
|
|
25884
25923
|
Page,
|
|
25885
25924
|
PageBootstrap,
|
|
25886
25925
|
References,
|
|
25926
|
+
RpcHandler,
|
|
25927
|
+
SourceHandler,
|
|
25928
|
+
UtilitiesHandler,
|
|
25887
25929
|
source
|
|
25888
25930
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gramlot/gramlot",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.10",
|
|
4
4
|
"description": "Declarative HTML/SVG interfaces with live data binding, authored in JavaScript or Python.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"type": "module",
|
|
12
12
|
"scripts": {
|
|
13
|
-
"test": "node --test tests/*.test.js tests/binding-contracts/gramlot-quiet-write.test.mjs tests/upstream-regressions/builder-symbolic-attribute.test.mjs
|
|
13
|
+
"test": "node --test tests/*.test.js tests/binding-contracts/gramlot-quiet-write.test.mjs tests/upstream-regressions/builder-symbolic-attribute.test.mjs",
|
|
14
14
|
"build": "node scripts/build-runtime.mjs",
|
|
15
15
|
"test:host": "node --test tests/host.test.js",
|
|
16
16
|
"prepack": "node scripts/build-runtime.mjs"
|
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
"./runtime": "./dist/gramlot.js",
|
|
32
32
|
"./page": "./src/adapters/page.js",
|
|
33
33
|
"./host": "./src/adapters/host.js",
|
|
34
|
-
"./routes": "./src/routes/index.js",
|
|
35
34
|
"./themes/*": "./themes/*",
|
|
36
35
|
"./assets/branding/*": "./assets/branding/*"
|
|
37
36
|
},
|
package/src/binding/inline.js
CHANGED
|
@@ -7,7 +7,7 @@ import {isExpression, templateParameters} from '../renderer/attributes.js';
|
|
|
7
7
|
* only by the page runtime: never by `adapters/*`, `builder/*` or a WorkerHost, so a page with named logic
|
|
8
8
|
* only runs under a CSP without `'unsafe-eval'`.
|
|
9
9
|
*
|
|
10
|
-
* Only the inline code received with the Source runs: the text activated by
|
|
10
|
+
* Only the inline code received with the Source runs: the text activated by SourceHandler.prepareSource
|
|
11
11
|
* (GramlotBuilderBagNode.activateCode). A text written later in the Source, by page code or from Data,
|
|
12
12
|
* is an error naming node and attribute.
|
|
13
13
|
*
|
package/src/binding/runtime.js
CHANGED
|
@@ -37,9 +37,9 @@ export class BindingRuntime {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
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; }
|
|
40
|
+
get builder() { return this.gramlot.src.builder; }
|
|
41
|
+
get renderer() { return this.gramlot.src.renderer; }
|
|
42
|
+
get logicRegistry() { return this.gramlot.src.logicRegistry; }
|
|
43
43
|
/** The document Data Bag. */
|
|
44
44
|
get data() { return this.builder.data; }
|
|
45
45
|
/** Builder's Data wrapper `_dataroot`: `root.getItem('_root_') === data`. */
|
package/src/bootstrap.js
CHANGED
|
@@ -63,7 +63,7 @@ export class PageBootstrap {
|
|
|
63
63
|
window.removeEventListener('pagehide', pagehide);
|
|
64
64
|
}
|
|
65
65
|
const app = new Gramlot({...config, document});
|
|
66
|
-
for (const {logicClass, group, resource} of entries) app.logicRegistry.register(logicClass, {group, resource});
|
|
66
|
+
for (const {logicClass, group, resource} of entries) app.src.logicRegistry.register(logicClass, {group, resource});
|
|
67
67
|
window.gramlot = app;
|
|
68
68
|
await app.start();
|
|
69
69
|
return app;
|
package/src/builder/source.js
CHANGED
|
@@ -46,7 +46,7 @@ export class GramlotBuilderBagNode extends SourceBagNode {
|
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Activate the inline code of this node: the current texts of its code attributes become the only
|
|
49
|
-
* texts InlineCompiler runs for it. Called by GramlotBuilderBag.activateCode, from
|
|
49
|
+
* texts InlineCompiler runs for it. Called by GramlotBuilderBag.activateCode, from SourceHandler.prepareSource:
|
|
50
50
|
* a text written later is not run. A code attribute holding a `^`/`=` pointer is an error, because
|
|
51
51
|
* its text would come from Data.
|
|
52
52
|
*/
|