@gramlot/gramlot 0.2.14 → 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.
package/dist/gramlot.js CHANGED
@@ -21783,8 +21783,12 @@ var CODE_ATTRIBUTES = {
21783
21783
  dataFormula: ["formula", "_if", "_else"],
21784
21784
  dataController: ["script", "_if", "_else"],
21785
21785
  dataRpc: ["_if", "_else", "_onCalling", "_onResult", "_onError"],
21786
+ dataRemote: ["_if", "_else"],
21786
21787
  button: ["action"]
21787
21788
  };
21789
+ var REMOTE_CONTROLS = ["_cachedRemote", "_if", "_else", "_onRemote"];
21790
+ var REMOTE_CODE_ATTRIBUTES = ["_if", "_else", "_onRemote"];
21791
+ var REMOTE_NAME = /^[A-Za-z]\w*$/;
21788
21792
  function isExpression(value) {
21789
21793
  return typeof value === "string" && value.startsWith("==");
21790
21794
  }
@@ -21797,10 +21801,14 @@ var GramlotBuilderBagNode = class extends SourceBagNode {
21797
21801
  * The inline code attributes of this node as `[name, text]`, under the names InlineCompiler uses:
21798
21802
  * `formula` of a dataFormula, `script` of a dataController, `_if`/`_else` of the three providers and the
21799
21803
  * `_onCalling`/`_onResult`/`_onError` of a dataRpc, `action` of a
21800
- * button, every `connect_on<event>`, every `==` attribute (canonical name) and a `==` node value (`''`).
21804
+ * button, `_if`/`_else`/`_onRemote` of a node whose `remote` is a string, every `connect_on<event>`,
21805
+ * every `==` attribute (canonical name) and a `==` node value (`''`).
21801
21806
  */
21802
21807
  codeAttributes() {
21803
- const own = CODE_ATTRIBUTES[this.nodeTag] ?? [];
21808
+ const own = [
21809
+ ...CODE_ATTRIBUTES[this.nodeTag] ?? [],
21810
+ ...typeof this.getAttr("remote") === "string" ? REMOTE_CODE_ATTRIBUTES : []
21811
+ ];
21804
21812
  const items = [];
21805
21813
  for (const [name, value] of Object.entries(this.getAttr() ?? {})) {
21806
21814
  if (typeof value === "string" && (own.includes(name) || name.startsWith("connect_on"))) items.push([name, value]);
@@ -21831,6 +21839,27 @@ var GramlotBuilderBagNode = class extends SourceBagNode {
21831
21839
  activatedCode(name) {
21832
21840
  return this.#activatedCode?.get(name) ?? null;
21833
21841
  }
21842
+ /**
21843
+ * Declare the content of this element as the fragment `name` of a `@source` method: writes
21844
+ * `remote = name` and one `remote_<k> = v` per entry of `params` on this node; the control attributes
21845
+ * `_cachedRemote`, `_if`, `_else` and `_onRemote` are written as they are. Returns this node. A bad
21846
+ * name or another `_` parameter is a TypeError.
21847
+ */
21848
+ remote(name, params = {}) {
21849
+ if (typeof name !== "string" || !REMOTE_NAME.test(name)) {
21850
+ throw new TypeError(`${this.nodeTag} '${this.label}': remote name must match ${REMOTE_NAME.source}, not ${JSON.stringify(name)}`);
21851
+ }
21852
+ const invalid = Object.keys(params).find((key) => key.startsWith("_") && !REMOTE_CONTROLS.includes(key));
21853
+ if (invalid) {
21854
+ throw new TypeError(`${this.nodeTag} '${this.label}': remote parameter '${invalid}' is not accepted; the control attributes are ${REMOTE_CONTROLS.join(", ")}`);
21855
+ }
21856
+ const attributes = { remote: name };
21857
+ for (const [key, value] of Object.entries(params)) {
21858
+ attributes[REMOTE_CONTROLS.includes(key) ? key : `remote_${key}`] = value;
21859
+ }
21860
+ this.setAttr(attributes);
21861
+ return wrapSource(this);
21862
+ }
21834
21863
  /** Builder's pointerType, except that a string starting with `==` is not a pointer. */
21835
21864
  pointerType(v) {
21836
21865
  if (typeof v === "string" && v.startsWith("==")) return null;
@@ -22016,6 +22045,43 @@ var GramlotBuilderBag = class _GramlotBuilderBag extends SourceBag {
22016
22045
  get nodeClass() {
22017
22046
  return GramlotBuilderBagNode;
22018
22047
  }
22048
+ /**
22049
+ * A copy of this Source without the elements whose `auth` rule `auth` refuses, as the Python
22050
+ * `authorized_copy`. `auth` is a function `rule => null | 'not_authenticated' | 'not_authorized'`.
22051
+ * An element whose `auth` attribute `auth` answers with a code is left out with its subtree;
22052
+ * nested Source branches are copied the same way. This Bag is not changed. A non-string `auth`
22053
+ * attribute is a TypeError.
22054
+ */
22055
+ authorizedCopy(auth) {
22056
+ const result = new this.constructor();
22057
+ for (const node of this.getNodes()) {
22058
+ const rule = node.getAttr("auth") ?? null;
22059
+ if (rule !== null) {
22060
+ if (typeof rule !== "string") {
22061
+ throw new TypeError(`${node.nodeTag} '${node.label}': 'auth' must be a string rule, not ${typeof rule}`);
22062
+ }
22063
+ if (auth(rule) !== null) continue;
22064
+ }
22065
+ let value = node.staticValue;
22066
+ if (value instanceof _GramlotBuilderBag) value = value.authorizedCopy(auth);
22067
+ const { auth: _rule, ...attributes } = node.getAttr();
22068
+ const copied = result.setItem(
22069
+ node.label,
22070
+ value,
22071
+ attributes,
22072
+ ">",
22073
+ false,
22074
+ false,
22075
+ null,
22076
+ false,
22077
+ true,
22078
+ null,
22079
+ node.nodeTag
22080
+ );
22081
+ copied.xmlTag = node.xmlTag;
22082
+ }
22083
+ return result;
22084
+ }
22019
22085
  /** Activate the inline code of every node, nested Source branches included (GramlotBuilderBagNode.activateCode); returns this. */
22020
22086
  activateCode() {
22021
22087
  for (const node of this.getNodes()) {
@@ -22048,9 +22114,12 @@ var BINDING_ATTRIBUTES = /* @__PURE__ */ new Set([
22048
22114
  "_onBuilt",
22049
22115
  "_delay",
22050
22116
  "_timing",
22051
- "_userChanges"
22117
+ "_userChanges",
22118
+ "remote",
22119
+ "_cachedRemote",
22120
+ "_onRemote"
22052
22121
  ]);
22053
- var BINDING_ATTRIBUTE_PREFIXES = ["default_", "attr_", "fire_", "connect_on"];
22122
+ var BINDING_ATTRIBUTE_PREFIXES = ["default_", "attr_", "fire_", "connect_on", "remote_"];
22054
22123
  var NATIVE_ATTRIBUTES = {
22055
22124
  table: ["width", "border"],
22056
22125
  editor: ["height"],
@@ -22644,16 +22713,97 @@ var binding_default = {
22644
22713
  _meta: {
22645
22714
  data_element: true
22646
22715
  }
22716
+ },
22717
+ dataRemote: {
22718
+ doc: "Data node resolved by a server endpoint through gramlot.rpc, on first read and again when cacheTime expires.",
22719
+ sub_tags: "",
22720
+ parent_tags: null,
22721
+ inherits_from: null,
22722
+ ns: null,
22723
+ attributes: {
22724
+ parameters: [
22725
+ {
22726
+ name: "destination_path",
22727
+ kind: "positional_or_keyword",
22728
+ role: "attribute",
22729
+ annotation: {
22730
+ kind: "type",
22731
+ module: "builtins",
22732
+ name: "str"
22733
+ },
22734
+ has_default: false
22735
+ },
22736
+ {
22737
+ name: "method",
22738
+ kind: "positional_or_keyword",
22739
+ role: "attribute",
22740
+ annotation: {
22741
+ kind: "type",
22742
+ module: "builtins",
22743
+ name: "str"
22744
+ },
22745
+ has_default: false
22746
+ },
22747
+ {
22748
+ name: "cacheTime",
22749
+ kind: "positional_or_keyword",
22750
+ role: "attribute",
22751
+ annotation: {
22752
+ kind: "union",
22753
+ items: [
22754
+ {
22755
+ kind: "type",
22756
+ module: "builtins",
22757
+ name: "int"
22758
+ },
22759
+ {
22760
+ kind: "type",
22761
+ module: "builtins",
22762
+ name: "float"
22763
+ },
22764
+ {
22765
+ kind: "type",
22766
+ module: "builtins",
22767
+ name: "NoneType"
22768
+ }
22769
+ ]
22770
+ },
22771
+ has_default: true,
22772
+ default: null
22773
+ },
22774
+ {
22775
+ name: "params",
22776
+ kind: "var_keyword",
22777
+ role: "attribute",
22778
+ annotation: {
22779
+ kind: "any"
22780
+ },
22781
+ has_default: false
22782
+ }
22783
+ ],
22784
+ accepts_var_keyword: true,
22785
+ accepts_var_positional: false
22786
+ },
22787
+ node_label: null,
22788
+ collection_key: null,
22789
+ _meta: {
22790
+ data_element: true
22791
+ }
22647
22792
  }
22648
22793
  }
22649
22794
  };
22650
22795
 
22651
22796
  // src/builder/gramlot-builder.js
22652
22797
  var DATA_FORBIDDEN = "data is forbidden: write dataSetter for a Data value or html_data for the HTML5 <data> element";
22653
- var EXCLUDED_ELEMENTS = /* @__PURE__ */ new Set(["dataremote"]);
22654
- var EXCLUDED_ATTRIBUTES = /* @__PURE__ */ new Set(["serverpath", "dbenv", "shared_id", "remote", "_ask", "ask"]);
22798
+ var EXCLUDED_ELEMENTS = /* @__PURE__ */ new Set();
22799
+ var EXCLUDED_ATTRIBUTES = /* @__PURE__ */ new Set(["serverpath", "dbenv", "shared_id", "_ask", "ask"]);
22655
22800
  var EXCLUDED_ATTRIBUTE_PREFIXES = ["subscribe_", "selfsubscribe_", "formsubscribe_"];
22656
- var DATA_PATH_ATTRIBUTES = { dataSetter: "destination_path", dataFormula: "result_path", dataRpc: "result_path" };
22801
+ var DATA_PATH_ATTRIBUTES = {
22802
+ dataSetter: "destination_path",
22803
+ dataFormula: "result_path",
22804
+ dataRpc: "result_path",
22805
+ dataRemote: "destination_path"
22806
+ };
22657
22807
  var DATA_INLINE_ATTRIBUTES = { dataFormula: "formula", dataController: "script" };
22658
22808
  function dataElementError(tag2, attrs) {
22659
22809
  const pathName = DATA_PATH_ATTRIBUTES[tag2];
@@ -22686,11 +22836,17 @@ var GramlotBuilder = class _GramlotBuilder extends HtmlBuilder {
22686
22836
  }
22687
22837
  return prefixes;
22688
22838
  }
22689
- constructor(name = null, { collections = [] } = {}) {
22839
+ constructor(name = null, { collections = [], auth = null } = {}) {
22690
22840
  super(name);
22691
22841
  this._binding = null;
22842
+ this._auth = auth;
22692
22843
  for (const collection of collections) this.loadCollection(collection);
22693
22844
  }
22845
+ /** The `auth` evaluator, `rule => null | 'not_authenticated' | 'not_authorized'`, or null:
22846
+ * without it every element is serialised. */
22847
+ get auth() {
22848
+ return this._auth;
22849
+ }
22694
22850
  /** The BindingRuntime of the Gramlot page that owns this builder, or null for authoring alone. */
22695
22851
  get binding() {
22696
22852
  return this._binding;
@@ -22801,8 +22957,10 @@ var GramlotBuilder = class _GramlotBuilder extends HtmlBuilder {
22801
22957
  }
22802
22958
  return { $gramlotRef: id, kind };
22803
22959
  }
22960
+ /** The Source encoded as TYTX text, as the Python `to_tytx`. With an `auth` evaluator, the elements
22961
+ * whose `auth` rule it refuses are left out (GramlotBuilderBag.authorizedCopy). */
22804
22962
  toTytx() {
22805
- return toTytx(this.source);
22963
+ return toTytx(this.auth === null ? this.source : this.source.authorizedCopy(this.auth));
22806
22964
  }
22807
22965
  };
22808
22966
 
@@ -23905,7 +24063,7 @@ function requireInstallable(branch) {
23905
24063
  }
23906
24064
  }
23907
24065
  var PROVIDER_ATTRIBUTES = ["_init", "_onStart", "_onBuilt", "_delay", "_timing", "_userChanges"];
23908
- var PROVIDER_TAGS = /* @__PURE__ */ new Set(["dataFormula", "dataController", "dataRpc"]);
24066
+ var PROVIDER_TAGS = /* @__PURE__ */ new Set(["dataFormula", "dataController", "dataRpc", "dataRemote"]);
23909
24067
  var RPC_ATTRIBUTES = ["_onCalling", "_onResult", "_onError"];
23910
24068
  var LIFECYCLE_ATTRIBUTES = ["_init", "_onBuilt", "_onStart"];
23911
24069
  function requireProviderAttributes(node) {
@@ -23919,6 +24077,10 @@ function requireProviderAttributes(node) {
23919
24077
  if (expression) {
23920
24078
  throw new Error(`${node.nodeTag} '${node.label}': '${expression[0]}' is a == expression, not accepted on ${node.nodeTag}`);
23921
24079
  }
24080
+ const lifecycle = node.nodeTag === "dataRemote" ? LIFECYCLE_ATTRIBUTES.find(isSet) : null;
24081
+ if (lifecycle) {
24082
+ throw new Error(`dataRemote '${node.label}': '${lifecycle}' is not accepted on dataRemote, its resolver loads when the Data is read`);
24083
+ }
23922
24084
  for (const name2 of LIFECYCLE_ATTRIBUTES) {
23923
24085
  const value = node.getAttr(name2);
23924
24086
  if (value === null || value === void 0 || typeof value === "boolean" || typeof value === "number") continue;
@@ -23927,7 +24089,9 @@ function requireProviderAttributes(node) {
23927
24089
  return;
23928
24090
  }
23929
24091
  const name = PROVIDER_ATTRIBUTES.find(isSet);
23930
- if (name) throw new Error(`${node.nodeTag ?? "fragment"} '${node.label}': '${name}' is allowed only on dataFormula, dataController and dataRpc`);
24092
+ if (!name) return;
24093
+ const tags = LIFECYCLE_ATTRIBUTES.includes(name) ? "dataFormula, dataController and dataRpc" : "dataFormula, dataController, dataRpc and dataRemote";
24094
+ throw new Error(`${node.nodeTag ?? "fragment"} '${node.label}': '${name}' is allowed only on ${tags}`);
23931
24095
  }
23932
24096
  function branchNodes(branch) {
23933
24097
  if (branch instanceof SourceBag) return branch.getNodes().flatMap((node) => branchNodes(node));
@@ -23936,6 +24100,74 @@ function branchNodes(branch) {
23936
24100
  return nodes;
23937
24101
  }
23938
24102
 
24103
+ // src/binding/rpc-resolver.js
24104
+ var RpcResolver = class extends BagResolver {
24105
+ static classKwargs = { ...BagResolver.classKwargs, asBag: false };
24106
+ static internalParams = /* @__PURE__ */ new Set([...BagResolver.internalParams, "gramlot", "method", "params", "onError"]);
24107
+ constructor({ gramlot, method, params, cacheTime, onError }) {
24108
+ super({ cacheTime: cacheTime ?? -1 });
24109
+ this.gramlot = gramlot;
24110
+ this.method = method;
24111
+ this.params = params;
24112
+ this.onError = onError;
24113
+ this.controller = null;
24114
+ this.loading = null;
24115
+ this.loaded = false;
24116
+ this.writing = false;
24117
+ }
24118
+ /** The current value; a non-static read starts the load when the cache has expired. */
24119
+ resolve({ static: isStatic = false } = {}) {
24120
+ if (!isStatic && !this.loading && !this.writing && this.expired) this.refresh();
24121
+ return this.cachedValue;
24122
+ }
24123
+ /** Start a load and write its value on the node, with the Data event unless `cacheTime` is 0; returns `loading`. */
24124
+ refresh() {
24125
+ const settle = () => {
24126
+ if (this.loading !== loading) return;
24127
+ this.controller = null;
24128
+ this.loading = null;
24129
+ };
24130
+ const load = this.load();
24131
+ const { signal } = this.controller;
24132
+ const loading = load.then((value) => {
24133
+ settle();
24134
+ if (signal.aborted) throw signal.reason;
24135
+ this._lastUpdate = Date.now();
24136
+ this.loaded = true;
24137
+ this.writing = true;
24138
+ try {
24139
+ if (this._node) this._node.setValue(value, this.cacheTime !== 0);
24140
+ else this.cachedValue = value;
24141
+ } finally {
24142
+ this.writing = false;
24143
+ }
24144
+ return value;
24145
+ }, (error) => {
24146
+ settle();
24147
+ throw error;
24148
+ });
24149
+ this.loading = loading;
24150
+ loading.catch((error) => {
24151
+ if (error?.name !== "AbortError") this.onError(error);
24152
+ });
24153
+ return loading;
24154
+ }
24155
+ /** The call of the endpoint with the params given at construction; the read kwargs are not sent. */
24156
+ load() {
24157
+ const controller = new AbortController();
24158
+ const aborted = new Promise((_, reject) => {
24159
+ controller.signal.addEventListener("abort", () => reject(controller.signal.reason), { once: true });
24160
+ });
24161
+ const call = this.gramlot.rpc.call("data", this.method, this.params, { signal: controller.signal });
24162
+ this.controller = controller;
24163
+ return Promise.race([call, aborted]);
24164
+ }
24165
+ /** Cancel the pending load, if any. */
24166
+ abort() {
24167
+ this.controller?.abort();
24168
+ }
24169
+ };
24170
+
23939
24171
  // src/binding/providers.js
23940
24172
  var CONTROL_ATTRIBUTES = /* @__PURE__ */ new Set([
23941
24173
  "destination_path",
@@ -23955,7 +24187,8 @@ var CONTROL_ATTRIBUTES = /* @__PURE__ */ new Set([
23955
24187
  "timeout",
23956
24188
  "_onCalling",
23957
24189
  "_onResult",
23958
- "_onError"
24190
+ "_onError",
24191
+ "cacheTime"
23959
24192
  ]);
23960
24193
  var Provider = class {
23961
24194
  #binding;
@@ -23965,11 +24198,16 @@ var Provider = class {
23965
24198
  #cancelDelay = null;
23966
24199
  // The AbortController of the in-flight call of a `dataRpc`: a new call aborts it (latest wins).
23967
24200
  #rpcCall = null;
24201
+ // The RpcResolver a `dataRemote` installed and its absolute Data path.
24202
+ #installed = null;
24203
+ // The resolver removed last had loaded or was loading: the next one loads at once.
24204
+ #reload = false;
23968
24205
  constructor(binding) {
23969
24206
  this.#binding = binding;
23970
24207
  binding.track(() => {
23971
24208
  this.cancel();
23972
24209
  this.#closeRegistrations();
24210
+ this.#uninstall();
23973
24211
  });
23974
24212
  }
23975
24213
  get binding() {
@@ -23978,14 +24216,15 @@ var Provider = class {
23978
24216
  get node() {
23979
24217
  return this.binding.node;
23980
24218
  }
23981
- /** 'formula' | 'controller' | 'rpc'. */
24219
+ /** 'formula' | 'controller' | 'rpc' | 'remote'. */
23982
24220
  get kind() {
23983
- throw new Error("Provider.kind is defined by FormulaProvider, ControllerProvider and RpcProvider");
24221
+ throw new Error("Provider.kind is defined by FormulaProvider, ControllerProvider, RpcProvider and RemoteProvider");
23984
24222
  }
23985
24223
  /**
23986
24224
  * Step 4 of §5.1, and every rebinding: the `^` pointers are registered again from the current
23987
24225
  * context (a pointer on a null path is not registered, Q7), and `_timing` starts again from its
23988
- * current value. A numeric `_onStart` must be a finite number not below zero (P22).
24226
+ * current value. A numeric `_onStart` must be a finite number not below zero (P22). A `dataRemote`
24227
+ * then installs its resolver with the current arguments.
23989
24228
  */
23990
24229
  register() {
23991
24230
  const node = this.node;
@@ -24005,6 +24244,7 @@ var Provider = class {
24005
24244
  if (name === "_timing") this.#timingRegistration = registration;
24006
24245
  }
24007
24246
  this.#startTiming();
24247
+ if (this.kind === "remote") this.invoke({ kind: "install", change: null });
24008
24248
  }
24009
24249
  /**
24010
24250
  * Router recipient. The `_timing` pointer restarts the timer; any other change is a Data
@@ -24031,24 +24271,29 @@ var Provider = class {
24031
24271
  * the inline `formula`/`script`, compiled by the page's InlineCompiler, or the named `func`. An
24032
24272
  * error propagates to the caller (P12). A closed node runs nothing. The body of a `dataRpc` is the
24033
24273
  * call: `invoke` returns its promise, and a false `_if` runs `_else` as inline code without calling.
24274
+ * The body of a `dataRemote` is the installation of its resolver; a false `_if` removes it, the
24275
+ * Data node keeping its last value, and runs `_else` as inline code.
24034
24276
  */
24035
24277
  invoke(trigger) {
24036
24278
  if (this.binding.closed) return;
24037
24279
  const node = this.node;
24038
24280
  const formula = this.kind === "formula";
24039
24281
  const rpc = this.kind === "rpc";
24282
+ const remote = this.kind === "remote";
24040
24283
  const kwargs = this.readArguments(trigger);
24041
24284
  let [attr, body] = [formula ? "formula" : "script", node.getAttr(formula ? "formula" : "script")];
24042
24285
  const condition = node.getAttr("_if");
24043
24286
  if (condition && !this.#inline("_if", condition, `return (${condition})`, kwargs, false)) {
24287
+ if (remote) this.#uninstall();
24044
24288
  [attr, body] = ["_else", node.getAttr("_else")];
24045
24289
  if (!body) return;
24046
- if (rpc) {
24290
+ if (rpc || remote) {
24047
24291
  this.#inline(attr, body, body, kwargs, true);
24048
24292
  return;
24049
24293
  }
24050
24294
  }
24051
24295
  if (rpc) return this.#call(kwargs);
24296
+ if (remote) return this.#install(kwargs);
24052
24297
  const func = node.getAttr("func");
24053
24298
  let result;
24054
24299
  if (body) {
@@ -24187,6 +24432,47 @@ var Provider = class {
24187
24432
  if (node.getAttr("result_path") != null) node.setRelativeData(node.pathAttribute("result_path"), result);
24188
24433
  if (onResult) this.#inline("_onResult", onResult, onResult, { ...kwargs, result }, true);
24189
24434
  }
24435
+ /**
24436
+ * The body of a `dataRemote`: a new RpcResolver on the Data node at `destination_path`, with the
24437
+ * author arguments as params, in place of the previous one, so the next read loads again. When the
24438
+ * resolver removed last, by this call or by a false `_if`, had loaded or was loading, the new one
24439
+ * loads at once: its consumers receive the new value through the Data event without reading again.
24440
+ * A failed load is thrown again naming the node, as an unhandled rejection. A null path (Q7)
24441
+ * installs nothing.
24442
+ */
24443
+ #install(kwargs) {
24444
+ this.#uninstall();
24445
+ const reload = this.#reload;
24446
+ this.#reload = false;
24447
+ const node = this.node;
24448
+ const path = node.absDatapath(node.pathAttribute("destination_path"));
24449
+ if (path === null) return;
24450
+ const resolver = new RpcResolver({
24451
+ gramlot: this.binding.runtime.gramlot,
24452
+ method: this.#resolved("method"),
24453
+ params: authorArguments(kwargs),
24454
+ cacheTime: this.#resolved("cacheTime"),
24455
+ onError: (error) => {
24456
+ throw new Error(
24457
+ `${node.nodeTag} '${node.label}': dataRemote '${resolver.method}' failed: ${error.message}`,
24458
+ { cause: error }
24459
+ );
24460
+ }
24461
+ });
24462
+ this.binding.runtime.data.setResolver(path, resolver);
24463
+ this.#installed = { path, resolver };
24464
+ if (reload) resolver.refresh();
24465
+ }
24466
+ /** Remove the resolver this `dataRemote` installed, when it is still on its Data node, and abort its load. */
24467
+ #uninstall() {
24468
+ if (!this.#installed) return;
24469
+ const { path, resolver } = this.#installed;
24470
+ this.#installed = null;
24471
+ this.#reload = resolver.loaded || resolver.loading !== null;
24472
+ resolver.abort();
24473
+ const dataNode = this.binding.runtime.data.getNode(path);
24474
+ if (dataNode?.resolver === resolver) dataNode.resolver = null;
24475
+ }
24190
24476
  #closeRegistrations() {
24191
24477
  for (const registration of this.#registrations.splice(0)) registration.close();
24192
24478
  this.#timingRegistration = null;
@@ -24207,6 +24493,11 @@ var RpcProvider = class extends Provider {
24207
24493
  return "rpc";
24208
24494
  }
24209
24495
  };
24496
+ var RemoteProvider = class extends Provider {
24497
+ get kind() {
24498
+ return "remote";
24499
+ }
24500
+ };
24210
24501
  var TRIGGER_FIELDS = ["_node", "_triggerpars", "_reason"];
24211
24502
  var CLICK_FIELDS = ["_evt", "button_counter", "button_shift", "button_ctrl", "button_alt", "button_meta"];
24212
24503
  function authorArguments(kwargs) {
@@ -24417,8 +24708,80 @@ ${text}`, { cause: error });
24417
24708
  }
24418
24709
  }
24419
24710
 
24711
+ // src/binding/remote.js
24712
+ var RemoteBinding = class {
24713
+ #binding;
24714
+ // The parameters of the last call, compared by `_cachedRemote`.
24715
+ #lastParams = null;
24716
+ constructor(binding) {
24717
+ this.#binding = binding;
24718
+ }
24719
+ get binding() {
24720
+ return this.#binding;
24721
+ }
24722
+ get node() {
24723
+ return this.binding.node;
24724
+ }
24725
+ get closed() {
24726
+ return this.binding.closed;
24727
+ }
24728
+ /**
24729
+ * Request the fragment for the current parameters. Nothing happens before the node's `start` step,
24730
+ * on a closed node, without a name, or when `_if` or `_cachedRemote` stop the call; a false `_if`
24731
+ * also cancels the request in flight, so its fragment is not mounted after `_else`. A failure of the
24732
+ * request rejects with an error naming the node, its cause attached.
24733
+ */
24734
+ async update() {
24735
+ if (this.closed || !this.binding.hasStamp("start")) return;
24736
+ const node = this.node;
24737
+ const [, attrs] = node.builder.runtimeValues(node);
24738
+ const name = attrs.remote;
24739
+ if (!name) return;
24740
+ const params = {};
24741
+ for (const [key, value] of Object.entries(attrs)) {
24742
+ if (key.startsWith("remote_")) params[key.slice("remote_".length)] = value;
24743
+ }
24744
+ const condition = node.getAttr("_if");
24745
+ if (condition && !this.#inline("_if", condition, `return (${condition})`, params)) {
24746
+ this.binding.runtime.gramlot.rpc.remoteRequests.get(node)?.controller.abort();
24747
+ const otherwise = node.getAttr("_else");
24748
+ if (otherwise) this.#inline("_else", otherwise, otherwise, params);
24749
+ return;
24750
+ }
24751
+ if (attrs._cachedRemote && this.#lastParams && sameValue(params, this.#lastParams)) return;
24752
+ let mounted;
24753
+ try {
24754
+ mounted = await this.binding.runtime.gramlot.src.remoteSource(node, name, params);
24755
+ } catch (error) {
24756
+ throw new Error(`${node.nodeTag} '${node.label}': remote '${name}' failed: ${error.message}`, { cause: error });
24757
+ }
24758
+ if (mounted) this.#lastParams = params;
24759
+ const onRemote = node.getAttr("_onRemote");
24760
+ if (mounted && onRemote) this.#inline("_onRemote", onRemote, onRemote, params);
24761
+ }
24762
+ #inline(attr, code, source, params) {
24763
+ return this.binding.runtime.inlineCompiler.compile(this.node, attr, code, Object.keys(params), source)(params);
24764
+ }
24765
+ };
24766
+ function sameValue(a, b) {
24767
+ if (Object.is(a, b)) return true;
24768
+ if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
24769
+ if (Array.isArray(a) && Array.isArray(b)) return a.length === b.length && a.every((item, i) => sameValue(item, b[i]));
24770
+ if (!isPlain(a) || !isPlain(b)) return false;
24771
+ const keys = Object.keys(a);
24772
+ return keys.length === Object.keys(b).length && keys.every((key) => Object.hasOwn(b, key) && sameValue(a[key], b[key]));
24773
+ }
24774
+ function isPlain(value) {
24775
+ return value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype;
24776
+ }
24777
+
24420
24778
  // src/binding/runtime.js
24421
- var PROVIDERS = { dataFormula: FormulaProvider, dataController: ControllerProvider, dataRpc: RpcProvider };
24779
+ var PROVIDERS = {
24780
+ dataFormula: FormulaProvider,
24781
+ dataController: ControllerProvider,
24782
+ dataRpc: RpcProvider,
24783
+ dataRemote: RemoteProvider
24784
+ };
24422
24785
  var CONTEXT_ATTRIBUTES = ["datapath", "_anchor", "node_id", "form", "formId"];
24423
24786
  var BindingRuntime = class {
24424
24787
  #gramlot;
@@ -24525,12 +24888,15 @@ var BindingRuntime = class {
24525
24888
  }
24526
24889
  /**
24527
24890
  * Redo the registrations of `node` and of its descendants (change of Data context), then
24528
- * project the branch again: the elements already built follow the new paths.
24891
+ * project the branch again: the elements already built follow the new paths, and a started
24892
+ * `remote` requests its fragment for the new context. Not in `rebind`: the fragment mounted by
24893
+ * `remoteSource` is a value event of the node itself, which rebinds it.
24529
24894
  */
24530
24895
  rebindBranch(node) {
24531
24896
  const nodes = this.#branchNodes(node);
24532
24897
  for (const each of nodes) this.bindingFor(each)?.rebind();
24533
24898
  for (const each of nodes) this.renderer.project(each);
24899
+ for (const each of nodes) this.bindingFor(each)?.remote?.update();
24534
24900
  }
24535
24901
  /**
24536
24902
  * Semantic work of one Source event, called inside the renderer FIFO (§5.2).
@@ -24673,11 +25039,12 @@ var BindingRuntime = class {
24673
25039
  }
24674
25040
  }
24675
25041
  }
24676
- /** The `_onStart` of a built node, once. */
25042
+ /** The `_onStart` of a built node, then the first request of its `remote`, once. */
24677
25043
  #start(binding) {
24678
25044
  if (binding.hasStamp("start")) return;
24679
25045
  binding.stamp("start");
24680
25046
  this.#lifecycle(binding, "start", "_onStart");
25047
+ binding.remote?.update();
24681
25048
  }
24682
25049
  /** Invoke the providers of `binding` for the lifecycle trigger `kind` when `attribute` is on; a number delays it. */
24683
25050
  #lifecycle(binding, kind, attribute) {
@@ -24746,6 +25113,9 @@ var NodeBinding = class {
24746
25113
  #registrations = [];
24747
25114
  #stamps = /* @__PURE__ */ new Set();
24748
25115
  #provider = null;
25116
+ #remote = null;
25117
+ // The registrations of `remote` and of the `remote_<k>` parameters: their changes request the fragment again.
25118
+ #remoteRegistrations = /* @__PURE__ */ new Set();
24749
25119
  // The clicks of a button, counted for its whole semantic life: a rebuild keeps them (P9, S12).
24750
25120
  #clickCount = 0;
24751
25121
  // The registration of the node's own variable datapath, read in the parent's context.
@@ -24767,10 +25137,14 @@ var NodeBinding = class {
24767
25137
  get registrations() {
24768
25138
  return [...this.#registrations];
24769
25139
  }
24770
- /** The Provider of a `dataFormula`, `dataController` or `dataRpc`, as a list of zero or one. */
25140
+ /** The Provider of a `dataFormula`, `dataController`, `dataRpc` or `dataRemote`, as a list of zero or one. */
24771
25141
  get providers() {
24772
25142
  return this.#provider ? [this.#provider] : [];
24773
25143
  }
25144
+ /** The RemoteBinding of a node whose `remote` attribute is a string, or null. */
25145
+ get remote() {
25146
+ return this.#remote;
25147
+ }
24774
25148
  /** The clicks counted by the node's ButtonBinding since the node entered the Source (P9). */
24775
25149
  get clickCount() {
24776
25150
  return this.#clickCount;
@@ -24792,11 +25166,13 @@ var NodeBinding = class {
24792
25166
  * pointer on a null path (Q7) is not registered. The `datapath` pointer is resolved in the
24793
25167
  * parent's context. A `dataSetter` registers nothing; a `dataFormula` or `dataController`
24794
25168
  * registers its `datapath` here and the rest through its Provider, created once (§5.1 step 4).
25169
+ * A node whose `remote` is a string gets its RemoteBinding, created once.
24795
25170
  */
24796
25171
  registerPointers() {
24797
25172
  const node = this.node;
24798
25173
  const dataElement = node._getMeta("data_element");
24799
25174
  if (dataElement && !PROVIDERS[node.nodeTag]) return;
25175
+ if (!dataElement && typeof node.getAttr("remote") === "string") this.#remote ??= new RemoteBinding(this);
24800
25176
  for (const [name, pointer] of node.pointers()) {
24801
25177
  if (dataElement && name !== "datapath") continue;
24802
25178
  const context = name === "datapath" ? node.parentNode : node;
@@ -24806,6 +25182,7 @@ var NodeBinding = class {
24806
25182
  const registration = this.runtime.router.register({ path, attr, recipient: this });
24807
25183
  this.#registrations.push(registration);
24808
25184
  if (name === "datapath") this.#datapathRegistration = registration;
25185
+ if (this.remote && (name === "remote" || name.startsWith("remote_"))) this.#remoteRegistrations.add(registration);
24809
25186
  }
24810
25187
  if (!dataElement) return;
24811
25188
  this.#provider ??= new PROVIDERS[node.nodeTag](this);
@@ -24816,10 +25193,17 @@ var NodeBinding = class {
24816
25193
  this.#closeRegistrations();
24817
25194
  this.registerPointers();
24818
25195
  }
24819
- /** Router recipient: the node's datapath rebinds its branch; any other registration projects the node. */
25196
+ /**
25197
+ * Router recipient: the node's datapath rebinds its branch; any other registration projects the node,
25198
+ * after a registration of `remote` or of a `remote_<k>` parameter requested the fragment again.
25199
+ */
24820
25200
  receive(change) {
24821
- if (change.registration === this.#datapathRegistration) this.runtime.rebindBranch(this.node);
24822
- else this.runtime.renderer.project(this.node, change);
25201
+ if (change.registration === this.#datapathRegistration) {
25202
+ this.runtime.rebindBranch(this.node);
25203
+ return;
25204
+ }
25205
+ if (this.#remoteRegistrations.has(change.registration)) this.remote.update();
25206
+ this.runtime.renderer.project(this.node, change);
24823
25207
  }
24824
25208
  /** Own a semantic resource until close; the returned function releases it without running it. */
24825
25209
  track(disposer) {
@@ -24848,6 +25232,7 @@ var NodeBinding = class {
24848
25232
  #closeRegistrations() {
24849
25233
  for (const registration of this.#registrations.splice(0)) registration.close();
24850
25234
  this.#datapathRegistration = null;
25235
+ this.#remoteRegistrations.clear();
24851
25236
  }
24852
25237
  };
24853
25238
 
@@ -25743,8 +26128,8 @@ var SourceHandler = class extends Handler {
25743
26128
  * The request lives as long as the target's NodeBinding (P24): a DOM rebuild
25744
26129
  * or a freeze keeps it, the removal of the target cancels it.
25745
26130
  *
25746
- * Source methods (`@source`, `registerSource`, `remoteSource`) are not yet part of the page-writing API:
25747
- * they arrive together with the `remote` grammar attribute and `@endpoint`.
26131
+ * `@source` / `registerSource` declare the fragments this method requests (GC-230 §035); the
26132
+ * `remote` attribute requests them declaratively (GC-095 §105).
25748
26133
  */
25749
26134
  async remoteSource(target, method, params = {}) {
25750
26135
  const rpc = this.gramlot.rpc;
@@ -26109,8 +26494,8 @@ var Page = class {
26109
26494
  * Mark the own method `methodName` of this page class as a remote Source method,
26110
26495
  * equivalent to Python `@source`; call it after the class.
26111
26496
  *
26112
- * Source methods (`@source`, `registerSource`, `remoteSource`) are not yet part of the page-writing API:
26113
- * they arrive together with the `remote` grammar attribute.
26497
+ * A declared fragment is requested from code with `gramlot.src.remoteSource` (GC-230 §035); the
26498
+ * declarative `remote` grammar attribute is not yet available.
26114
26499
  */
26115
26500
  static registerSource(methodName, { auth = null } = {}) {
26116
26501
  declare(this, methodName, SOURCE_METHOD, auth);