@gramlot/gramlot 0.3.17 → 0.3.19
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/components/editors/__init__.py +1 -1
- package/dist/components/editors/assets/collection.js +1 -1
- package/dist/components/editors/assets/index.js +1 -1
- package/dist/components/inputs/__init__.py +1 -1
- package/dist/components/inputs/assets/collection.js +1 -1
- package/dist/components/inputs/assets/index.js +1 -1
- package/dist/components/layout/__init__.py +1 -1
- package/dist/components/layout/assets/collection.js +2 -2
- package/dist/components/layout/assets/index.js +1 -1
- package/dist/components/tree/__init__.py +1 -1
- package/dist/components/tree/assets/collection.js +1 -1
- package/dist/components/tree/assets/index.js +1 -1
- package/dist/gramlot.js +1099 -620
- package/dist/gramlot.min.js +41 -39
- package/package.json +2 -2
- package/src/binding/data-binder.js +71 -0
- package/src/binding/providers.js +7 -4
- package/src/binding/router.js +3 -1
- package/src/binding/runtime.js +9 -1
- package/src/builder/binding.json +49 -0
- package/src/components/window-surface.js +1 -1
- package/src/frame-messages.js +142 -73
- package/src/gramlot.d.ts +6 -3
- package/src/gramlot.js +10 -3
- package/src/handlers/rpc-handler.js +1 -1
- package/src/handlers/subscriptions-handler.js +20 -14
- package/src/index-page-styles.js +1 -1
- package/src/page-store-connection.js +220 -0
- package/src/server-adapter.d.ts +4 -3
- package/src/server-adapter.js +6 -5
- package/src/view/labelbox.js +4 -2
- package/src/websocket-transport.d.ts +10 -0
- package/src/websocket-transport.js +13 -0
- package/themes/gramlot-base/theme.css +6 -0
- package/themes/gramlot-compact/theme.css +6 -0
- package/themes/joanna/theme.css +6 -0
- package/themes/mela/theme.css +6 -0
- package/themes/mimi/theme.css +6 -0
- package/themes/palettes/compact.css +6 -0
- package/themes/palettes/joanna.css +6 -0
- package/themes/palettes/mimi.css +6 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gramlot/gramlot",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.19",
|
|
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": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"@codemirror/search": "^6.7.2",
|
|
32
32
|
"@codemirror/state": "^6.7.6",
|
|
33
33
|
"@codemirror/view": "^6.43.15",
|
|
34
|
-
"@genrojs/bag": ">=0.11.
|
|
34
|
+
"@genrojs/bag": ">=0.11.1",
|
|
35
35
|
"@genrojs/builders": ">=0.7.0",
|
|
36
36
|
"@genrojs/toolbox": ">=0.19.0",
|
|
37
37
|
"@genrojs/tytx": ">=0.16.3",
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {randomId} from '../random-id.js';
|
|
2
|
+
|
|
3
|
+
const REMOTE = 'gramlot-page-store-remote';
|
|
4
|
+
|
|
5
|
+
/** One Source node's subscription between client Data and its page's server Bag. */
|
|
6
|
+
export class DataBinder {
|
|
7
|
+
constructor(binding) {
|
|
8
|
+
this.binding = binding;
|
|
9
|
+
this.registration = null;
|
|
10
|
+
this.ready = false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
register() {
|
|
14
|
+
const {runtime, node} = this.binding;
|
|
15
|
+
this.connection = runtime.gramlot._pageStore;
|
|
16
|
+
if (!this.connection) throw new Error('dataBinder requires a PageStore WebSocket');
|
|
17
|
+
this.clientPath = node.absDatapath(node.pathAttribute('destination_path'));
|
|
18
|
+
this.serverPath = node.getAttr('server_path');
|
|
19
|
+
if (typeof this.clientPath !== 'string' || !this.clientPath || this.clientPath.includes('?')) {
|
|
20
|
+
throw new TypeError('dataBinder requires a nonempty client Data path');
|
|
21
|
+
}
|
|
22
|
+
if (typeof this.serverPath !== 'string' || !/^[^.?#\[\]!:]+(?:\.[^.?#\[\]!:]+)*$/.test(this.serverPath)) {
|
|
23
|
+
throw new TypeError('dataBinder requires a literal server path');
|
|
24
|
+
}
|
|
25
|
+
this.id = randomId();
|
|
26
|
+
this.ready = false;
|
|
27
|
+
this.registration = runtime.router.register({path: this.clientPath, recipient: this, subtree: true});
|
|
28
|
+
try { this.connection.register(this); }
|
|
29
|
+
catch (error) { this.registration.close(); this.registration = null; throw error; }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
receive(change) {
|
|
33
|
+
if (!this.ready || change.reason === REMOTE) return;
|
|
34
|
+
if (change.fired) throw new Error('Fired events are outside the first PageStore PoC');
|
|
35
|
+
const {data} = this.binding.runtime;
|
|
36
|
+
const ancestor = change.level === 'container';
|
|
37
|
+
const node = ancestor ? data.getNode(this.clientPath) : change.node;
|
|
38
|
+
this.connection.send({
|
|
39
|
+
op: 'change', binderId: this.id,
|
|
40
|
+
path: ancestor || change.path === this.clientPath ? '' : change.path.slice(this.clientPath.length + 1),
|
|
41
|
+
evt: ancestor ? (node ? 'upd_value' : 'del') : change.evt,
|
|
42
|
+
value: node?.value ?? null, attr: {...node?.attr},
|
|
43
|
+
index: change.index, fired: false,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
apply(message) {
|
|
48
|
+
if (!this.registration?.active) return;
|
|
49
|
+
const {data} = this.binding.runtime;
|
|
50
|
+
const path = message.path ? this.clientPath + '.' + message.path : this.clientPath;
|
|
51
|
+
this.ready = true;
|
|
52
|
+
if (message.op === 'snapshot') {
|
|
53
|
+
if (!message.exists) data.popNode(this.clientPath, REMOTE);
|
|
54
|
+
else data.setItem(this.clientPath, message.value, message.attr, '>', false, false, REMOTE);
|
|
55
|
+
} else if (message.op === 'change') {
|
|
56
|
+
if (message.evt === 'del') data.popNode(path, REMOTE);
|
|
57
|
+
else if (message.evt === 'upd_attrs') data.getNode(path).setAttr(message.attr, REMOTE, false, false);
|
|
58
|
+
else data.setItem(path, message.value, message.attr, message.index ?? '>', false, false, REMOTE);
|
|
59
|
+
} else {
|
|
60
|
+
throw new Error('Unknown PageStore delivery');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
unregister() {
|
|
65
|
+
this.ready = false;
|
|
66
|
+
this.registration?.close();
|
|
67
|
+
this.registration = null;
|
|
68
|
+
this.connection?.unregister(this);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
package/src/binding/providers.js
CHANGED
|
@@ -127,8 +127,8 @@ export class Provider {
|
|
|
127
127
|
if (name === '_timing' || timingDependencies.has(name)) this.#timingRegistrations.add(registration);
|
|
128
128
|
}
|
|
129
129
|
// Legacy provider declarations subscribe by presence, including a false value.
|
|
130
|
-
this.#registrations.push(...this.binding.runtime.subscriptions.subscribeNode(node, (name, value, topic) => payload => {
|
|
131
|
-
const trigger = {kind: 'subscription', topic, payload};
|
|
130
|
+
this.#registrations.push(...this.binding.runtime.subscriptions.subscribeNode(node, (name, value, topic) => (payload, metadata) => {
|
|
131
|
+
const trigger = {kind: 'subscription', topic, payload, metadata};
|
|
132
132
|
if (this.#resolved('_userChanges') && topic !== 'node') return;
|
|
133
133
|
const delay = this.#resolved('_delay');
|
|
134
134
|
if (delay > 0) this.#timers.debounce(delay, () => this.invoke(trigger));
|
|
@@ -234,7 +234,10 @@ export class Provider {
|
|
|
234
234
|
!CONTROL_ATTRIBUTES.has(name) && !name.startsWith('subscribe_') && !name.startsWith('selfsubscribe_')));
|
|
235
235
|
const kwargs = {...(trigger.kind === 'subscription' ? trigger.payload : {}),
|
|
236
236
|
...this.binding.runtime.renderer.references.resolveArguments(authored)};
|
|
237
|
-
if (trigger.kind === 'subscription')
|
|
237
|
+
if (trigger.kind === 'subscription') {
|
|
238
|
+
kwargs._subscription_kwargs = trigger.payload;
|
|
239
|
+
kwargs._subscription_metadata = trigger.metadata;
|
|
240
|
+
}
|
|
238
241
|
const change = trigger.change ?? null;
|
|
239
242
|
const reason = change ? change.level : trigger.kind === 'subscription' ? trigger.topic : trigger.kind;
|
|
240
243
|
kwargs._node = change ? change.node : null;
|
|
@@ -396,7 +399,7 @@ export class RemoteProvider extends Provider {
|
|
|
396
399
|
}
|
|
397
400
|
|
|
398
401
|
// The trigger fields of the kwargs, in the legacy order of the inline parameters.
|
|
399
|
-
const TRIGGER_FIELDS = ['_node', '_triggerpars', '_reason', '_subscription_kwargs', '_askResult'];
|
|
402
|
+
const TRIGGER_FIELDS = ['_node', '_triggerpars', '_reason', '_subscription_kwargs', '_subscription_metadata', '_askResult'];
|
|
400
403
|
// The fields a click adds (P9): locals of the inline bodies, never author arguments.
|
|
401
404
|
const CLICK_FIELDS = ['_evt', 'button_counter', 'button_shift', 'button_ctrl', 'button_alt', 'button_meta'];
|
|
402
405
|
|
package/src/binding/router.js
CHANGED
|
@@ -58,7 +58,7 @@ export class DataRegistration {
|
|
|
58
58
|
|
|
59
59
|
/** The object delivered to a recipient. */
|
|
60
60
|
export class DataChange {
|
|
61
|
-
constructor({evt, node, path, oldvalue, attrsDiff, reason, fired, level, registration}) {
|
|
61
|
+
constructor({evt, node, path, oldvalue, attrsDiff, reason, fired, level, registration, index}) {
|
|
62
62
|
this.evt = evt;
|
|
63
63
|
this.node = node;
|
|
64
64
|
this.path = path;
|
|
@@ -68,6 +68,7 @@ export class DataChange {
|
|
|
68
68
|
this.fired = fired;
|
|
69
69
|
this.level = level;
|
|
70
70
|
this.registration = registration;
|
|
71
|
+
this.index = index;
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
|
|
@@ -142,6 +143,7 @@ export class DataRouter {
|
|
|
142
143
|
evt: event.evt, node, path, oldvalue: event.oldvalue ?? null,
|
|
143
144
|
attrsDiff: event.attrs_diff ?? null, reason: event.reason ?? null,
|
|
144
145
|
fired: event.fired ?? false, level, registration,
|
|
146
|
+
index: event.ind ?? null,
|
|
145
147
|
}));
|
|
146
148
|
}
|
|
147
149
|
}
|
package/src/binding/runtime.js
CHANGED
|
@@ -5,6 +5,7 @@ import {ControllerProvider, FormulaProvider, RemoteProvider, RpcProvider} from '
|
|
|
5
5
|
import {InlineCompiler} from './inline.js';
|
|
6
6
|
import {isLogicMethod, callLogic} from './hooks.js';
|
|
7
7
|
import {RemoteBinding} from './remote.js';
|
|
8
|
+
import {DataBinder} from './data-binder.js';
|
|
8
9
|
import {AskDialog} from './ask-dialog.js';
|
|
9
10
|
import {ErrorNotifications} from './error-notifications.js';
|
|
10
11
|
import {randomId} from '../random-id.js';
|
|
@@ -357,6 +358,7 @@ export class NodeBinding {
|
|
|
357
358
|
#registrations = [];
|
|
358
359
|
#stamps = new Set();
|
|
359
360
|
#provider = null;
|
|
361
|
+
#binder = null;
|
|
360
362
|
#remote = null;
|
|
361
363
|
// The registrations of `remote` and of the `remote_<k>` parameters: their changes request the fragment again.
|
|
362
364
|
#remoteRegistrations = new Set();
|
|
@@ -416,7 +418,7 @@ export class NodeBinding {
|
|
|
416
418
|
}
|
|
417
419
|
const node = this.node;
|
|
418
420
|
const dataElement = node._getMeta('data_element');
|
|
419
|
-
if (dataElement && !PROVIDERS[node.nodeTag]) return;
|
|
421
|
+
if (dataElement && !PROVIDERS[node.nodeTag] && node.nodeTag !== 'dataBinder') return;
|
|
420
422
|
if (!dataElement && typeof node.getAttr('remote') === 'string') this.#remote ??= new RemoteBinding(this);
|
|
421
423
|
else if (this.#remote) {
|
|
422
424
|
this.#remote.close();
|
|
@@ -452,6 +454,11 @@ export class NodeBinding {
|
|
|
452
454
|
this.remote?.register();
|
|
453
455
|
return;
|
|
454
456
|
}
|
|
457
|
+
if (node.nodeTag === 'dataBinder') {
|
|
458
|
+
this.#binder ??= new DataBinder(this);
|
|
459
|
+
this.#binder.register();
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
455
462
|
this.#provider ??= new PROVIDERS[node.nodeTag](this);
|
|
456
463
|
this.#provider.register();
|
|
457
464
|
}
|
|
@@ -548,6 +555,7 @@ export class NodeBinding {
|
|
|
548
555
|
}
|
|
549
556
|
|
|
550
557
|
#closeRegistrations() {
|
|
558
|
+
this.#binder?.unregister();
|
|
551
559
|
for (const registration of this.#registrations.splice(0)) registration.close();
|
|
552
560
|
this.#datapathRegistrations.clear();
|
|
553
561
|
this.#remoteRegistrations.clear();
|
package/src/builder/binding.json
CHANGED
|
@@ -11,6 +11,55 @@
|
|
|
11
11
|
},
|
|
12
12
|
"abstracts": {},
|
|
13
13
|
"elements": {
|
|
14
|
+
"dataBinder": {
|
|
15
|
+
"doc": "Bidirectional subscription between a client Data branch and its page's server store.",
|
|
16
|
+
"sub_tags": "",
|
|
17
|
+
"parent_tags": null,
|
|
18
|
+
"inherits_from": null,
|
|
19
|
+
"ns": null,
|
|
20
|
+
"attributes": {
|
|
21
|
+
"parameters": [
|
|
22
|
+
{
|
|
23
|
+
"name": "destination_path",
|
|
24
|
+
"kind": "positional_or_keyword",
|
|
25
|
+
"role": "attribute",
|
|
26
|
+
"annotation": {
|
|
27
|
+
"kind": "type",
|
|
28
|
+
"module": "builtins",
|
|
29
|
+
"name": "str"
|
|
30
|
+
},
|
|
31
|
+
"has_default": false
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "server_path",
|
|
35
|
+
"kind": "positional_or_keyword",
|
|
36
|
+
"role": "attribute",
|
|
37
|
+
"annotation": {
|
|
38
|
+
"kind": "type",
|
|
39
|
+
"module": "builtins",
|
|
40
|
+
"name": "str"
|
|
41
|
+
},
|
|
42
|
+
"has_default": false
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"name": "attr",
|
|
46
|
+
"kind": "var_keyword",
|
|
47
|
+
"role": "attribute",
|
|
48
|
+
"annotation": {
|
|
49
|
+
"kind": "any"
|
|
50
|
+
},
|
|
51
|
+
"has_default": false
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
"accepts_var_keyword": true,
|
|
55
|
+
"accepts_var_positional": false
|
|
56
|
+
},
|
|
57
|
+
"node_label": null,
|
|
58
|
+
"collection_key": null,
|
|
59
|
+
"_meta": {
|
|
60
|
+
"data_element": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
14
63
|
"dataSetter": {
|
|
15
64
|
"doc": "Initial Data value, written literally at destination_path.",
|
|
16
65
|
"sub_tags": "",
|
|
@@ -23,7 +23,7 @@ export class WindowSurface {
|
|
|
23
23
|
.window-surface dialog[data-maximized]{max-width:100vw;max-height:100dvh;border-radius:0}
|
|
24
24
|
.window-surface dialog::backdrop{background:var(--gramlot-window-backdrop,#0006)}
|
|
25
25
|
.window-surface .titlebar{display:flex;align-items:center;gap:4px;flex:none;min-height:max(32px,var(--gramlot-tab-height,32px));padding:3px 5px 3px 12px;
|
|
26
|
-
background:
|
|
26
|
+
background:var(--gramlot-header-background,#303946);color:var(--gramlot-header-color,#fff);border-bottom:1px solid var(--gramlot-border,#d5dce5)}
|
|
27
27
|
.window-surface .title{flex:1;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;margin:0;font-size:1em;font-weight:600}
|
|
28
28
|
.window-surface .titlebar[data-movable]{cursor:move;touch-action:none;user-select:none}
|
|
29
29
|
.window-surface button{font:inherit;color:inherit;background:transparent;border:0;border-radius:4px;cursor:pointer;min-width:max(24px,var(--gramlot-touch-target,0px));min-height:max(24px,var(--gramlot-touch-target,0px))}
|
package/src/frame-messages.js
CHANGED
|
@@ -3,20 +3,16 @@ import {SourceBagNode, sourceTarget} from '@genrojs/builders';
|
|
|
3
3
|
import {assertTransportData} from './references.js';
|
|
4
4
|
import {randomId} from './random-id.js';
|
|
5
5
|
|
|
6
|
-
const TYPE = 'gramlot:
|
|
6
|
+
const TYPE = 'gramlot:message';
|
|
7
7
|
|
|
8
|
-
/**
|
|
8
|
+
/** One Gramlot family registry and protocol for publications and server traffic. */
|
|
9
9
|
export class FrameMessages {
|
|
10
|
-
#app;
|
|
11
|
-
#window;
|
|
12
|
-
#document;
|
|
10
|
+
#app; #window; #document;
|
|
13
11
|
#session = randomId();
|
|
14
12
|
#peers = new Map();
|
|
13
|
+
#channels = new Map();
|
|
15
14
|
#closed = false;
|
|
16
|
-
#listener;
|
|
17
|
-
#load;
|
|
18
|
-
#hide;
|
|
19
|
-
#show;
|
|
15
|
+
#listener; #load; #hide; #show; #watch;
|
|
20
16
|
|
|
21
17
|
constructor(app, document) {
|
|
22
18
|
this.#app = app;
|
|
@@ -26,37 +22,82 @@ export class FrameMessages {
|
|
|
26
22
|
this.#load = event => {
|
|
27
23
|
if (event.target?.localName !== 'iframe') return;
|
|
28
24
|
const peer = this.#child(event.target);
|
|
29
|
-
if (
|
|
30
|
-
this.#forget(peer.window);
|
|
31
|
-
this.#send(peer, {kind: 'hello'});
|
|
25
|
+
if (peer) this.#hello(peer);
|
|
32
26
|
};
|
|
33
|
-
this.#
|
|
34
|
-
this.#hide = () => this.#disconnect();
|
|
27
|
+
this.#hide = event => { if (!event.persisted) this.#app.dispose({beacon: true}); };
|
|
35
28
|
this.#show = () => this.ready();
|
|
29
|
+
this.#window.addEventListener('message', this.#listener);
|
|
36
30
|
this.#window.addEventListener('pagehide', this.#hide);
|
|
37
31
|
this.#window.addEventListener('pageshow', this.#show);
|
|
38
32
|
document.addEventListener('load', this.#load, true);
|
|
39
|
-
const parent = this.#parent();
|
|
40
|
-
if (parent) this.#send(parent, {kind: 'hello'});
|
|
41
33
|
}
|
|
42
34
|
|
|
43
|
-
|
|
44
|
-
if (this.#
|
|
45
|
-
|
|
46
|
-
|
|
35
|
+
#watchFamily() {
|
|
36
|
+
if (this.#watch !== undefined) return;
|
|
37
|
+
// Unload messages can be lost when a browser destroys a browsing context.
|
|
38
|
+
this.#watch = this.#window.setInterval(() => {
|
|
39
|
+
if (this.parent?.window.closed) { this.#shutdown(); return; }
|
|
40
|
+
for (const peer of this.children) {
|
|
41
|
+
if (peer.window.closed) this.#forget(peer.window);
|
|
42
|
+
else if (peer.node && !this.#child(this.#app.dom.getDomNode(peer.node))) {
|
|
43
|
+
this.#forget(peer.window, true);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}, 500);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get parent() {
|
|
50
|
+
const iframe = this.#window.parent !== this.#window;
|
|
51
|
+
const window = iframe ? this.#window.parent : this.#window.opener;
|
|
52
|
+
return window ? {window, parent: true, kind: iframe ? 'iframe' : 'popup',
|
|
53
|
+
origin: this.#origin(this.#document.referrer || this.#document.baseURI)} : null;
|
|
54
|
+
}
|
|
55
|
+
/** Internal snapshot of direct Gramlot children, excluding unrelated windows. */
|
|
56
|
+
get children() { return [...this.#peers.values()].filter(peer => !peer.parent); }
|
|
57
|
+
isConnected(window) { return !this.#closed && this.#peers.has(window); }
|
|
58
|
+
|
|
59
|
+
connect() {
|
|
60
|
+
if (this.parent) { this.#watchFamily(); this.#hello(this.parent); }
|
|
47
61
|
for (const frame of this.#document.getElementsByTagName('iframe')) {
|
|
48
62
|
const child = this.#child(frame);
|
|
49
|
-
if (child) this.#
|
|
63
|
+
if (child) this.#hello(child);
|
|
50
64
|
}
|
|
51
65
|
}
|
|
66
|
+
ready() {
|
|
67
|
+
if (this.#closed) return;
|
|
68
|
+
this.connect();
|
|
69
|
+
for (const peer of this.#peers.values()) this.#announce(peer);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
registerChannel(name, handler, state) {
|
|
73
|
+
if (this.#closed || name === 'publish' || this.#channels.has(name)) throw new Error('Invalid family channel');
|
|
74
|
+
this.#channels.set(name, {handler, state});
|
|
75
|
+
for (const peer of this.#peers.values()) handler.changed(peer);
|
|
76
|
+
}
|
|
77
|
+
setChannelState(name, state) {
|
|
78
|
+
this.#channels.get(name).state = state;
|
|
79
|
+
for (const peer of this.#peers.values()) this.#announce(peer);
|
|
80
|
+
}
|
|
81
|
+
unregisterChannel(name) {
|
|
82
|
+
this.#channels.delete(name);
|
|
83
|
+
// Disposal sends a family close next; do not announce transient channel loss.
|
|
84
|
+
if (this.#app.state === 'disposed') return;
|
|
85
|
+
for (const peer of this.#peers.values()) this.#announce(peer);
|
|
86
|
+
}
|
|
87
|
+
/** Every consumer sends data through this typed envelope. */
|
|
88
|
+
send(window, channel, payload) {
|
|
89
|
+
const peer = this.#peers.get(window);
|
|
90
|
+
if (this.#closed || !peer) throw new Error('Destination page is not connected');
|
|
91
|
+
assertTransportData(payload);
|
|
92
|
+
this.#send(peer, {kind: 'message', recipient: peer.session, channel, wire: toTytx(payload)});
|
|
93
|
+
}
|
|
52
94
|
|
|
53
|
-
/** Preflight before local dispatch
|
|
95
|
+
/** Preflight before local dispatch; publications require a started destination. */
|
|
54
96
|
prepare(descriptor, payload) {
|
|
55
97
|
const peers = [];
|
|
56
98
|
if (descriptor.parent) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
peers.push(parent);
|
|
99
|
+
if (!this.parent) throw new Error('This page has no parent frame or opener');
|
|
100
|
+
peers.push(this.parent);
|
|
60
101
|
}
|
|
61
102
|
if (descriptor.iframe !== undefined) {
|
|
62
103
|
const target = descriptor.iframe;
|
|
@@ -68,13 +109,18 @@ export class FrameMessages {
|
|
|
68
109
|
if (!child) throw new Error('iframe destination must be an active Gramlot iframe ID or Source reference');
|
|
69
110
|
peers.push(child);
|
|
70
111
|
}
|
|
112
|
+
if (descriptor.pageId !== undefined) {
|
|
113
|
+
const child = this.children.find(peer => peer.pageId === descriptor.pageId);
|
|
114
|
+
if (!child) throw new Error('pageId destination must be a registered direct Gramlot child');
|
|
115
|
+
peers.push(child);
|
|
116
|
+
}
|
|
71
117
|
const destinations = peers.map(peer => {
|
|
72
118
|
const connection = this.#peers.get(peer.window);
|
|
73
|
-
if (!connection || connection.origin !== peer.origin) throw new Error('Destination page is not ready');
|
|
119
|
+
if (!connection?.ready || connection.origin !== peer.origin) throw new Error('Destination page is not ready');
|
|
74
120
|
return {...peer, session: connection.session};
|
|
75
121
|
});
|
|
76
122
|
assertTransportData(payload);
|
|
77
|
-
const wire = toTytx(payload);
|
|
123
|
+
const wire = toTytx({topic: descriptor.topic, payload});
|
|
78
124
|
return () => {
|
|
79
125
|
if (this.#closed) return;
|
|
80
126
|
for (const peer of destinations) {
|
|
@@ -82,97 +128,120 @@ export class FrameMessages {
|
|
|
82
128
|
if (!current || current.origin !== peer.origin || this.#peers.get(peer.window)?.session !== peer.session) {
|
|
83
129
|
throw new Error('Destination page disconnected before publication');
|
|
84
130
|
}
|
|
85
|
-
this.#send(peer, {kind: '
|
|
131
|
+
this.#send(peer, {kind: 'message', recipient: peer.session, channel: 'publish', wire});
|
|
86
132
|
}
|
|
87
133
|
};
|
|
88
134
|
}
|
|
89
135
|
|
|
90
136
|
dispose() {
|
|
91
137
|
if (this.#closed) return;
|
|
92
|
-
|
|
138
|
+
// Let consumers detach routes while the parent link still exists.
|
|
139
|
+
for (const peer of this.children) this.#forget(peer.window, true);
|
|
140
|
+
const parent = this.parent;
|
|
141
|
+
if (parent && this.#peers.has(parent.window)) {
|
|
142
|
+
this.#send(this.#peers.get(parent.window), {kind: 'close'});
|
|
143
|
+
this.#forget(parent.window);
|
|
144
|
+
}
|
|
93
145
|
this.#closed = true;
|
|
146
|
+
this.#channels.clear();
|
|
94
147
|
this.#window.removeEventListener('message', this.#listener);
|
|
95
148
|
this.#window.removeEventListener('pagehide', this.#hide);
|
|
96
149
|
this.#window.removeEventListener('pageshow', this.#show);
|
|
150
|
+
this.#window.clearInterval(this.#watch);
|
|
97
151
|
this.#document.removeEventListener('load', this.#load, true);
|
|
98
152
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
for (const window of this.#peers.keys()) this.#forget(window);
|
|
153
|
+
#shutdown() {
|
|
154
|
+
this.#app.dispose({beacon: true});
|
|
155
|
+
if (this.#window.parent === this.#window && this.#window.opener) this.#window.close();
|
|
103
156
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
157
|
+
#forget(window, terminate = false) {
|
|
158
|
+
const peer = this.#peers.get(window);
|
|
159
|
+
if (!peer) return;
|
|
160
|
+
if (terminate) this.#send(peer, {kind: 'close'});
|
|
107
161
|
this.#peers.delete(window);
|
|
162
|
+
peer.release?.();
|
|
163
|
+
for (const {handler} of this.#channels.values()) handler.disconnected(peer);
|
|
164
|
+
if (terminate && peer.kind === 'popup' && !peer.parent) window.close();
|
|
108
165
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
this.#forget(peer.window);
|
|
116
|
-
});
|
|
166
|
+
#remember(peer, message) {
|
|
167
|
+
const old = this.#peers.get(peer.window);
|
|
168
|
+
const fresh = old?.session !== message.session;
|
|
169
|
+
if (fresh) this.#forget(peer.window);
|
|
170
|
+
const connection = fresh ? {...peer, session: message.session} : old;
|
|
171
|
+
Object.assign(connection, {ready: message.ready === true, pageId: message.pageId, channels: message.channels});
|
|
117
172
|
this.#peers.set(peer.window, connection);
|
|
173
|
+
this.#watchFamily();
|
|
174
|
+
if (fresh && peer.node) connection.release = this.#app.src.binding.bindingFor(peer.node).track(() => {
|
|
175
|
+
this.#forget(peer.window, true);
|
|
176
|
+
});
|
|
177
|
+
for (const {handler} of this.#channels.values()) handler.changed(connection);
|
|
178
|
+
return fresh;
|
|
118
179
|
}
|
|
119
|
-
|
|
120
180
|
#receive(event) {
|
|
121
181
|
if (this.#closed || event.data?.type !== TYPE) return;
|
|
122
182
|
const peer = this.#peer(event.source);
|
|
123
183
|
if (!peer || event.origin !== peer.origin) return;
|
|
124
184
|
const message = event.data;
|
|
125
185
|
if (typeof message.session !== 'string' || !message.session) return;
|
|
126
|
-
if (message.kind === 'hello') {
|
|
127
|
-
this.#
|
|
128
|
-
if (message.
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
186
|
+
if (message.kind === 'hello' || message.kind === 'ready') {
|
|
187
|
+
if (message.kind === 'ready' && message.recipient !== this.#session) return;
|
|
188
|
+
if (!message.channels || typeof message.channels !== 'object' || Array.isArray(message.channels)) return;
|
|
189
|
+
const fresh = this.#remember(peer, message);
|
|
190
|
+
if (message.kind === 'hello' || fresh) this.#announce(this.#peers.get(peer.window));
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const connection = this.#peers.get(peer.window);
|
|
194
|
+
if (!connection || connection.session !== message.session || message.recipient !== this.#session) return;
|
|
195
|
+
if (message.kind === 'close') {
|
|
196
|
+
if (peer.parent) this.#shutdown();
|
|
197
|
+
else this.#forget(peer.window);
|
|
198
|
+
} else if (message.kind === 'message' && typeof message.wire === 'string') {
|
|
199
|
+
const payload = fromTytx(message.wire);
|
|
200
|
+
if (message.channel === 'publish') {
|
|
201
|
+
if (this.#app.state !== 'started' || !connection.ready) return;
|
|
202
|
+
if (typeof payload.topic !== 'string' || !payload.topic) return;
|
|
203
|
+
this.#app.publish(payload.topic, payload.payload);
|
|
204
|
+
} else this.#channels.get(message.channel)?.handler.receive(connection, payload);
|
|
142
205
|
}
|
|
143
206
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
207
|
+
#state() {
|
|
208
|
+
return {pageId: this.#app.pageId, ready: this.#app.state === 'started',
|
|
209
|
+
channels: Object.fromEntries([...this.#channels].map(([name, channel]) => [name, channel.state]))};
|
|
147
210
|
}
|
|
148
|
-
#
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
211
|
+
#hello(peer) { this.#send(peer, {kind: 'hello', ...this.#state()}); }
|
|
212
|
+
#announce(peer) { this.#send(peer, {kind: 'ready', recipient: peer.session, ...this.#state()}); }
|
|
213
|
+
#send(peer, message) {
|
|
214
|
+
peer.window.postMessage({type: TYPE, session: this.#session, recipient: peer.session, ...message},
|
|
215
|
+
peer.origin === 'null' ? '*' : peer.origin);
|
|
152
216
|
}
|
|
153
217
|
#child(frame) {
|
|
154
218
|
if (frame?.localName !== 'iframe' || !frame.isConnected || !frame.contentWindow) return null;
|
|
155
219
|
const node = this.#app.dom.getBaseSourceNode(frame);
|
|
156
|
-
if (!node || !this.#app.src.binding.bindingFor(node)
|
|
157
|
-
|| this.#app.dom.getDomNode(node) !== frame) return null;
|
|
220
|
+
if (!node || !this.#app.src.binding.bindingFor(node) || this.#app.dom.getDomNode(node) !== frame) return null;
|
|
158
221
|
const sandbox = frame.getAttribute('sandbox');
|
|
159
222
|
const opaque = sandbox !== null && !sandbox.split(/\s+/).includes('allow-same-origin');
|
|
160
223
|
const origin = opaque ? 'null' : this.#origin(frame.hasAttribute('srcdoc')
|
|
161
224
|
? this.#document.baseURI : frame.getAttribute('src') || this.#document.baseURI);
|
|
162
|
-
return {window: frame.contentWindow, origin, node};
|
|
225
|
+
return {window: frame.contentWindow, origin, node, parent: false, kind: 'iframe'};
|
|
163
226
|
}
|
|
164
227
|
#origin(address) {
|
|
165
228
|
const url = new URL(address, this.#document.baseURI);
|
|
166
|
-
// Chromium's URL.origin can be file://, while MessageEvent.origin is null.
|
|
167
229
|
return url.protocol === 'file:' ? 'null' : url.origin;
|
|
168
230
|
}
|
|
169
231
|
#peer(window) {
|
|
170
|
-
const parent = this
|
|
232
|
+
const parent = this.parent;
|
|
171
233
|
if (parent?.window === window) return parent;
|
|
172
234
|
for (const frame of this.#document.getElementsByTagName('iframe')) {
|
|
173
235
|
const child = this.#child(frame);
|
|
174
236
|
if (child?.window === window) return child;
|
|
175
237
|
}
|
|
238
|
+
// Only an actual same-origin opener relationship can enroll a popup.
|
|
239
|
+
try {
|
|
240
|
+
if (window && window !== this.#window && window.opener === this.#window
|
|
241
|
+
&& window.location.origin === this.#window.location.origin) {
|
|
242
|
+
return {window, origin: this.#origin(this.#document.baseURI), parent: false, kind: 'popup'};
|
|
243
|
+
}
|
|
244
|
+
} catch { /* Cross-origin windows cannot enroll as popups. */ }
|
|
176
245
|
return null;
|
|
177
246
|
}
|
|
178
247
|
}
|
package/src/gramlot.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ import type {UtilitiesHandler} from './handlers/utilities-handler.js';
|
|
|
19
19
|
|
|
20
20
|
/** The options of a `Gramlot` instance. */
|
|
21
21
|
export interface GramlotOptions {
|
|
22
|
+
/** Experimental shared family WebSocket for PageStore synchronization and asynchronous RPC. */
|
|
23
|
+
websocketUrl?: string | null;
|
|
22
24
|
/** Formatting locale of this page; null/omitted uses the browser locale. */
|
|
23
25
|
locale?: string | null;
|
|
24
26
|
/** Declared device suitability; empty means unspecified. */
|
|
@@ -70,7 +72,7 @@ export interface FormatOptions {
|
|
|
70
72
|
|
|
71
73
|
/** Legacy-shaped publication. Frame forwarding follows synchronous local delivery. */
|
|
72
74
|
export type Publication = string | {topic: string; nodeId: string} |
|
|
73
|
-
{topic: string; parent?: boolean; iframe?: string | SourceBagNode};
|
|
75
|
+
{topic: string; parent?: boolean; iframe?: string | SourceBagNode; pageId?: string; server?: boolean};
|
|
74
76
|
|
|
75
77
|
/** One Gramlot page: lifecycle, Data and the four proxies `src`, `rpc`, `dom`, `utl`. */
|
|
76
78
|
export class Gramlot {
|
|
@@ -110,9 +112,10 @@ export class Gramlot {
|
|
|
110
112
|
/** Create the page instance; the construction order is the one of the source plan. */
|
|
111
113
|
constructor(options?: GramlotOptions);
|
|
112
114
|
/** Publish one named payload; forwarding uses postMessage and requires a ready page. */
|
|
113
|
-
publish(topic:
|
|
115
|
+
publish(topic: {topic: string; server: true; pageId?: string}, payload?: Record<string, unknown>): Promise<'ok'>;
|
|
116
|
+
publish(topic: Publication, payload?: Record<string, unknown>): void | Promise<'ok'>;
|
|
114
117
|
/** Subscribe to a local page topic, synchronously in registration order. */
|
|
115
|
-
subscribe(topic: string, callback: (payload: Record<string, unknown
|
|
118
|
+
subscribe(topic: string, callback: (payload: Record<string, unknown>, metadata: {pageId?: string}) => unknown): SubscriptionHandle;
|
|
116
119
|
/** Remove a registration; repeating removal of an owned handle is harmless. */
|
|
117
120
|
unsubscribe(registration: SubscriptionHandle): void;
|
|
118
121
|
/** Format a number, Date or null. Explicit dtype overrides TYTX-style Date inference. */
|
package/src/gramlot.js
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
import {MobileEnvironment} from './mobile-environment.js';
|
|
8
8
|
import {CommandRegistry} from './commands.js';
|
|
9
9
|
import {ServerAdapter} from './server-adapter.js';
|
|
10
|
+
import {PageStoreConnection} from './page-store-connection.js';
|
|
11
|
+
import {FrameMessages} from './frame-messages.js';
|
|
10
12
|
import {NAME_SYNTAX, NameSyntax} from './builder/name-syntax.js';
|
|
11
13
|
import {SourceHandler} from './handlers/source-handler.js';
|
|
12
14
|
import {RpcHandler} from './handlers/rpc-handler.js';
|
|
@@ -28,7 +30,8 @@ export class Gramlot {
|
|
|
28
30
|
#mobileEnvironment;
|
|
29
31
|
constructor({pageId, rpcUrl = '/gramlot/rpc', closeUrl = '/gramlot/close', rootId = 'gramlot-root', capabilities = [],
|
|
30
32
|
element = null, document = element?.ownerDocument ?? globalThis.document, transport = null, collections = [],
|
|
31
|
-
locale = null, timeZone = null, devices = [], serverAdapter = new ServerAdapter(), nameSyntax = null
|
|
33
|
+
locale = null, timeZone = null, devices = [], serverAdapter = new ServerAdapter(), nameSyntax = null,
|
|
34
|
+
websocketUrl = null} = {}) {
|
|
32
35
|
buttons.register(document.defaultView);
|
|
33
36
|
for (const collection of collections) {
|
|
34
37
|
if (collection instanceof ComponentCollection) collection.register(document.defaultView);
|
|
@@ -48,12 +51,14 @@ export class Gramlot {
|
|
|
48
51
|
this.#mobileEnvironment = MobileEnvironment.install({document,root:this.src.renderer.destination});
|
|
49
52
|
this.data = this.src.builder.data;
|
|
50
53
|
this.dvc = new DeviceHandler(this);
|
|
51
|
-
this.rpc = new RpcHandler(this, {rpcUrl, closeUrl, document, transport});
|
|
52
54
|
this.dom = new DomHandler(this);
|
|
53
55
|
this.utl = new UtilitiesHandler(this, document);
|
|
54
56
|
this.state = 'ready';
|
|
55
|
-
this._subscriptions.attach(document);
|
|
56
57
|
this.abort = new AbortController();
|
|
58
|
+
Object.defineProperty(this, '_frames', {value: new FrameMessages(this, document)});
|
|
59
|
+
if (websocketUrl !== null) this._pageStore = new PageStoreConnection(this, websocketUrl, document);
|
|
60
|
+
this.rpc = new RpcHandler(this, {rpcUrl, closeUrl, document, transport});
|
|
61
|
+
this._frames.connect();
|
|
57
62
|
// A page with a transport closes its server page when the browser leaves it (a beacon over HTTP).
|
|
58
63
|
if (this.rpc.transport) {
|
|
59
64
|
this.pagehide = event => { if (!event.persisted) this.dispose(); };
|
|
@@ -106,6 +111,8 @@ export class Gramlot {
|
|
|
106
111
|
try { this.src.binding.dispose(); }
|
|
107
112
|
finally { this.src.renderer.dispose(); }
|
|
108
113
|
} finally {
|
|
114
|
+
this._pageStore?.close();
|
|
115
|
+
this._frames.dispose();
|
|
109
116
|
if (this.rpc.transport) {
|
|
110
117
|
try { this.rpc.transport.close(this.pageId); }
|
|
111
118
|
finally { this.rpc.transport.dispose(); }
|