@gramlot/gramlot 0.3.5 → 0.3.6
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 +1344 -875
- package/dist/gramlot.min.js +22 -23
- package/package.json +2 -2
- package/src/binding/ask-dialog.js +46 -23
- package/src/binding/inline.js +7 -5
- package/src/binding/installation.js +14 -5
- package/src/binding/logic.js +10 -5
- package/src/binding/pointer.js +6 -10
- package/src/binding/providers.js +105 -82
- package/src/binding/remote.js +60 -26
- package/src/binding/router.js +2 -4
- package/src/binding/rpc-resolver.js +102 -53
- package/src/binding/runtime.js +58 -17
- package/src/bootstrap.d.ts +8 -1
- package/src/bootstrap.js +28 -11
- package/src/builder/binding.json +13 -1
- package/src/builder/gramlot-builder.d.ts +5 -2
- package/src/builder/gramlot-builder.js +15 -7
- package/src/builder/name-syntax.d.ts +37 -0
- package/src/builder/name-syntax.js +54 -0
- package/src/builder/source.js +169 -13
- package/src/components/border-container.js +17 -10
- package/src/components/selected-container.js +19 -18
- package/src/dom.d.ts +3 -0
- package/src/format.js +3 -10
- package/src/frame-messages.js +2 -1
- package/src/gramlot.d.ts +12 -4
- package/src/gramlot.js +25 -10
- package/src/handlers/rpc-handler.d.ts +25 -12
- package/src/handlers/rpc-handler.js +57 -13
- package/src/handlers/source-handler.d.ts +13 -3
- package/src/handlers/source-handler.js +23 -18
- package/src/handlers/subscriptions-handler.js +24 -0
- package/src/http-transport.d.ts +11 -2
- package/src/http-transport.js +31 -11
- package/src/index.d.ts +4 -2
- package/src/index.js +6 -2
- package/src/random-id.js +14 -0
- package/src/renderer/attributes.js +1 -1
- package/src/renderer/gramlot-html-renderer.js +14 -5
- package/src/renderer/gramlot-renderer.js +3 -3
- package/src/server/conformance.d.ts +4 -2
- package/src/server/conformance.js +70 -9
- package/src/server/files.js +40 -0
- package/src/server/gramlot-file-server.d.ts +2 -0
- package/src/server/gramlot-file-server.js +18 -18
- package/src/server/gramlot-server.d.ts +26 -7
- package/src/server/gramlot-server.js +153 -33
- package/src/server/index-page.js +1 -1
- package/src/server/index.d.ts +3 -1
- package/src/server/index.js +2 -1
- package/src/server/page-base.js +55 -20
- package/src/server/page.d.ts +37 -15
- package/src/server/page.js +7 -0
- package/src/server/resources.d.ts +2 -0
- package/src/server-adapter.d.ts +17 -0
- package/src/server-adapter.js +23 -0
- package/src/view/button.js +5 -5
- package/src/view/controls.js +5 -6
- package/src/view/html.js +7 -1
- package/themes/gramlot-base/README.md +5 -4
- package/themes/gramlot-base/theme.css +2 -0
- package/themes/gramlot-compact/README.md +5 -12
- package/themes/joanna/README.md +2 -3
- package/themes/mimi/README.md +2 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gramlot/gramlot",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
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": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@genrojs/bag": ">=0.11.0",
|
|
20
20
|
"@genrojs/builders": ">=0.6.0",
|
|
21
|
-
"@genrojs/tytx": ">=0.16.
|
|
21
|
+
"@genrojs/tytx": ">=0.16.3"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"c8": "10.1.3",
|
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
import {GramlotBuilder} from '../builder/gramlot-builder.js';
|
|
2
|
+
import {randomId} from '../random-id.js';
|
|
2
3
|
|
|
3
|
-
const pages = new WeakMap();
|
|
4
4
|
const SPEC_KEYS = new Set(['title', 'msg', 'fields', '_if', 'askIf']);
|
|
5
5
|
const FIELD_KEYS = new Set(['name', 'lbl', 'tag', 'type', 'default_value', 'required', 'min', 'max', 'step', 'placeholder']);
|
|
6
6
|
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* A bounded native dialog authored through Source/Data and named Logic. One instance per page, owned by
|
|
9
|
+
* its BindingRuntime (`runtime.askDialog`): the `ask_<id>` Logic group, registered at the first dialog, and
|
|
10
|
+
* the dialogs open.
|
|
11
|
+
*/
|
|
8
12
|
export class AskDialog {
|
|
9
|
-
|
|
13
|
+
#runtime;
|
|
14
|
+
// The name of the Logic group of the dialogs, null before the first one.
|
|
15
|
+
#group = null;
|
|
16
|
+
// The open dialogs by id: `{finish(confirmed)}`.
|
|
17
|
+
#requests = new Map();
|
|
18
|
+
|
|
19
|
+
constructor(runtime) {
|
|
20
|
+
this.#runtime = runtime;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get runtime() { return this.#runtime; }
|
|
24
|
+
|
|
25
|
+
/** Whether the ask `specification` of `binding` asks now, with `parameters` for its conditions. */
|
|
26
|
+
required(binding, specification, parameters, attribute) {
|
|
10
27
|
const evaluate = (name, code) => typeof code === 'boolean' ? code
|
|
11
28
|
: binding.runtime.inlineCompiler.compile(binding.node, name, code,
|
|
12
29
|
Object.keys(parameters), `return (${code});`)(parameters);
|
|
@@ -19,7 +36,8 @@ export class AskDialog {
|
|
|
19
36
|
return !specification?._if || Boolean(evaluate(`${attribute}._if`, specification._if));
|
|
20
37
|
}
|
|
21
38
|
|
|
22
|
-
|
|
39
|
+
/** Show the dialog of `specification`; resolves with the values of its fields, or null when cancelled. */
|
|
40
|
+
show(binding, specification, parameters, title = 'Parameters') {
|
|
23
41
|
const spec = typeof specification === 'string' ? {msg: specification, title} : specification;
|
|
24
42
|
if (!spec || typeof spec !== 'object' || Array.isArray(spec)) throw new TypeError('ask requires text or a parameter description');
|
|
25
43
|
for (const key of Object.keys(spec)) {
|
|
@@ -39,21 +57,11 @@ export class AskDialog {
|
|
|
39
57
|
throw new Error(`Unsupported ask input type: ${field.type}`);
|
|
40
58
|
}
|
|
41
59
|
}
|
|
42
|
-
const app =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const group = `ask_${Array.from(crypto.getRandomValues(new Uint8Array(16)), byte => byte.toString(16).padStart(2, '0')).join('')}`;
|
|
46
|
-
const requests = new Map();
|
|
47
|
-
class Logic {
|
|
48
|
-
confirm(node, event) { event.preventDefault(); requests.get(node.getAttr('request'))?.finish(true); }
|
|
49
|
-
cancel(node, event) { event.preventDefault(); requests.get(node.getAttr('request'))?.finish(false); }
|
|
50
|
-
}
|
|
51
|
-
app.src.logicRegistry.register(Logic, {group, resource: 'gramlot:ask'});
|
|
52
|
-
page = {group, requests};
|
|
53
|
-
pages.set(app, page);
|
|
54
|
-
}
|
|
60
|
+
const app = this.runtime.gramlot;
|
|
61
|
+
const group = this.#logicGroup();
|
|
62
|
+
const requests = this.#requests;
|
|
55
63
|
return new Promise((resolve, reject) => {
|
|
56
|
-
const id = `ask_${
|
|
64
|
+
const id = `ask_${randomId()}`;
|
|
57
65
|
let finished = false;
|
|
58
66
|
let release = () => {};
|
|
59
67
|
const finish = (confirmed, failure = null) => {
|
|
@@ -61,7 +69,7 @@ export class AskDialog {
|
|
|
61
69
|
finished = true;
|
|
62
70
|
const result = confirmed ? Object.fromEntries(fields.map(field =>
|
|
63
71
|
[field.name, app.data.getItem(`${id}.${field.name}`)])) : null;
|
|
64
|
-
|
|
72
|
+
requests.delete(id);
|
|
65
73
|
release();
|
|
66
74
|
try {
|
|
67
75
|
app.src.source.popNode(id);
|
|
@@ -70,17 +78,17 @@ export class AskDialog {
|
|
|
70
78
|
else resolve(result);
|
|
71
79
|
} catch (error) { reject(error); }
|
|
72
80
|
};
|
|
73
|
-
|
|
81
|
+
requests.set(id, {finish});
|
|
74
82
|
release = binding.track(() => finish(false));
|
|
75
83
|
try {
|
|
76
84
|
const builder = new GramlotBuilder();
|
|
77
85
|
const dialog = builder.root.dialog({node_id: id, datapath: id, request: id,
|
|
78
|
-
connect_oncancel: `${
|
|
86
|
+
connect_oncancel: `${group}.cancel`, connect_onclose: `${group}.cancel`,
|
|
79
87
|
class: 'gramlot-ask', 'aria-labelledby': `${id}_title`,
|
|
80
88
|
style: 'border:1px solid #aab4c2;border-radius:8px;padding:16px;max-width:32rem;width:calc(100% - 48px);font:inherit;'});
|
|
81
89
|
dialog.h2(spec.title ?? title, {id: `${id}_title`, style: 'font-size:1.1em;margin:0 0 12px;'});
|
|
82
90
|
if (spec.msg) dialog.p(spec.msg);
|
|
83
|
-
const form = dialog.form({request: id, connect_onsubmit: `${
|
|
91
|
+
const form = dialog.form({request: id, connect_onsubmit: `${group}.confirm`});
|
|
84
92
|
for (const field of fields) {
|
|
85
93
|
form.dataSetter({destination_path: `.${field.name}`,
|
|
86
94
|
value: Object.hasOwn(parameters, field.name) ? parameters[field.name]
|
|
@@ -95,7 +103,7 @@ export class AskDialog {
|
|
|
95
103
|
else row.input({...attrs, type: field.type ?? 'text'});
|
|
96
104
|
}
|
|
97
105
|
const buttons = form.div({style: 'display:flex;justify-content:flex-end;gap:8px;margin-top:16px;'});
|
|
98
|
-
buttons.html_button('Cancel', {type: 'button', request: id, connect_onclick: `${
|
|
106
|
+
buttons.html_button('Cancel', {type: 'button', request: id, connect_onclick: `${group}.cancel`});
|
|
99
107
|
buttons.html_button('Confirm', {type: 'submit', autofocus: fields.length === 0});
|
|
100
108
|
app.src.source.setItem(id, app.src.prepareSource(builder.source));
|
|
101
109
|
const node = app.src.binding.nodeIds.get(id);
|
|
@@ -105,4 +113,19 @@ export class AskDialog {
|
|
|
105
113
|
}
|
|
106
114
|
});
|
|
107
115
|
}
|
|
116
|
+
|
|
117
|
+
/** The name of the Logic group whose `confirm` and `cancel` close the dialogs, registered once. */
|
|
118
|
+
#logicGroup() {
|
|
119
|
+
if (this.#group === null) {
|
|
120
|
+
const group = `ask_${randomId()}`;
|
|
121
|
+
const requests = this.#requests;
|
|
122
|
+
class Logic {
|
|
123
|
+
confirm(node, event) { event.preventDefault(); requests.get(node.getAttr('request'))?.finish(true); }
|
|
124
|
+
cancel(node, event) { event.preventDefault(); requests.get(node.getAttr('request'))?.finish(false); }
|
|
125
|
+
}
|
|
126
|
+
this.runtime.logicRegistry.register(Logic, {group, resource: 'gramlot:ask'});
|
|
127
|
+
this.#group = group;
|
|
128
|
+
}
|
|
129
|
+
return this.#group;
|
|
130
|
+
}
|
|
108
131
|
}
|
package/src/binding/inline.js
CHANGED
|
@@ -56,7 +56,7 @@ export class InlineCompiler {
|
|
|
56
56
|
*/
|
|
57
57
|
preprocess(node, attr, source) {
|
|
58
58
|
const text = source.trim();
|
|
59
|
-
if (PUBLISH.test(text)) throw new Error(`${declaration(node, attr)}: the PUBLISH macro is excluded from Gramlot
|
|
59
|
+
if (PUBLISH.test(text)) throw new Error(`${declaration(node, attr)}: the PUBLISH macro is excluded from Gramlot`);
|
|
60
60
|
let result = text.replace(/\$(\d+)/g, (_token, n) => `arguments[${parseInt(n, 10) - 1}]`);
|
|
61
61
|
for (const [separator, macro, target] of MACROS) {
|
|
62
62
|
if (separator) result = result.replace(separator, `; ${separator.source.slice(1)}`);
|
|
@@ -109,12 +109,14 @@ export class InlineCompiler {
|
|
|
109
109
|
/**
|
|
110
110
|
* The function of the `==` expression `code` of the attribute `attr` of `node` (`''` for the node
|
|
111
111
|
* value), as written in the Source: `run(resolved)` evaluates it on the attributes resolved by
|
|
112
|
-
* runtimeValues.
|
|
113
|
-
*
|
|
114
|
-
*
|
|
112
|
+
* runtimeValues. The only compile path of `==`: its arguments are the other attributes of the
|
|
113
|
+
* node, except the other `==` (Q11.2), and `_F`; its body is `return <text>`, the text without
|
|
114
|
+
* surrounding whitespace, as legacy `funcCreate('return ' + text)`, so a text starting with a
|
|
115
|
+
* newline returns its value and a final `;` is accepted. Named pointer slots retain those
|
|
116
|
+
* attributes in the resolved projection; ordinary ${name} strings remain literal.
|
|
115
117
|
*/
|
|
116
118
|
compileExpression(node, attr, code) {
|
|
117
|
-
const expr = code.slice(2);
|
|
119
|
+
const expr = code.slice(2).trim();
|
|
118
120
|
const items = sourceAttributeItems(node.getAttr()).filter(([, raw]) => !isExpression(raw));
|
|
119
121
|
const run = this.compile(node, attr, code, [...items.map(([name]) => name).filter(name => name !== '_F'), '_F'], `return ${expr}`);
|
|
120
122
|
return resolved => {
|
|
@@ -174,11 +174,13 @@ export function requireInstallable(branch) {
|
|
|
174
174
|
|
|
175
175
|
// Provider attributes (§4.8): only a `dataFormula`, a `dataController`, a `dataRpc` or a `dataRemote`
|
|
176
176
|
// carries them; the lifecycle attributes not a `dataRemote`, whose resolver loads when its Data is read;
|
|
177
|
-
//
|
|
178
|
-
// and rendered ancestors that supply their
|
|
177
|
+
// `_onResult` and `_onError` only a `dataRpc`, `_onCalling` a `dataRpc`, a `dataRemote` or a node whose
|
|
178
|
+
// `remote` is a string. `_delay` also belongs to Gramlot buttons and rendered ancestors that supply their
|
|
179
|
+
// inherited click delay; `_delay`, `_timing` and `_userChanges` also to a node whose `remote` is a string.
|
|
179
180
|
const PROVIDER_ATTRIBUTES = ['_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges'];
|
|
180
181
|
const PROVIDER_TAGS = new Set(['dataFormula', 'dataController', 'dataRpc', 'dataRemote']);
|
|
181
182
|
const RPC_ATTRIBUTES = ['_onCalling', '_onResult', '_onError'];
|
|
183
|
+
const REMOTE_TRIGGER_ATTRIBUTES = ['_delay', '_timing', '_userChanges'];
|
|
182
184
|
// The lifecycle attributes: true, false, null or a number (a delay in ms, P22); a pointer is not read.
|
|
183
185
|
const LIFECYCLE_ATTRIBUTES = ['_init', '_onBuilt', '_onStart'];
|
|
184
186
|
|
|
@@ -190,9 +192,15 @@ const LIFECYCLE_ATTRIBUTES = ['_init', '_onBuilt', '_onStart'];
|
|
|
190
192
|
*/
|
|
191
193
|
function requireProviderAttributes(node) {
|
|
192
194
|
const isSet = name => node.getAttr(name) !== null && node.getAttr(name) !== undefined;
|
|
195
|
+
// A rendered node with a string `remote` has a RemoteBinding (NodeBinding.registerPointers).
|
|
196
|
+
const remote = !node._getMeta('data_element') && typeof node.getAttr('remote') === 'string';
|
|
193
197
|
if (node.nodeTag !== 'dataRpc') {
|
|
194
|
-
const callback = RPC_ATTRIBUTES.find(isSet)
|
|
195
|
-
|
|
198
|
+
const callback = RPC_ATTRIBUTES.find(name => isSet(name)
|
|
199
|
+
&& !(name === '_onCalling' && (node.nodeTag === 'dataRemote' || remote)));
|
|
200
|
+
if (callback) {
|
|
201
|
+
const owners = callback === '_onCalling' ? 'dataRpc, dataRemote and remote' : 'dataRpc';
|
|
202
|
+
throw new Error(`${node.nodeTag ?? 'fragment'} '${node.label}': '${callback}' is allowed only on ${owners}`);
|
|
203
|
+
}
|
|
196
204
|
}
|
|
197
205
|
if (PROVIDER_TAGS.has(node.nodeTag)) {
|
|
198
206
|
const expression = Object.entries(node.getAttr()).find(([, value]) => isExpression(value));
|
|
@@ -213,7 +221,8 @@ function requireProviderAttributes(node) {
|
|
|
213
221
|
return;
|
|
214
222
|
}
|
|
215
223
|
const name = PROVIDER_ATTRIBUTES.find(name => isSet(name)
|
|
216
|
-
&& !(name === '_delay' && !node._getMeta('data_element') && node.nodeTag !== 'html_button')
|
|
224
|
+
&& !(name === '_delay' && !node._getMeta('data_element') && node.nodeTag !== 'html_button')
|
|
225
|
+
&& !(remote && REMOTE_TRIGGER_ATTRIBUTES.includes(name)));
|
|
217
226
|
if (!name) return;
|
|
218
227
|
const tags = LIFECYCLE_ATTRIBUTES.includes(name) ? 'dataFormula, dataController and dataRpc'
|
|
219
228
|
: 'dataFormula, dataController, dataRpc and dataRemote';
|
package/src/binding/logic.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Named logic of one Gramlot page (source plan §4.9, decision P21).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* `LogicGroup`: the root group for the page logic (group null), a
|
|
6
|
-
* for each name of `js_requires` (`a/b` is the group `b` inside the group
|
|
7
|
-
*
|
|
4
|
+
* A logic file exports `class Logic` when it has logic methods; its prototype methods
|
|
5
|
+
* are copied into a `LogicGroup`: the root group for the page logic (group null), a
|
|
6
|
+
* child group for each name of `js_requires` (`a/b` is the group `b` inside the group
|
|
7
|
+
* `a`). A module without the `Logic` export has no logic methods. The constructor of
|
|
8
|
+
* `Logic` never runs.
|
|
8
9
|
*/
|
|
9
10
|
|
|
10
11
|
/** Names that are never a group: `page` is the group's own member, `constructor` the prototype's. */
|
|
@@ -93,8 +94,12 @@ function declaresConstructor(source) {
|
|
|
93
94
|
return /(?<![\w$.#])(?<!\bstatic\s+)constructor\s*\(/.test(members);
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* The `[name, function]` pairs of a Logic class, after the §4.9 rules; errors name the resource. A module
|
|
99
|
+
* without the `Logic` export (undefined) has none; a `Logic` export that is not a class is an error.
|
|
100
|
+
*/
|
|
97
101
|
function logicMethods(logicClass, resource) {
|
|
102
|
+
if (logicClass === undefined) return [];
|
|
98
103
|
if (typeof logicClass !== 'function' || !/^class\b/.test(Function.prototype.toString.call(logicClass))) {
|
|
99
104
|
throw new Error(`${resource}: the module exports no class Logic`);
|
|
100
105
|
}
|
package/src/binding/pointer.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import {BagResolver} from '@genrojs/bag';
|
|
2
2
|
import {sourceAttributeItems, pointerTransform, pointerTemplate, pointerExpression} from '@genrojs/builders';
|
|
3
|
+
import {isExpression} from '../renderer/attributes.js';
|
|
3
4
|
|
|
4
5
|
/** Structural attributes (not projected by Builder) may use a parent context, notably
|
|
5
|
-
* variable datapath. Reuse Builder's grammar; only the contextual reads and
|
|
6
|
-
* compilation belong here. Visual attributes go through Builder.runtimeValues.
|
|
6
|
+
* variable datapath. Reuse Builder's grammar; only the contextual reads and the `:=`
|
|
7
|
+
* compilation belong here. Visual attributes go through Builder.runtimeValues. A `==`
|
|
8
|
+
* attribute is a null input, as in Builder.runtimeValues: `==` is computed after the
|
|
9
|
+
* suffixes, by the renderer, with InlineCompiler.compileExpression (constitution 11.48 item 11). */
|
|
7
10
|
function attributeReader(node, items, context = node) {
|
|
8
11
|
const values = new Map(), active = new Set();
|
|
9
|
-
const compiler = node.rootBuilder.binding?.inlineCompiler;
|
|
10
12
|
// Resolve the complete Source attribute set before applying any suffix. Projection
|
|
11
13
|
// and named references share these typed values; the DOM is never an input.
|
|
12
14
|
const current = new Map();
|
|
@@ -14,7 +16,7 @@ function attributeReader(node, items, context = node) {
|
|
|
14
16
|
if (typeof name !== 'string') continue;
|
|
15
17
|
const pointer = pointerTransform(raw)?.pointer ?? raw;
|
|
16
18
|
current.set(name, pointer instanceof BagResolver ? pointer.resolve()
|
|
17
|
-
: node.pointerType(pointer) ? context.getRelativeData(pointer) : pointer);
|
|
19
|
+
: node.pointerType(pointer) ? context.getRelativeData(pointer) : isExpression(pointer) ? null : pointer);
|
|
18
20
|
}
|
|
19
21
|
const resolve = name => {
|
|
20
22
|
if (values.has(name)) return values.get(name);
|
|
@@ -40,12 +42,6 @@ function attributeReader(node, items, context = node) {
|
|
|
40
42
|
// A null leading pointer blocks even a constant template.
|
|
41
43
|
if (leading == null) result = null;
|
|
42
44
|
}
|
|
43
|
-
} else if (typeof raw === 'string' && raw.startsWith('==')) {
|
|
44
|
-
const names = pointerExpression(raw.slice(2)).identifiers;
|
|
45
|
-
const args = {};
|
|
46
|
-
for (const key of names) if (key !== name && key !== '_F' && items.has(key)) args[key] = resolve(key);
|
|
47
|
-
result = compiler ? compiler.compile(node, name, raw, [...Object.keys(args), '_F'],
|
|
48
|
-
`return (${raw.slice(2)}\n)` )({...args, _F: (value, options) => node.rootBuilder.binding.gramlot.format(value, options)}) : null;
|
|
49
45
|
} else {
|
|
50
46
|
result = current.get(name);
|
|
51
47
|
}
|
package/src/binding/providers.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {AskDialog} from './ask-dialog.js';
|
|
2
1
|
import {Bag} from '@genrojs/bag';
|
|
3
2
|
import {RpcResolver} from './rpc-resolver.js';
|
|
4
3
|
import {pointerTransform, pointerDependencies} from '@genrojs/builders';
|
|
@@ -21,17 +20,67 @@ const CONTROL_ATTRIBUTES = new Set([
|
|
|
21
20
|
'destination_path', 'result_path', 'func', 'formula', 'script', 'if', 'else',
|
|
22
21
|
'_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges',
|
|
23
22
|
'_ask', '_ask_if', '_ask_title', '_ask_onCancel',
|
|
24
|
-
'method', 'timeout', '_onCalling', '_onResult', '_onError', 'cacheTime',
|
|
23
|
+
'method', 'timeout', '_onCalling', '_onResult', '_onError', 'cacheTime', 'sync',
|
|
25
24
|
]);
|
|
26
25
|
|
|
26
|
+
/**
|
|
27
|
+
* The `_delay` and `_timing` timers of one Source node: the debounce of its Data triggers and its periodic
|
|
28
|
+
* trigger. Each timer is tracked on the node's NodeBinding, so the close of the node cancels it. Shared by
|
|
29
|
+
* the Provider of a data-element and the RemoteBinding of a `remote` node.
|
|
30
|
+
*/
|
|
31
|
+
export class TriggerTimers {
|
|
32
|
+
#binding;
|
|
33
|
+
#stopTiming = null;
|
|
34
|
+
#cancelDelay = null;
|
|
35
|
+
|
|
36
|
+
constructor(binding) {
|
|
37
|
+
this.#binding = binding;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get binding() { return this.#binding; }
|
|
41
|
+
|
|
42
|
+
/** Run `run` after `delay` ms; a new call cancels the scheduled one, never a request in flight (the last call wins). */
|
|
43
|
+
debounce(delay, run) {
|
|
44
|
+
this.#cancelDelay?.();
|
|
45
|
+
let release = null;
|
|
46
|
+
const timer = setTimeout(() => {
|
|
47
|
+
release();
|
|
48
|
+
this.#cancelDelay = null;
|
|
49
|
+
run();
|
|
50
|
+
}, delay);
|
|
51
|
+
release = this.binding.track(() => clearTimeout(timer));
|
|
52
|
+
this.#cancelDelay = () => {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
release();
|
|
55
|
+
this.#cancelDelay = null;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** `_timing` in seconds: `run` in a `setInterval` while `seconds` is above 0; null or 0 stops it. A new call restarts it. */
|
|
60
|
+
startTiming(seconds, run) {
|
|
61
|
+
this.#stopTiming?.();
|
|
62
|
+
if (!(seconds > 0)) return;
|
|
63
|
+
const timer = setInterval(run, seconds * 1000);
|
|
64
|
+
const release = this.binding.track(() => clearInterval(timer));
|
|
65
|
+
this.#stopTiming = () => {
|
|
66
|
+
clearInterval(timer);
|
|
67
|
+
release();
|
|
68
|
+
this.#stopTiming = null;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Cancel the scheduled `_delay` call and stop `_timing`. */
|
|
73
|
+
cancel() {
|
|
74
|
+
this.#cancelDelay?.();
|
|
75
|
+
this.#stopTiming?.();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
27
79
|
export class Provider {
|
|
28
80
|
#binding;
|
|
29
81
|
#registrations = [];
|
|
30
82
|
#timingRegistrations = new Set();
|
|
31
|
-
#
|
|
32
|
-
#cancelDelay = null;
|
|
33
|
-
// The AbortController of the in-flight call of a `dataRpc`: a new call aborts it (latest wins).
|
|
34
|
-
#rpcCall = null;
|
|
83
|
+
#timers;
|
|
35
84
|
// The RpcResolver a `dataRemote` installed and its absolute Data path.
|
|
36
85
|
#installed = null;
|
|
37
86
|
// The resolver removed last had loaded or was loading: the next one loads at once.
|
|
@@ -39,6 +88,7 @@ export class Provider {
|
|
|
39
88
|
|
|
40
89
|
constructor(binding) {
|
|
41
90
|
this.#binding = binding;
|
|
91
|
+
this.#timers = new TriggerTimers(binding);
|
|
42
92
|
binding.track(() => {
|
|
43
93
|
this.cancel();
|
|
44
94
|
this.#closeRegistrations();
|
|
@@ -75,22 +125,14 @@ export class Provider {
|
|
|
75
125
|
this.#registrations.push(registration);
|
|
76
126
|
if (name === '_timing' || timingDependencies.has(name)) this.#timingRegistrations.add(registration);
|
|
77
127
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const
|
|
81
|
-
if (
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const trigger = {kind: 'subscription', topic, payload};
|
|
87
|
-
if (this.#resolved('_userChanges') && topic !== 'node') return;
|
|
88
|
-
const delay = this.#resolved('_delay');
|
|
89
|
-
if (delay > 0) this.#debounce(trigger, delay);
|
|
90
|
-
else this.invoke(trigger);
|
|
91
|
-
});
|
|
92
|
-
this.#registrations.push({close: () => subscriptions.unsubscribe(handle)});
|
|
93
|
-
}
|
|
128
|
+
// Legacy provider declarations subscribe by presence, including a false value.
|
|
129
|
+
this.#registrations.push(...this.binding.runtime.subscriptions.subscribeNode(node, (name, value, topic) => payload => {
|
|
130
|
+
const trigger = {kind: 'subscription', topic, payload};
|
|
131
|
+
if (this.#resolved('_userChanges') && topic !== 'node') return;
|
|
132
|
+
const delay = this.#resolved('_delay');
|
|
133
|
+
if (delay > 0) this.#timers.debounce(delay, () => this.invoke(trigger));
|
|
134
|
+
else this.invoke(trigger);
|
|
135
|
+
}));
|
|
94
136
|
this.#startTiming();
|
|
95
137
|
if (this.kind === 'remote') this.invoke({kind: 'install', change: null});
|
|
96
138
|
}
|
|
@@ -108,7 +150,7 @@ export class Provider {
|
|
|
108
150
|
if (this.#resolved('_userChanges') && change.level !== 'node') return;
|
|
109
151
|
const trigger = {kind: 'data', change};
|
|
110
152
|
const delay = this.#resolved('_delay');
|
|
111
|
-
if (delay > 0) this.#debounce(
|
|
153
|
+
if (delay > 0) this.#timers.debounce(delay, () => this.invoke(trigger));
|
|
112
154
|
else this.invoke(trigger);
|
|
113
155
|
}
|
|
114
156
|
|
|
@@ -122,7 +164,8 @@ export class Provider {
|
|
|
122
164
|
* error propagates to the caller (P12). A closed node runs nothing. The body of a `dataRpc` is the
|
|
123
165
|
* call: `invoke` returns its promise, and a false `_if` runs `_else` as inline code without calling.
|
|
124
166
|
* The body of a `dataRemote` is the installation of its resolver; a false `_if` removes it, the
|
|
125
|
-
* Data node keeping its last value, and runs `_else` as inline code.
|
|
167
|
+
* Data node keeping its last value, and runs `_else` as inline code. A false `_if` never aborts a
|
|
168
|
+
* request in flight: it is a readiness condition.
|
|
126
169
|
*/
|
|
127
170
|
invoke(trigger) {
|
|
128
171
|
if (this.binding.closed) return;
|
|
@@ -136,8 +179,9 @@ export class Provider {
|
|
|
136
179
|
kwargs._askResult = trigger.askResult;
|
|
137
180
|
}
|
|
138
181
|
const ask = node.getAttr('_ask');
|
|
139
|
-
|
|
140
|
-
|
|
182
|
+
const askDialog = this.binding.runtime.askDialog;
|
|
183
|
+
if (ask && !trigger.askDone && askDialog.required(this.binding, ask, kwargs, '_ask')) {
|
|
184
|
+
return askDialog.show(this.binding, ask, kwargs, node.getAttr('_ask_title') ?? 'Warning').then(result => {
|
|
141
185
|
if (this.binding.closed) return;
|
|
142
186
|
if (result === null) {
|
|
143
187
|
const cancel = node.getAttr('_ask_onCancel');
|
|
@@ -184,7 +228,7 @@ export class Provider {
|
|
|
184
228
|
*/
|
|
185
229
|
readArguments(trigger) {
|
|
186
230
|
const node = this.node;
|
|
187
|
-
const [, attrs] = node.
|
|
231
|
+
const [, attrs] = node.rootBuilder.runtimeValues(node);
|
|
188
232
|
const authored = Object.fromEntries(Object.entries(attrs).filter(([name]) =>
|
|
189
233
|
!CONTROL_ATTRIBUTES.has(name) && !name.startsWith('subscribe_') && !name.startsWith('selfsubscribe_')));
|
|
190
234
|
const kwargs = {...(trigger.kind === 'subscription' ? trigger.payload : {}),
|
|
@@ -201,38 +245,12 @@ export class Provider {
|
|
|
201
245
|
|
|
202
246
|
/** Cancel the pending `_delay` call and stop `_timing`. */
|
|
203
247
|
cancel() {
|
|
204
|
-
this.#
|
|
205
|
-
this.#stopTiming?.();
|
|
248
|
+
this.#timers.cancel();
|
|
206
249
|
}
|
|
207
250
|
|
|
208
251
|
/** `_timing` in seconds: a `setInterval` while the value is above 0; null or 0 stops it. */
|
|
209
252
|
#startTiming() {
|
|
210
|
-
this.#
|
|
211
|
-
const seconds = this.#resolved('_timing');
|
|
212
|
-
if (!(seconds > 0)) return;
|
|
213
|
-
const timer = setInterval(() => this.invoke({kind: 'timing', change: null}), seconds * 1000);
|
|
214
|
-
const release = this.binding.track(() => clearInterval(timer));
|
|
215
|
-
this.#stopTiming = () => {
|
|
216
|
-
clearInterval(timer);
|
|
217
|
-
release();
|
|
218
|
-
this.#stopTiming = null;
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
#debounce(trigger, delay) {
|
|
223
|
-
this.#cancelDelay?.();
|
|
224
|
-
let release = null;
|
|
225
|
-
const timer = setTimeout(() => {
|
|
226
|
-
release();
|
|
227
|
-
this.#cancelDelay = null;
|
|
228
|
-
this.invoke(trigger);
|
|
229
|
-
}, delay);
|
|
230
|
-
release = this.binding.track(() => clearTimeout(timer));
|
|
231
|
-
this.#cancelDelay = () => {
|
|
232
|
-
clearTimeout(timer);
|
|
233
|
-
release();
|
|
234
|
-
this.#cancelDelay = null;
|
|
235
|
-
};
|
|
253
|
+
this.#timers.startTiming(this.#resolved('_timing'), () => this.invoke({kind: 'timing', change: null}));
|
|
236
254
|
}
|
|
237
255
|
|
|
238
256
|
/** The current value of a control attribute: a pointer is read from Data. */
|
|
@@ -256,43 +274,42 @@ export class Provider {
|
|
|
256
274
|
}
|
|
257
275
|
|
|
258
276
|
/**
|
|
259
|
-
* The call of a `dataRpc`: `_onCalling
|
|
260
|
-
* arguments, then the value written at
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
277
|
+
* The call of a `dataRpc`: `_onCalling` (a result `false` makes no call), then
|
|
278
|
+
* `gramlot.rpc.call('data', method, …)` with the author arguments, then the value written at
|
|
279
|
+
* `result_path` when present and `_onResult`. The call follows the rule of `node.requests`
|
|
280
|
+
* (NodeRequests): one request in flight, its answer always applied; the first call during the flight
|
|
281
|
+
* waits with its arguments and is sent at the answer. The close of the node aborts the call;
|
|
282
|
+
* `timeout` > 0 ms aborts it with a `TimeoutError`. A failure goes to `_onError`, or rejects the
|
|
283
|
+
* returned promise when the node declares none; a method name outside the syntax rejects it too.
|
|
264
284
|
*/
|
|
265
285
|
async #call(kwargs) {
|
|
266
286
|
const node = this.node;
|
|
267
287
|
const [onCalling, onResult, onError] = ['_onCalling', '_onResult', '_onError'].map(name => node.getAttr(name));
|
|
268
|
-
|
|
269
|
-
this.#
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
288
|
+
const requests = node.requests;
|
|
289
|
+
if (!requests.calling(onCalling ? () => this.#inline('_onCalling', onCalling, onCalling, kwargs, true) : null)) return;
|
|
290
|
+
return requests.submit(this.#resolved('method'), authorArguments(kwargs),
|
|
291
|
+
request => this.#send(request, kwargs, onResult, onError));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** Send the call `request` of a `dataRpc` and apply its outcome; a call of a closed node sends nothing. */
|
|
295
|
+
async #send(request, kwargs, onResult, onError) {
|
|
296
|
+
if (this.binding.closed) return;
|
|
297
|
+
const node = this.node;
|
|
273
298
|
const timeout = this.#resolved('timeout');
|
|
274
299
|
const timer = timeout > 0 ? setTimeout(() => {
|
|
275
300
|
const error = new Error(`${node.nodeTag} '${node.label}': no answer within ${timeout} ms`);
|
|
276
301
|
error.name = 'TimeoutError';
|
|
277
|
-
controller.abort(error);
|
|
302
|
+
request.controller.abort(error);
|
|
278
303
|
}, timeout) : null;
|
|
279
|
-
const aborted = new Promise((resolve, reject) => {
|
|
280
|
-
controller.signal.addEventListener('abort', () => reject(controller.signal.reason), {once: true});
|
|
281
|
-
});
|
|
282
304
|
let outcome;
|
|
283
305
|
try {
|
|
284
|
-
|
|
285
|
-
{signal: controller.signal});
|
|
286
|
-
outcome = {result: await Promise.race([call, aborted])};
|
|
306
|
+
outcome = {result: await this.binding.call('data', request)};
|
|
287
307
|
} catch (error) {
|
|
288
308
|
outcome = {error};
|
|
289
309
|
}
|
|
290
310
|
clearTimeout(timer);
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
const current = this.#rpcCall === controller && !this.binding.closed;
|
|
294
|
-
if (this.#rpcCall === controller) this.#rpcCall = null;
|
|
295
|
-
if (!current) return;
|
|
311
|
+
// The node closed, or the developer cancelled the request: the answer is ignored.
|
|
312
|
+
if (this.binding.closed || request.ignored) return;
|
|
296
313
|
if ('error' in outcome) {
|
|
297
314
|
if (!onError) throw outcome.error;
|
|
298
315
|
this.#inline('_onError', onError, onError, {...kwargs, error: outcome.error}, true);
|
|
@@ -308,8 +325,8 @@ export class Provider {
|
|
|
308
325
|
* author arguments as params, in place of the previous one, so the next read loads again. When the
|
|
309
326
|
* resolver removed last, by this call or by a false `_if`, had loaded or was loading, the new one
|
|
310
327
|
* loads at once: its consumers receive the new value through the Data event without reading again.
|
|
311
|
-
*
|
|
312
|
-
* installs nothing.
|
|
328
|
+
* `_onCalling` runs, with the arguments of the installation, before each load. A failed load is thrown
|
|
329
|
+
* again naming the node, as an unhandled rejection. A null path (Q7) installs nothing.
|
|
313
330
|
*/
|
|
314
331
|
#install(kwargs) {
|
|
315
332
|
this.#uninstall();
|
|
@@ -318,9 +335,13 @@ export class Provider {
|
|
|
318
335
|
const node = this.node;
|
|
319
336
|
const path = node.absDatapath(node.pathAttribute('destination_path'));
|
|
320
337
|
if (path === null) return;
|
|
338
|
+
const onCalling = node.getAttr('_onCalling');
|
|
321
339
|
const resolver = new RpcResolver({
|
|
322
|
-
|
|
340
|
+
binding: this.binding, method: this.#resolved('method'),
|
|
323
341
|
params: authorArguments(kwargs), cacheTime: this.#resolved('cacheTime'),
|
|
342
|
+
// `sync`, default true as in the legacy, waits only on a transport with synchronous calls.
|
|
343
|
+
sync: this.#resolved('sync') !== false && this.binding.runtime.gramlot.rpc.supportsCallSync,
|
|
344
|
+
onCalling: onCalling ? () => this.#inline('_onCalling', onCalling, onCalling, kwargs, true) : null,
|
|
324
345
|
onError: error => {
|
|
325
346
|
throw new Error(`${node.nodeTag} '${node.label}': dataRemote '${resolver.method}' failed: ${error.message}`,
|
|
326
347
|
{cause: error});
|
|
@@ -331,13 +352,15 @@ export class Provider {
|
|
|
331
352
|
if (reload) resolver.refresh();
|
|
332
353
|
}
|
|
333
354
|
|
|
334
|
-
/**
|
|
355
|
+
/**
|
|
356
|
+
* Remove the resolver this `dataRemote` installed, when it is still on its Data node. Its load in flight
|
|
357
|
+
* is not aborted: it finishes and writes its value (`node.requests`); the removal of the node aborts it.
|
|
358
|
+
*/
|
|
335
359
|
#uninstall() {
|
|
336
360
|
if (!this.#installed) return;
|
|
337
361
|
const {path, resolver} = this.#installed;
|
|
338
362
|
this.#installed = null;
|
|
339
363
|
this.#reload = resolver.loaded || resolver.loading !== null;
|
|
340
|
-
resolver.abort();
|
|
341
364
|
const dataNode = this.binding.runtime.data.getNode(path);
|
|
342
365
|
if (dataNode?.resolver === resolver) dataNode.resolver = null;
|
|
343
366
|
}
|