@gramlot/gramlot 0.3.39 → 0.3.41

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
@@ -15993,6 +15993,7 @@ var RpcHandler = class extends Handler {
15993
15993
  var PageStoreConnection = class {
15994
15994
  constructor(gramlot, url, document) {
15995
15995
  this.gramlot = gramlot;
15996
+ this.canReconnect = gramlot.capabilities.includes("pageStoreReconnect");
15996
15997
  this.window = document.defaultView;
15997
15998
  this.frames = gramlot._frames;
15998
15999
  this.binders = /* @__PURE__ */ new Map();
@@ -16002,6 +16003,9 @@ var PageStoreConnection = class {
16002
16003
  this.closed = false;
16003
16004
  this.ready = false;
16004
16005
  this.socket = null;
16006
+ this.reconnecting = false;
16007
+ this.retryTimer = null;
16008
+ this.retryDelay = 250;
16005
16009
  const address = new URL(url, document.baseURI);
16006
16010
  address.protocol = address.protocol === "https:" ? "wss:" : "ws:";
16007
16011
  this.endpoint = address.href;
@@ -16035,20 +16039,46 @@ var PageStoreConnection = class {
16035
16039
  open() {
16036
16040
  const address = this.address;
16037
16041
  address.searchParams.set("pageId", this.gramlot.pageId);
16038
- this.socket = new this.window.WebSocket(address.href);
16039
- this.socket.addEventListener("open", () => this.connected());
16040
- this.socket.addEventListener("message", (event) => {
16042
+ const socket = this.socket = new this.window.WebSocket(address.href);
16043
+ socket.addEventListener("open", () => {
16044
+ if (this.socket === socket) this.connected();
16045
+ });
16046
+ socket.addEventListener("message", (event) => {
16047
+ if (this.socket !== socket) return;
16041
16048
  try {
16042
16049
  this.deliver(event.data);
16043
16050
  } catch (error) {
16044
16051
  this.fail(error);
16045
16052
  }
16046
16053
  });
16047
- this.socket.addEventListener("error", () => this.fail(new Error("PageStore WebSocket failed")));
16048
- this.socket.addEventListener("close", (event) => {
16049
- if (!this.closed) this.fail(new Error("PageStore disconnected (" + event.code + ")"));
16054
+ socket.addEventListener("error", () => {
16055
+ if (this.socket === socket && !this.canReconnect) this.fail(new Error("PageStore WebSocket failed"));
16056
+ });
16057
+ socket.addEventListener("close", (event) => {
16058
+ if (this.closed || this.socket !== socket) return;
16059
+ const error = new Error("PageStore disconnected (" + event.code + ")");
16060
+ if (!this.canReconnect || !this.ready && !this.reconnecting) {
16061
+ this.fail(error);
16062
+ return;
16063
+ }
16064
+ this.socket = null;
16065
+ this.suspend(error);
16066
+ this.retryTimer = this.window.setTimeout(() => {
16067
+ this.retryTimer = null;
16068
+ this.open();
16069
+ }, this.retryDelay);
16070
+ this.retryDelay = Math.min(this.retryDelay * 2, 1e4);
16050
16071
  });
16051
16072
  }
16073
+ suspend(error) {
16074
+ if (this.reconnecting) return;
16075
+ this.ready = false;
16076
+ this.reconnecting = true;
16077
+ this.routes.clear();
16078
+ for (const call of this.calls.values()) call.finish(error);
16079
+ for (const binder of this.binders.values()) binder.suspend();
16080
+ this.frames.setChannelState("server", { endpoint: this.endpoint, ready: false, reconnecting: true });
16081
+ }
16052
16082
  peerChanged(peer) {
16053
16083
  if (this.closed || !peer.parent || !this.parent) return;
16054
16084
  const state = peer.channels.server;
@@ -16058,11 +16088,14 @@ var PageStoreConnection = class {
16058
16088
  } else if (peer.origin !== this.origin || state?.endpoint !== this.endpoint) {
16059
16089
  this.fail(new Error("Parent does not provide the same PageStore endpoint"));
16060
16090
  } else if (state.ready) this.connected();
16091
+ else if (state.reconnecting && this.canReconnect) this.suspend(new Error("PageStore parent disconnected"));
16061
16092
  else if (this.ready) this.fail(new Error("PageStore parent channel closed"));
16062
16093
  }
16063
16094
  connected() {
16064
16095
  if (this.closed || this.ready) return;
16065
16096
  this.ready = true;
16097
+ this.reconnecting = false;
16098
+ this.retryDelay = 250;
16066
16099
  if (this.parent) this.upstream({ op: "attach", pageId: this.gramlot.pageId });
16067
16100
  for (const binder of this.binders.values()) this.subscribe(binder);
16068
16101
  for (const call of this.calls.values()) this.sendCall(call);
@@ -16097,7 +16130,7 @@ var PageStoreConnection = class {
16097
16130
  else this.socket.send(toTytx(message));
16098
16131
  }
16099
16132
  deliver(wire) {
16100
- if (this.closed) return;
16133
+ if (this.closed || this.reconnecting) return;
16101
16134
  const message = fromTytx(wire);
16102
16135
  if (message.pageId === this.gramlot.pageId) {
16103
16136
  if (message.op === "closed") {
@@ -16154,6 +16187,7 @@ var PageStoreConnection = class {
16154
16187
  /** Queue until attached, then pair responses by id independently of binder changes. */
16155
16188
  call(text3, signal) {
16156
16189
  if (this.closed) return Promise.reject(this.error ?? new Error("PageStore connection is closed"));
16190
+ if (this.reconnecting) return Promise.reject(new Error("PageStore is reconnecting"));
16157
16191
  if (signal?.aborted) return Promise.reject(signal.reason);
16158
16192
  const message = fromTytx(text3);
16159
16193
  if (message.pageId !== this.gramlot.pageId || typeof message.id !== "string" || !message.id || Object.hasOwn(message, "op") && message.op !== "publish" || this.calls.has(message.id)) {
@@ -16203,6 +16237,7 @@ var PageStoreConnection = class {
16203
16237
  close() {
16204
16238
  if (this.closed) return;
16205
16239
  this.closed = true;
16240
+ if (this.retryTimer !== null) this.window.clearTimeout(this.retryTimer);
16206
16241
  if (this.parent && this.ready && this.frames.isConnected(this.parent)) {
16207
16242
  for (const pageId of [...this.routes.keys(), this.gramlot.pageId]) {
16208
16243
  this.frames.send(this.parent, "server", { op: "detach", pageId });
@@ -25627,6 +25662,10 @@ var DataBinder = class {
25627
25662
  throw new Error("Unknown PageStore delivery");
25628
25663
  }
25629
25664
  }
25665
+ suspend() {
25666
+ this.ready = false;
25667
+ this.binding.runtime.data.popNode(this.clientPath, REMOTE);
25668
+ }
25630
25669
  unregister() {
25631
25670
  this.ready = false;
25632
25671
  this.registration?.close();