@gramlot/gramlot 0.2.15 → 0.2.16

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.
@@ -1,4 +1,4 @@
1
- import {SourceBag, SourceBagNode, sourceAttributeItems} from '@genrojs/builders';
1
+ import {SourceBag, SourceBagNode, sourceAttributeItems, wrapSource} from '@genrojs/builders';
2
2
  import {getSubtypeDict, setSubtypeDict} from '@genrojs/tytx';
3
3
 
4
4
  // The inline code attributes of each tag, besides `connect_on<event>` and the `==` expressions of any tag.
@@ -6,9 +6,16 @@ const CODE_ATTRIBUTES = {
6
6
  dataFormula: ['formula', '_if', '_else'],
7
7
  dataController: ['script', '_if', '_else'],
8
8
  dataRpc: ['_if', '_else', '_onCalling', '_onResult', '_onError'],
9
+ dataRemote: ['_if', '_else'],
9
10
  button: ['action'],
10
11
  };
11
12
 
13
+ // The control attributes of `remote`, written on the node as they are; every other parameter becomes `remote_<k>`.
14
+ const REMOTE_CONTROLS = ['_cachedRemote', '_if', '_else', '_onRemote'];
15
+ // The inline code attributes of any node whose `remote` is a string.
16
+ const REMOTE_CODE_ATTRIBUTES = ['_if', '_else', '_onRemote'];
17
+ const REMOTE_NAME = /^[A-Za-z]\w*$/;
18
+
12
19
  /** Whether `value` is a `==` expression (Q11.2). */
13
20
  function isExpression(value) {
14
21
  return typeof value === 'string' && value.startsWith('==');
@@ -31,10 +38,12 @@ export class GramlotBuilderBagNode extends SourceBagNode {
31
38
  * The inline code attributes of this node as `[name, text]`, under the names InlineCompiler uses:
32
39
  * `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of the three providers and the
33
40
  * `_onCalling`/`_onResult`/`_onError` of a dataRpc, `action` of a
34
- * button, every `connect_on<event>`, every `==` attribute (canonical name) and a `==` node value (`''`).
41
+ * button, `_if`/`_else`/`_onRemote` of a node whose `remote` is a string, every `connect_on<event>`,
42
+ * every `==` attribute (canonical name) and a `==` node value (`''`).
35
43
  */
36
44
  codeAttributes() {
37
- const own = CODE_ATTRIBUTES[this.nodeTag] ?? [];
45
+ const own = [...(CODE_ATTRIBUTES[this.nodeTag] ?? []),
46
+ ...(typeof this.getAttr('remote') === 'string' ? REMOTE_CODE_ATTRIBUTES : [])];
38
47
  const items = [];
39
48
  for (const [name, value] of Object.entries(this.getAttr() ?? {})) {
40
49
  if (typeof value === 'string' && (own.includes(name) || name.startsWith('connect_on'))) items.push([name, value]);
@@ -69,6 +78,29 @@ export class GramlotBuilderBagNode extends SourceBagNode {
69
78
  return this.#activatedCode?.get(name) ?? null;
70
79
  }
71
80
 
81
+ /**
82
+ * Declare the content of this element as the fragment `name` of a `@source` method: writes
83
+ * `remote = name` and one `remote_<k> = v` per entry of `params` on this node; the control attributes
84
+ * `_cachedRemote`, `_if`, `_else` and `_onRemote` are written as they are. Returns this node. A bad
85
+ * name or another `_` parameter is a TypeError.
86
+ */
87
+ remote(name, params = {}) {
88
+ if (typeof name !== 'string' || !REMOTE_NAME.test(name)) {
89
+ throw new TypeError(`${this.nodeTag} '${this.label}': remote name must match ${REMOTE_NAME.source}, not ${JSON.stringify(name)}`);
90
+ }
91
+ const invalid = Object.keys(params).find(key => key.startsWith('_') && !REMOTE_CONTROLS.includes(key));
92
+ if (invalid) {
93
+ throw new TypeError(`${this.nodeTag} '${this.label}': remote parameter '${invalid}' is not accepted; `
94
+ + `the control attributes are ${REMOTE_CONTROLS.join(', ')}`);
95
+ }
96
+ const attributes = {remote: name};
97
+ for (const [key, value] of Object.entries(params)) {
98
+ attributes[REMOTE_CONTROLS.includes(key) ? key : `remote_${key}`] = value;
99
+ }
100
+ this.setAttr(attributes);
101
+ return wrapSource(this);
102
+ }
103
+
72
104
  /** Builder's pointerType, except that a string starting with `==` is not a pointer. */
73
105
  pointerType(v) {
74
106
  if (typeof v === 'string' && v.startsWith('==')) return null;
@@ -41,7 +41,7 @@ export class SourceHandler extends Handler {
41
41
  * validates. Resolves with true when applied, false when superseded or cancelled.
42
42
  *
43
43
  * A declared fragment is requested from code with `gramlot.src.remoteSource` (GC-230 §035); the
44
- * declarative `remote` grammar attribute is not yet available.
44
+ * `remote` attribute requests it declaratively (GC-095 §105).
45
45
  */
46
46
  remoteSource(target: SourceBagNode, method: string, params?: Record<string, unknown>): Promise<boolean>;
47
47
  }
@@ -80,7 +80,7 @@ export class SourceHandler extends Handler {
80
80
  * or a freeze keeps it, the removal of the target cancels it.
81
81
  *
82
82
  * `@source` / `registerSource` declare the fragments this method requests (GC-230 §035); the
83
- * declarative `remote` grammar attribute is not yet available.
83
+ * `remote` attribute requests them declaratively (GC-095 §105).
84
84
  */
85
85
  async remoteSource(target, method, params = {}) {
86
86
  const rpc = this.gramlot.rpc;
@@ -10,8 +10,9 @@ export const SVG_NS = 'http://www.w3.org/2000/svg';
10
10
  const BINDING_ATTRIBUTES = new Set([
11
11
  'default', 'default_value', 'live', 'group', 'visible', 'action', 'fire',
12
12
  'if', 'else', '_init', '_onStart', '_onBuilt', '_delay', '_timing', '_userChanges',
13
+ 'remote', '_cachedRemote', '_onRemote',
13
14
  ]);
14
- const BINDING_ATTRIBUTE_PREFIXES = ['default_', 'attr_', 'fire_', 'connect_on'];
15
+ const BINDING_ATTRIBUTE_PREFIXES = ['default_', 'attr_', 'fire_', 'connect_on', 'remote_'];
15
16
 
16
17
  // Legacy noConvertStyle (genro_wdg.js:102-108): these attributes stay native attributes, never style.
17
18
  const NATIVE_ATTRIBUTES = {