@gramlot/gramlot 0.2.15 → 0.2.17
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/assets/branding/app-icon.png +0 -0
- package/dist/gramlot.js +1223 -219
- package/dist/gramlot.min.js +77 -20
- package/package.json +8 -5
- package/src/binding/installation.js +15 -5
- package/src/binding/providers.js +63 -6
- package/src/binding/remote.js +77 -0
- package/src/binding/rpc-resolver.js +93 -0
- package/src/binding/runtime.js +34 -9
- package/src/builder/binding.json +76 -0
- package/src/builder/gramlot-builder.js +16 -4
- package/src/builder/source.js +35 -3
- package/src/builder/webcomponent.json +51 -0
- package/src/handlers/source-handler.d.ts +1 -1
- package/src/handlers/source-handler.js +1 -1
- package/src/index-page-runtime.d.ts +11 -0
- package/src/index-page-runtime.js +287 -0
- package/src/index-page-styles.js +2 -0
- package/src/index-theme.js +5 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +3 -0
- package/src/inout.js +3 -1
- package/src/renderer/attributes.js +13 -1
- package/src/renderer/gramlot-html-renderer.js +9 -2
- package/src/renderer/gramlot-renderer.js +2 -1
- package/src/renderer/gramlot-svg-renderer.js +2 -1
- package/src/server/conformance.js +22 -2
- package/src/server/gramlot-server.d.ts +1 -0
- package/src/server/gramlot-server.js +12 -0
- package/src/server/index-page.d.ts +14 -0
- package/src/server/index-page.js +102 -0
- package/src/server/page-base.js +88 -0
- package/src/server/page.d.ts +8 -2
- package/src/server/page.js +3 -76
- package/src/view/html.js +2 -1
- package/src/webcomponent.js +13 -0
- package/themes/gramlot-base/README.md +8 -0
- package/themes/gramlot-compact/README.md +49 -0
- package/themes/gramlot-compact/theme.css +57 -0
- package/themes/gramlot-index/theme.css +51 -0
- package/tools/gramlot-tools-inspector.js +1460 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gramlot/gramlot",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.17",
|
|
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": {
|
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
"prepack": "node scripts/build-runtime.mjs"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@genrojs/bag": ">=0.
|
|
20
|
-
"@genrojs/builders": ">=0.
|
|
19
|
+
"@genrojs/bag": ">=0.11.0",
|
|
20
|
+
"@genrojs/builders": ">=0.5.2",
|
|
21
21
|
"@genrojs/tytx": ">=0.16.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
@@ -32,7 +32,9 @@
|
|
|
32
32
|
"./page": "./src/server/page.js",
|
|
33
33
|
"./gramlot-server": "./src/server/gramlot-server.js",
|
|
34
34
|
"./themes/*": "./themes/*",
|
|
35
|
-
"./assets/branding/*": "./assets/branding/*"
|
|
35
|
+
"./assets/branding/*": "./assets/branding/*",
|
|
36
|
+
"./tools/inspector": "./tools/gramlot-tools-inspector.js",
|
|
37
|
+
"./index-page-runtime": "./src/index-page-runtime.js"
|
|
36
38
|
},
|
|
37
39
|
"files": [
|
|
38
40
|
"LICENSE",
|
|
@@ -40,7 +42,8 @@
|
|
|
40
42
|
"src",
|
|
41
43
|
"dist",
|
|
42
44
|
"themes",
|
|
43
|
-
"assets"
|
|
45
|
+
"assets",
|
|
46
|
+
"tools"
|
|
44
47
|
],
|
|
45
48
|
"engines": {
|
|
46
49
|
"node": ">=22"
|
|
@@ -169,18 +169,20 @@ export function requireInstallable(branch) {
|
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
// Provider attributes (§4.8): only a `dataFormula`, a `dataController` or a `
|
|
172
|
+
// Provider attributes (§4.8): only a `dataFormula`, a `dataController`, a `dataRpc` or a `dataRemote`
|
|
173
|
+
// carries them; the lifecycle attributes not a `dataRemote`, whose resolver loads when its Data is read;
|
|
173
174
|
// the callbacks of the call only a `dataRpc`.
|
|
174
175
|
const PROVIDER_ATTRIBUTES = ['_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges'];
|
|
175
|
-
const PROVIDER_TAGS = new Set(['dataFormula', 'dataController', 'dataRpc']);
|
|
176
|
+
const PROVIDER_TAGS = new Set(['dataFormula', 'dataController', 'dataRpc', 'dataRemote']);
|
|
176
177
|
const RPC_ATTRIBUTES = ['_onCalling', '_onResult', '_onError'];
|
|
177
178
|
// The lifecycle attributes: true, false, null or a number (a delay in ms, P22); a pointer is not read.
|
|
178
179
|
const LIFECYCLE_ATTRIBUTES = ['_init', '_onBuilt', '_onStart'];
|
|
179
180
|
|
|
180
181
|
/**
|
|
181
182
|
* A provider attribute on any other node is an error before any write, and so is an rpc callback
|
|
182
|
-
* outside a `dataRpc`; so is a `==` expression in an
|
|
183
|
-
* to the visual nodes), and a lifecycle attribute
|
|
183
|
+
* outside a `dataRpc` and a lifecycle attribute on a `dataRemote`; so is a `==` expression in an
|
|
184
|
+
* attribute of a provider node (S09: `==` belongs to the visual nodes), and a lifecycle attribute
|
|
185
|
+
* whose value is not `true`, `false`, null or a number.
|
|
184
186
|
*/
|
|
185
187
|
function requireProviderAttributes(node) {
|
|
186
188
|
const isSet = name => node.getAttr(name) !== null && node.getAttr(name) !== undefined;
|
|
@@ -193,6 +195,11 @@ function requireProviderAttributes(node) {
|
|
|
193
195
|
if (expression) {
|
|
194
196
|
throw new Error(`${node.nodeTag} '${node.label}': '${expression[0]}' is a == expression, not accepted on ${node.nodeTag}`);
|
|
195
197
|
}
|
|
198
|
+
const lifecycle = node.nodeTag === 'dataRemote' ? LIFECYCLE_ATTRIBUTES.find(isSet) : null;
|
|
199
|
+
if (lifecycle) {
|
|
200
|
+
throw new Error(`dataRemote '${node.label}': '${lifecycle}' is not accepted on dataRemote, `
|
|
201
|
+
+ 'its resolver loads when the Data is read');
|
|
202
|
+
}
|
|
196
203
|
for (const name of LIFECYCLE_ATTRIBUTES) {
|
|
197
204
|
const value = node.getAttr(name);
|
|
198
205
|
if (value === null || value === undefined || typeof value === 'boolean' || typeof value === 'number') continue;
|
|
@@ -202,7 +209,10 @@ function requireProviderAttributes(node) {
|
|
|
202
209
|
return;
|
|
203
210
|
}
|
|
204
211
|
const name = PROVIDER_ATTRIBUTES.find(isSet);
|
|
205
|
-
if (name)
|
|
212
|
+
if (!name) return;
|
|
213
|
+
const tags = LIFECYCLE_ATTRIBUTES.includes(name) ? 'dataFormula, dataController and dataRpc'
|
|
214
|
+
: 'dataFormula, dataController, dataRpc and dataRemote';
|
|
215
|
+
throw new Error(`${node.nodeTag ?? 'fragment'} '${node.label}': '${name}' is allowed only on ${tags}`);
|
|
206
216
|
}
|
|
207
217
|
|
|
208
218
|
/** The nodes of `branch` (a SourceBagNode or a SourceBag), depth first, in document order. */
|
package/src/binding/providers.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {Bag} from '@genrojs/bag';
|
|
2
|
+
import {RpcResolver} from './rpc-resolver.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Providers of one Gramlot page (source plan §4.8): the semantic runtime of a `dataFormula`, a
|
|
5
|
-
* `dataController` or a `
|
|
6
|
+
* `dataController`, a `dataRpc` or a `dataRemote`, owned by its NodeBinding.
|
|
6
7
|
*
|
|
7
8
|
* One owner per trigger (B7): the router delivers the Data changes of the `^` pointers to
|
|
8
9
|
* `receive`; `_timing` is started by `register`; `_init`, `_onBuilt` and `_onStart` are invoked by
|
|
@@ -16,7 +17,7 @@ import {Bag} from '@genrojs/bag';
|
|
|
16
17
|
const CONTROL_ATTRIBUTES = new Set([
|
|
17
18
|
'destination_path', 'result_path', 'func', 'formula', 'script', 'if', 'else',
|
|
18
19
|
'_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges',
|
|
19
|
-
'method', 'timeout', '_onCalling', '_onResult', '_onError',
|
|
20
|
+
'method', 'timeout', '_onCalling', '_onResult', '_onError', 'cacheTime',
|
|
20
21
|
]);
|
|
21
22
|
|
|
22
23
|
export class Provider {
|
|
@@ -27,24 +28,30 @@ export class Provider {
|
|
|
27
28
|
#cancelDelay = null;
|
|
28
29
|
// The AbortController of the in-flight call of a `dataRpc`: a new call aborts it (latest wins).
|
|
29
30
|
#rpcCall = null;
|
|
31
|
+
// The RpcResolver a `dataRemote` installed and its absolute Data path.
|
|
32
|
+
#installed = null;
|
|
33
|
+
// The resolver removed last had loaded or was loading: the next one loads at once.
|
|
34
|
+
#reload = false;
|
|
30
35
|
|
|
31
36
|
constructor(binding) {
|
|
32
37
|
this.#binding = binding;
|
|
33
38
|
binding.track(() => {
|
|
34
39
|
this.cancel();
|
|
35
40
|
this.#closeRegistrations();
|
|
41
|
+
this.#uninstall();
|
|
36
42
|
});
|
|
37
43
|
}
|
|
38
44
|
|
|
39
45
|
get binding() { return this.#binding; }
|
|
40
46
|
get node() { return this.binding.node; }
|
|
41
|
-
/** 'formula' | 'controller' | 'rpc'. */
|
|
42
|
-
get kind() { throw new Error('Provider.kind is defined by FormulaProvider, ControllerProvider and
|
|
47
|
+
/** 'formula' | 'controller' | 'rpc' | 'remote'. */
|
|
48
|
+
get kind() { throw new Error('Provider.kind is defined by FormulaProvider, ControllerProvider, RpcProvider and RemoteProvider'); }
|
|
43
49
|
|
|
44
50
|
/**
|
|
45
51
|
* Step 4 of §5.1, and every rebinding: the `^` pointers are registered again from the current
|
|
46
52
|
* context (a pointer on a null path is not registered, Q7), and `_timing` starts again from its
|
|
47
|
-
* current value. A numeric `_onStart` must be a finite number not below zero (P22).
|
|
53
|
+
* current value. A numeric `_onStart` must be a finite number not below zero (P22). A `dataRemote`
|
|
54
|
+
* then installs its resolver with the current arguments.
|
|
48
55
|
*/
|
|
49
56
|
register() {
|
|
50
57
|
const node = this.node;
|
|
@@ -64,6 +71,7 @@ export class Provider {
|
|
|
64
71
|
if (name === '_timing') this.#timingRegistration = registration;
|
|
65
72
|
}
|
|
66
73
|
this.#startTiming();
|
|
74
|
+
if (this.kind === 'remote') this.invoke({kind: 'install', change: null});
|
|
67
75
|
}
|
|
68
76
|
|
|
69
77
|
/**
|
|
@@ -92,25 +100,30 @@ export class Provider {
|
|
|
92
100
|
* the inline `formula`/`script`, compiled by the page's InlineCompiler, or the named `func`. An
|
|
93
101
|
* error propagates to the caller (P12). A closed node runs nothing. The body of a `dataRpc` is the
|
|
94
102
|
* call: `invoke` returns its promise, and a false `_if` runs `_else` as inline code without calling.
|
|
103
|
+
* The body of a `dataRemote` is the installation of its resolver; a false `_if` removes it, the
|
|
104
|
+
* Data node keeping its last value, and runs `_else` as inline code.
|
|
95
105
|
*/
|
|
96
106
|
invoke(trigger) {
|
|
97
107
|
if (this.binding.closed) return;
|
|
98
108
|
const node = this.node;
|
|
99
109
|
const formula = this.kind === 'formula';
|
|
100
110
|
const rpc = this.kind === 'rpc';
|
|
111
|
+
const remote = this.kind === 'remote';
|
|
101
112
|
const kwargs = this.readArguments(trigger);
|
|
102
113
|
let [attr, body] = [formula ? 'formula' : 'script', node.getAttr(formula ? 'formula' : 'script')];
|
|
103
114
|
// Truthiness of `_if`, `_else` and the body, as legacy (`gnrdomsource.js:371-385, 473-482`).
|
|
104
115
|
const condition = node.getAttr('_if');
|
|
105
116
|
if (condition && !this.#inline('_if', condition, `return (${condition})`, kwargs, false)) {
|
|
117
|
+
if (remote) this.#uninstall();
|
|
106
118
|
[attr, body] = ['_else', node.getAttr('_else')];
|
|
107
119
|
if (!body) return;
|
|
108
|
-
if (rpc) {
|
|
120
|
+
if (rpc || remote) {
|
|
109
121
|
this.#inline(attr, body, body, kwargs, true);
|
|
110
122
|
return;
|
|
111
123
|
}
|
|
112
124
|
}
|
|
113
125
|
if (rpc) return this.#call(kwargs);
|
|
126
|
+
if (remote) return this.#install(kwargs);
|
|
114
127
|
const func = node.getAttr('func');
|
|
115
128
|
let result;
|
|
116
129
|
if (body) {
|
|
@@ -251,6 +264,45 @@ export class Provider {
|
|
|
251
264
|
if (onResult) this.#inline('_onResult', onResult, onResult, {...kwargs, result}, true);
|
|
252
265
|
}
|
|
253
266
|
|
|
267
|
+
/**
|
|
268
|
+
* The body of a `dataRemote`: a new RpcResolver on the Data node at `destination_path`, with the
|
|
269
|
+
* author arguments as params, in place of the previous one, so the next read loads again. When the
|
|
270
|
+
* resolver removed last, by this call or by a false `_if`, had loaded or was loading, the new one
|
|
271
|
+
* loads at once: its consumers receive the new value through the Data event without reading again.
|
|
272
|
+
* A failed load is thrown again naming the node, as an unhandled rejection. A null path (Q7)
|
|
273
|
+
* installs nothing.
|
|
274
|
+
*/
|
|
275
|
+
#install(kwargs) {
|
|
276
|
+
this.#uninstall();
|
|
277
|
+
const reload = this.#reload;
|
|
278
|
+
this.#reload = false;
|
|
279
|
+
const node = this.node;
|
|
280
|
+
const path = node.absDatapath(node.pathAttribute('destination_path'));
|
|
281
|
+
if (path === null) return;
|
|
282
|
+
const resolver = new RpcResolver({
|
|
283
|
+
gramlot: this.binding.runtime.gramlot, method: this.#resolved('method'),
|
|
284
|
+
params: authorArguments(kwargs), cacheTime: this.#resolved('cacheTime'),
|
|
285
|
+
onError: error => {
|
|
286
|
+
throw new Error(`${node.nodeTag} '${node.label}': dataRemote '${resolver.method}' failed: ${error.message}`,
|
|
287
|
+
{cause: error});
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
this.binding.runtime.data.setResolver(path, resolver);
|
|
291
|
+
this.#installed = {path, resolver};
|
|
292
|
+
if (reload) resolver.refresh();
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Remove the resolver this `dataRemote` installed, when it is still on its Data node, and abort its load. */
|
|
296
|
+
#uninstall() {
|
|
297
|
+
if (!this.#installed) return;
|
|
298
|
+
const {path, resolver} = this.#installed;
|
|
299
|
+
this.#installed = null;
|
|
300
|
+
this.#reload = resolver.loaded || resolver.loading !== null;
|
|
301
|
+
resolver.abort();
|
|
302
|
+
const dataNode = this.binding.runtime.data.getNode(path);
|
|
303
|
+
if (dataNode?.resolver === resolver) dataNode.resolver = null;
|
|
304
|
+
}
|
|
305
|
+
|
|
254
306
|
#closeRegistrations() {
|
|
255
307
|
for (const registration of this.#registrations.splice(0)) registration.close();
|
|
256
308
|
this.#timingRegistration = null;
|
|
@@ -272,6 +324,11 @@ export class RpcProvider extends Provider {
|
|
|
272
324
|
get kind() { return 'rpc'; }
|
|
273
325
|
}
|
|
274
326
|
|
|
327
|
+
/** The provider of a `dataRemote`: the body installs an RpcResolver, which calls its endpoint when the Data node is read. */
|
|
328
|
+
export class RemoteProvider extends Provider {
|
|
329
|
+
get kind() { return 'remote'; }
|
|
330
|
+
}
|
|
331
|
+
|
|
275
332
|
// The trigger fields of the kwargs, in the legacy order of the inline parameters.
|
|
276
333
|
const TRIGGER_FIELDS = ['_node', '_triggerpars', '_reason'];
|
|
277
334
|
// The fields a click adds (P9): locals of the inline bodies, never author arguments.
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {toTytx} from '@genrojs/tytx';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The `remote` attribute of a Source node (GC-095 §105): the content of the element is the fragment of a
|
|
5
|
+
* `@source` method, requested through `gramlot.src.remoteSource` when the node starts and again when a
|
|
6
|
+
* `^` pointer of `remote` or of a `remote_<k>` parameter changes. Owned by the node's NodeBinding; the
|
|
7
|
+
* request is cancelled with it by `remoteSource` itself (P24), and the answer of a superseded request is
|
|
8
|
+
* dropped there (latest wins).
|
|
9
|
+
*
|
|
10
|
+
* The control attributes: `_cachedRemote` (no call when name and parameters equal the last mount's), `_if`
|
|
11
|
+
* (inline, false → no call, and `_else` runs when present), `_onRemote` (inline, after the fragment is
|
|
12
|
+
* mounted). The inline code has the parameters as locals and `this` = the node.
|
|
13
|
+
*/
|
|
14
|
+
export class RemoteBinding {
|
|
15
|
+
#binding;
|
|
16
|
+
// The name and TYTX parameters of the last mounted call, captured before sending it.
|
|
17
|
+
#lastName = null;
|
|
18
|
+
#lastParams = null;
|
|
19
|
+
|
|
20
|
+
constructor(binding) {
|
|
21
|
+
this.#binding = binding;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get binding() { return this.#binding; }
|
|
25
|
+
get node() { return this.binding.node; }
|
|
26
|
+
get closed() { return this.binding.closed; }
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Request the fragment for the current parameters. Nothing happens before the node's `start` step,
|
|
30
|
+
* on a closed node, without a name, or when `_if` or `_cachedRemote` stop the call; a false `_if`
|
|
31
|
+
* also cancels the request in flight, so its fragment is not mounted after `_else`. A cache hit
|
|
32
|
+
* cancels a pending request too, keeping the mounted fragment. A failure of the request rejects
|
|
33
|
+
* with an error naming the node, its cause attached.
|
|
34
|
+
*/
|
|
35
|
+
async update() {
|
|
36
|
+
if (this.closed || !this.binding.hasStamp('start')) return;
|
|
37
|
+
const node = this.node;
|
|
38
|
+
const [, attrs] = node.builder.runtimeValues(node);
|
|
39
|
+
const name = attrs.remote;
|
|
40
|
+
if (!name) return;
|
|
41
|
+
const params = {};
|
|
42
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
43
|
+
if (key.startsWith('remote_')) params[key.slice('remote_'.length)] = value;
|
|
44
|
+
}
|
|
45
|
+
const condition = node.getAttr('_if');
|
|
46
|
+
if (condition && !this.#inline('_if', condition, `return (${condition})`, params)) {
|
|
47
|
+
this.binding.runtime.gramlot.rpc.remoteRequests.get(node)?.controller.abort();
|
|
48
|
+
const otherwise = node.getAttr('_else');
|
|
49
|
+
if (otherwise) this.#inline('_else', otherwise, otherwise, params);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
let mounted;
|
|
53
|
+
let text;
|
|
54
|
+
try {
|
|
55
|
+
text = toTytx(params);
|
|
56
|
+
if (attrs._cachedRemote && name === this.#lastName && text === this.#lastParams) {
|
|
57
|
+
// Keeping the mounted fragment also supersedes any different request still in flight.
|
|
58
|
+
this.binding.runtime.gramlot.rpc.remoteRequests.get(node)?.controller.abort();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
mounted = await this.binding.runtime.gramlot.src.remoteSource(node, name, params);
|
|
62
|
+
} catch (error) {
|
|
63
|
+
throw new Error(`${node.nodeTag} '${node.label}': remote '${name}' failed: ${error.message}`, {cause: error});
|
|
64
|
+
}
|
|
65
|
+
// Only a mounted fragment is the last call `_cachedRemote` compares: a failed request is retried.
|
|
66
|
+
if (mounted) {
|
|
67
|
+
this.#lastName = name;
|
|
68
|
+
this.#lastParams = text;
|
|
69
|
+
}
|
|
70
|
+
const onRemote = node.getAttr('_onRemote');
|
|
71
|
+
if (mounted && onRemote) this.#inline('_onRemote', onRemote, onRemote, params);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
#inline(attr, code, source, params) {
|
|
75
|
+
return this.binding.runtime.inlineCompiler.compile(this.node, attr, code, Object.keys(params), source)(params);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {BagResolver} from '@genrojs/bag';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The resolver a `dataRemote` installs on its Data node: the value is the answer of the endpoint
|
|
5
|
+
* `method`, called through `gramlot.rpc.call('data', method, params)` when the node is read and its
|
|
6
|
+
* cache has expired. `cacheTime` in seconds, as legacy: below 0 once, 0 at every read, above 0 again
|
|
7
|
+
* after that many seconds.
|
|
8
|
+
*
|
|
9
|
+
* A read never waits: it returns the node's current value (null before the first load) and, when the
|
|
10
|
+
* cache has expired, starts the load. A read made while a load is in flight starts no other load. The
|
|
11
|
+
* loaded value is written on the node with its Data event, so every `^` consumer of the path is
|
|
12
|
+
* projected again; with `cacheTime: 0` it is written without the event, as legacy: a value per
|
|
13
|
+
* explicit read, never a source for `^` consumers, whose projection would read and load again.
|
|
14
|
+
* A failed load writes nothing: `loading` rejects with its error and `onError` receives it; an aborted
|
|
15
|
+
* load writes nothing, even when its answer had already arrived. `abort()` cancels the pending load.
|
|
16
|
+
*/
|
|
17
|
+
export class RpcResolver extends BagResolver {
|
|
18
|
+
static classKwargs = {...BagResolver.classKwargs, asBag: false};
|
|
19
|
+
static internalParams = new Set([...BagResolver.internalParams, 'gramlot', 'method', 'params', 'onError']);
|
|
20
|
+
|
|
21
|
+
constructor({gramlot, method, params, cacheTime, onError}) {
|
|
22
|
+
// null: resolve once, as a negative cacheTime.
|
|
23
|
+
super({cacheTime: cacheTime ?? -1});
|
|
24
|
+
this.gramlot = gramlot;
|
|
25
|
+
this.method = method;
|
|
26
|
+
this.params = params;
|
|
27
|
+
// Receives the error of a failed load, never an abort.
|
|
28
|
+
this.onError = onError;
|
|
29
|
+
// The AbortController and the promise of the pending load, or null.
|
|
30
|
+
this.controller = null;
|
|
31
|
+
this.loading = null;
|
|
32
|
+
// A load has written its value; the loaded value is being written.
|
|
33
|
+
this.loaded = false;
|
|
34
|
+
this.writing = false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The current value; a non-static read starts the load when the cache has expired. */
|
|
38
|
+
resolve({static: isStatic = false} = {}) {
|
|
39
|
+
if (!isStatic && !this.loading && !this.writing && this.expired) this.refresh();
|
|
40
|
+
return this.cachedValue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Start a load and write its value on the node, with the Data event unless `cacheTime` is 0; returns `loading`. */
|
|
44
|
+
refresh() {
|
|
45
|
+
const settle = () => {
|
|
46
|
+
if (this.loading !== loading) return;
|
|
47
|
+
this.controller = null;
|
|
48
|
+
this.loading = null;
|
|
49
|
+
};
|
|
50
|
+
const load = this.load();
|
|
51
|
+
const {signal} = this.controller;
|
|
52
|
+
const loading = load.then(value => {
|
|
53
|
+
settle();
|
|
54
|
+
// Aborted after the answer arrived: the answer belongs to a removed or replaced resolver.
|
|
55
|
+
if (signal.aborted) throw signal.reason;
|
|
56
|
+
this._lastUpdate = Date.now();
|
|
57
|
+
this.loaded = true;
|
|
58
|
+
this.writing = true;
|
|
59
|
+
try {
|
|
60
|
+
if (this._node) this._node.setValue(value, this.cacheTime !== 0);
|
|
61
|
+
else this.cachedValue = value;
|
|
62
|
+
} finally {
|
|
63
|
+
this.writing = false;
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}, error => {
|
|
67
|
+
settle();
|
|
68
|
+
throw error;
|
|
69
|
+
});
|
|
70
|
+
this.loading = loading;
|
|
71
|
+
// A load nobody awaits: its failure reaches `onError`, an abort is not a failure.
|
|
72
|
+
loading.catch(error => {
|
|
73
|
+
if (error?.name !== 'AbortError') this.onError(error);
|
|
74
|
+
});
|
|
75
|
+
return loading;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The call of the endpoint with the params given at construction; the read kwargs are not sent. */
|
|
79
|
+
load() {
|
|
80
|
+
const controller = new AbortController();
|
|
81
|
+
const aborted = new Promise((_, reject) => {
|
|
82
|
+
controller.signal.addEventListener('abort', () => reject(controller.signal.reason), {once: true});
|
|
83
|
+
});
|
|
84
|
+
const call = this.gramlot.rpc.call('data', this.method, this.params, {signal: controller.signal});
|
|
85
|
+
this.controller = controller;
|
|
86
|
+
return Promise.race([call, aborted]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Cancel the pending load, if any. */
|
|
90
|
+
abort() {
|
|
91
|
+
this.controller?.abort();
|
|
92
|
+
}
|
|
93
|
+
}
|
package/src/binding/runtime.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import {SourceBag} from '@genrojs/builders';
|
|
2
2
|
import {DataRouter} from './router.js';
|
|
3
3
|
import {DataInstaller} from './installation.js';
|
|
4
|
-
import {ControllerProvider, FormulaProvider, RpcProvider} from './providers.js';
|
|
4
|
+
import {ControllerProvider, FormulaProvider, RemoteProvider, RpcProvider} from './providers.js';
|
|
5
5
|
import {InlineCompiler} from './inline.js';
|
|
6
|
+
import {RemoteBinding} from './remote.js';
|
|
6
7
|
|
|
7
8
|
// The Provider class of each provider data-element (§4.8).
|
|
8
|
-
const PROVIDERS = {
|
|
9
|
+
const PROVIDERS = {
|
|
10
|
+
dataFormula: FormulaProvider, dataController: ControllerProvider, dataRpc: RpcProvider,
|
|
11
|
+
dataRemote: RemoteProvider,
|
|
12
|
+
};
|
|
9
13
|
|
|
10
14
|
// Attributes whose change moves the Data context of a whole branch (§4.4, upd_attrs row).
|
|
11
15
|
const CONTEXT_ATTRIBUTES = ['datapath', '_anchor', 'node_id', 'form', 'formId'];
|
|
@@ -26,7 +30,8 @@ export class BindingRuntime {
|
|
|
26
30
|
#router;
|
|
27
31
|
#installer;
|
|
28
32
|
#inlineCompiler;
|
|
29
|
-
#subscription = `gramlot-data-${crypto.
|
|
33
|
+
#subscription = `gramlot-data-${Array.from(crypto.getRandomValues(new Uint8Array(16)),
|
|
34
|
+
byte => byte.toString(16).padStart(2, '0')).join('')}`;
|
|
30
35
|
#disposed = false;
|
|
31
36
|
|
|
32
37
|
constructor(gramlot) {
|
|
@@ -100,12 +105,15 @@ export class BindingRuntime {
|
|
|
100
105
|
|
|
101
106
|
/**
|
|
102
107
|
* Redo the registrations of `node` and of its descendants (change of Data context), then
|
|
103
|
-
* project the branch again: the elements already built follow the new paths
|
|
108
|
+
* project the branch again: the elements already built follow the new paths, and a started
|
|
109
|
+
* `remote` requests its fragment for the new context. Not in `rebind`: the fragment mounted by
|
|
110
|
+
* `remoteSource` is a value event of the node itself, which rebinds it.
|
|
104
111
|
*/
|
|
105
112
|
rebindBranch(node) {
|
|
106
113
|
const nodes = this.#branchNodes(node);
|
|
107
114
|
for (const each of nodes) this.bindingFor(each)?.rebind();
|
|
108
115
|
for (const each of nodes) this.renderer.project(each);
|
|
116
|
+
for (const each of nodes) this.bindingFor(each)?.remote?.update();
|
|
109
117
|
}
|
|
110
118
|
|
|
111
119
|
/**
|
|
@@ -248,11 +256,12 @@ export class BindingRuntime {
|
|
|
248
256
|
}
|
|
249
257
|
}
|
|
250
258
|
|
|
251
|
-
/** The `_onStart` of a built node, once. */
|
|
259
|
+
/** The `_onStart` of a built node, then the first request of its `remote`, once. */
|
|
252
260
|
#start(binding) {
|
|
253
261
|
if (binding.hasStamp('start')) return;
|
|
254
262
|
binding.stamp('start');
|
|
255
263
|
this.#lifecycle(binding, 'start', '_onStart');
|
|
264
|
+
binding.remote?.update();
|
|
256
265
|
}
|
|
257
266
|
|
|
258
267
|
/** Invoke the providers of `binding` for the lifecycle trigger `kind` when `attribute` is on; a number delays it. */
|
|
@@ -330,6 +339,9 @@ export class NodeBinding {
|
|
|
330
339
|
#registrations = [];
|
|
331
340
|
#stamps = new Set();
|
|
332
341
|
#provider = null;
|
|
342
|
+
#remote = null;
|
|
343
|
+
// The registrations of `remote` and of the `remote_<k>` parameters: their changes request the fragment again.
|
|
344
|
+
#remoteRegistrations = new Set();
|
|
333
345
|
// The clicks of a button, counted for its whole semantic life: a rebuild keeps them (P9, S12).
|
|
334
346
|
#clickCount = 0;
|
|
335
347
|
// The registration of the node's own variable datapath, read in the parent's context.
|
|
@@ -345,8 +357,10 @@ export class NodeBinding {
|
|
|
345
357
|
get closed() { return this.#closed; }
|
|
346
358
|
/** The active DataRegistrations of the node. */
|
|
347
359
|
get registrations() { return [...this.#registrations]; }
|
|
348
|
-
/** The Provider of a `dataFormula`, `dataController` or `
|
|
360
|
+
/** The Provider of a `dataFormula`, `dataController`, `dataRpc` or `dataRemote`, as a list of zero or one. */
|
|
349
361
|
get providers() { return this.#provider ? [this.#provider] : []; }
|
|
362
|
+
/** The RemoteBinding of a node whose `remote` attribute is a string, or null. */
|
|
363
|
+
get remote() { return this.#remote; }
|
|
350
364
|
|
|
351
365
|
/** The clicks counted by the node's ButtonBinding since the node entered the Source (P9). */
|
|
352
366
|
get clickCount() { return this.#clickCount; }
|
|
@@ -371,11 +385,13 @@ export class NodeBinding {
|
|
|
371
385
|
* pointer on a null path (Q7) is not registered. The `datapath` pointer is resolved in the
|
|
372
386
|
* parent's context. A `dataSetter` registers nothing; a `dataFormula` or `dataController`
|
|
373
387
|
* registers its `datapath` here and the rest through its Provider, created once (§5.1 step 4).
|
|
388
|
+
* A node whose `remote` is a string gets its RemoteBinding, created once.
|
|
374
389
|
*/
|
|
375
390
|
registerPointers() {
|
|
376
391
|
const node = this.node;
|
|
377
392
|
const dataElement = node._getMeta('data_element');
|
|
378
393
|
if (dataElement && !PROVIDERS[node.nodeTag]) return;
|
|
394
|
+
if (!dataElement && typeof node.getAttr('remote') === 'string') this.#remote ??= new RemoteBinding(this);
|
|
379
395
|
for (const [name, pointer] of node.pointers()) {
|
|
380
396
|
if (dataElement && name !== 'datapath') continue;
|
|
381
397
|
const context = name === 'datapath' ? node.parentNode : node;
|
|
@@ -385,6 +401,7 @@ export class NodeBinding {
|
|
|
385
401
|
const registration = this.runtime.router.register({path, attr, recipient: this});
|
|
386
402
|
this.#registrations.push(registration);
|
|
387
403
|
if (name === 'datapath') this.#datapathRegistration = registration;
|
|
404
|
+
if (this.remote && (name === 'remote' || name.startsWith('remote_'))) this.#remoteRegistrations.add(registration);
|
|
388
405
|
}
|
|
389
406
|
if (!dataElement) return;
|
|
390
407
|
this.#provider ??= new PROVIDERS[node.nodeTag](this);
|
|
@@ -397,10 +414,17 @@ export class NodeBinding {
|
|
|
397
414
|
this.registerPointers();
|
|
398
415
|
}
|
|
399
416
|
|
|
400
|
-
/**
|
|
417
|
+
/**
|
|
418
|
+
* Router recipient: the node's datapath rebinds its branch; any other registration projects the node,
|
|
419
|
+
* after a registration of `remote` or of a `remote_<k>` parameter requested the fragment again.
|
|
420
|
+
*/
|
|
401
421
|
receive(change) {
|
|
402
|
-
if (change.registration === this.#datapathRegistration)
|
|
403
|
-
|
|
422
|
+
if (change.registration === this.#datapathRegistration) {
|
|
423
|
+
this.runtime.rebindBranch(this.node);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (this.#remoteRegistrations.has(change.registration)) this.remote.update();
|
|
427
|
+
this.runtime.renderer.project(this.node, change);
|
|
404
428
|
}
|
|
405
429
|
|
|
406
430
|
/** Own a semantic resource until close; the returned function releases it without running it. */
|
|
@@ -428,5 +452,6 @@ export class NodeBinding {
|
|
|
428
452
|
#closeRegistrations() {
|
|
429
453
|
for (const registration of this.#registrations.splice(0)) registration.close();
|
|
430
454
|
this.#datapathRegistration = null;
|
|
455
|
+
this.#remoteRegistrations.clear();
|
|
431
456
|
}
|
|
432
457
|
}
|
package/src/builder/binding.json
CHANGED
|
@@ -298,6 +298,82 @@
|
|
|
298
298
|
"_meta": {
|
|
299
299
|
"data_element": true
|
|
300
300
|
}
|
|
301
|
+
},
|
|
302
|
+
"dataRemote": {
|
|
303
|
+
"doc": "Data node resolved by a server endpoint through gramlot.rpc, on first read and again when cacheTime expires.",
|
|
304
|
+
"sub_tags": "",
|
|
305
|
+
"parent_tags": null,
|
|
306
|
+
"inherits_from": null,
|
|
307
|
+
"ns": null,
|
|
308
|
+
"attributes": {
|
|
309
|
+
"parameters": [
|
|
310
|
+
{
|
|
311
|
+
"name": "destination_path",
|
|
312
|
+
"kind": "positional_or_keyword",
|
|
313
|
+
"role": "attribute",
|
|
314
|
+
"annotation": {
|
|
315
|
+
"kind": "type",
|
|
316
|
+
"module": "builtins",
|
|
317
|
+
"name": "str"
|
|
318
|
+
},
|
|
319
|
+
"has_default": false
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
"name": "method",
|
|
323
|
+
"kind": "positional_or_keyword",
|
|
324
|
+
"role": "attribute",
|
|
325
|
+
"annotation": {
|
|
326
|
+
"kind": "type",
|
|
327
|
+
"module": "builtins",
|
|
328
|
+
"name": "str"
|
|
329
|
+
},
|
|
330
|
+
"has_default": false
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
"name": "cacheTime",
|
|
334
|
+
"kind": "positional_or_keyword",
|
|
335
|
+
"role": "attribute",
|
|
336
|
+
"annotation": {
|
|
337
|
+
"kind": "union",
|
|
338
|
+
"items": [
|
|
339
|
+
{
|
|
340
|
+
"kind": "type",
|
|
341
|
+
"module": "builtins",
|
|
342
|
+
"name": "int"
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
"kind": "type",
|
|
346
|
+
"module": "builtins",
|
|
347
|
+
"name": "float"
|
|
348
|
+
},
|
|
349
|
+
{
|
|
350
|
+
"kind": "type",
|
|
351
|
+
"module": "builtins",
|
|
352
|
+
"name": "NoneType"
|
|
353
|
+
}
|
|
354
|
+
]
|
|
355
|
+
},
|
|
356
|
+
"has_default": true,
|
|
357
|
+
"default": null
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
"name": "params",
|
|
361
|
+
"kind": "var_keyword",
|
|
362
|
+
"role": "attribute",
|
|
363
|
+
"annotation": {
|
|
364
|
+
"kind": "any"
|
|
365
|
+
},
|
|
366
|
+
"has_default": false
|
|
367
|
+
}
|
|
368
|
+
],
|
|
369
|
+
"accepts_var_keyword": true,
|
|
370
|
+
"accepts_var_positional": false
|
|
371
|
+
},
|
|
372
|
+
"node_label": null,
|
|
373
|
+
"collection_key": null,
|
|
374
|
+
"_meta": {
|
|
375
|
+
"data_element": true
|
|
376
|
+
}
|
|
301
377
|
}
|
|
302
378
|
}
|
|
303
379
|
}
|