@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gramlot/gramlot",
3
- "version": "0.3.39",
3
+ "version": "0.3.41",
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": {
@@ -69,6 +69,11 @@ export class DataBinder {
69
69
  }
70
70
  }
71
71
 
72
+ suspend() {
73
+ this.ready = false;
74
+ this.binding.runtime.data.popNode(this.clientPath, REMOTE);
75
+ }
76
+
72
77
  unregister() {
73
78
  this.ready = false;
74
79
  this.registration?.close();
@@ -7,6 +7,7 @@ import {RpcError} from './handlers/rpc-handler.js';
7
7
  export class PageStoreConnection {
8
8
  constructor(gramlot, url, document) {
9
9
  this.gramlot = gramlot;
10
+ this.canReconnect = gramlot.capabilities.includes('pageStoreReconnect');
10
11
  this.window = document.defaultView;
11
12
  this.frames = gramlot._frames;
12
13
  this.binders = new Map();
@@ -17,6 +18,9 @@ export class PageStoreConnection {
17
18
  this.closed = false;
18
19
  this.ready = false;
19
20
  this.socket = null;
21
+ this.reconnecting = false;
22
+ this.retryTimer = null;
23
+ this.retryDelay = 250;
20
24
  const address = new URL(url, document.baseURI);
21
25
  address.protocol = address.protocol === 'https:' ? 'wss:' : 'ws:';
22
26
  this.endpoint = address.href;
@@ -49,18 +53,46 @@ export class PageStoreConnection {
49
53
  open() {
50
54
  const address = this.address;
51
55
  address.searchParams.set('pageId', this.gramlot.pageId);
52
- this.socket = new this.window.WebSocket(address.href);
53
- this.socket.addEventListener('open', () => this.connected());
54
- this.socket.addEventListener('message', event => {
56
+ const socket = this.socket = new this.window.WebSocket(address.href);
57
+ socket.addEventListener('open', () => {
58
+ if (this.socket === socket) this.connected();
59
+ });
60
+ socket.addEventListener('message', event => {
61
+ if (this.socket !== socket) return;
55
62
  try { this.deliver(event.data); }
56
63
  catch (error) { this.fail(error); }
57
64
  });
58
- this.socket.addEventListener('error', () => this.fail(new Error('PageStore WebSocket failed')));
59
- this.socket.addEventListener('close', event => {
60
- if (!this.closed) this.fail(new Error('PageStore disconnected (' + event.code + ')'));
65
+ socket.addEventListener('error', () => {
66
+ // A browser connection error also dispatches close, which schedules the retry.
67
+ if (this.socket === socket && !this.canReconnect) this.fail(new Error('PageStore WebSocket failed'));
68
+ });
69
+ socket.addEventListener('close', event => {
70
+ if (this.closed || this.socket !== socket) return;
71
+ const error = new Error('PageStore disconnected (' + event.code + ')');
72
+ if (!this.canReconnect || (!this.ready && !this.reconnecting)) {
73
+ this.fail(error);
74
+ return;
75
+ }
76
+ this.socket = null;
77
+ this.suspend(error);
78
+ this.retryTimer = this.window.setTimeout(() => {
79
+ this.retryTimer = null;
80
+ this.open();
81
+ }, this.retryDelay);
82
+ this.retryDelay = Math.min(this.retryDelay * 2, 10000);
61
83
  });
62
84
  }
63
85
 
86
+ suspend(error) {
87
+ if (this.reconnecting) return;
88
+ this.ready = false;
89
+ this.reconnecting = true;
90
+ this.routes.clear();
91
+ for (const call of this.calls.values()) call.finish(error);
92
+ for (const binder of this.binders.values()) binder.suspend();
93
+ this.frames.setChannelState('server', {endpoint: this.endpoint, ready: false, reconnecting: true});
94
+ }
95
+
64
96
  peerChanged(peer) {
65
97
  if (this.closed || !peer.parent || !this.parent) return;
66
98
  const state = peer.channels.server;
@@ -71,12 +103,15 @@ export class PageStoreConnection {
71
103
  } else if (peer.origin !== this.origin || state?.endpoint !== this.endpoint) {
72
104
  this.fail(new Error('Parent does not provide the same PageStore endpoint'));
73
105
  } else if (state.ready) this.connected();
106
+ else if (state.reconnecting && this.canReconnect) this.suspend(new Error('PageStore parent disconnected'));
74
107
  else if (this.ready) this.fail(new Error('PageStore parent channel closed'));
75
108
  }
76
109
 
77
110
  connected() {
78
111
  if (this.closed || this.ready) return;
79
112
  this.ready = true;
113
+ this.reconnecting = false;
114
+ this.retryDelay = 250;
80
115
  if (this.parent) this.upstream({op: 'attach', pageId: this.gramlot.pageId});
81
116
  for (const binder of this.binders.values()) this.subscribe(binder);
82
117
  for (const call of this.calls.values()) this.sendCall(call);
@@ -115,7 +150,7 @@ export class PageStoreConnection {
115
150
  }
116
151
 
117
152
  deliver(wire) {
118
- if (this.closed) return;
153
+ if (this.closed || this.reconnecting) return;
119
154
  const message = fromTytx(wire);
120
155
  if (message.pageId === this.gramlot.pageId) {
121
156
  if (message.op === 'closed') {
@@ -175,6 +210,7 @@ export class PageStoreConnection {
175
210
  /** Queue until attached, then pair responses by id independently of binder changes. */
176
211
  call(text, signal) {
177
212
  if (this.closed) return Promise.reject(this.error ?? new Error('PageStore connection is closed'));
213
+ if (this.reconnecting) return Promise.reject(new Error('PageStore is reconnecting'));
178
214
  if (signal?.aborted) return Promise.reject(signal.reason);
179
215
  const message = fromTytx(text);
180
216
  if (message.pageId !== this.gramlot.pageId || typeof message.id !== 'string'
@@ -225,6 +261,7 @@ export class PageStoreConnection {
225
261
  close() {
226
262
  if (this.closed) return;
227
263
  this.closed = true;
264
+ if (this.retryTimer !== null) this.window.clearTimeout(this.retryTimer);
228
265
  if (this.parent && this.ready && this.frames.isConnected(this.parent)) {
229
266
  for (const pageId of [...this.routes.keys(), this.gramlot.pageId]) {
230
267
  this.frames.send(this.parent, 'server', {op: 'detach', pageId});
@@ -336,7 +336,7 @@
336
336
 
337
337
  /* Chat controls reserve focus paint inside their measured composer regions. */
338
338
  .gramlot-chat-editor:focus-visible { outline:2px solid var(--gramlot-focus-ring);outline-offset:-2px; }
339
- .gramlot-chat-send-icon { display:block;flex:none;width:16px;height:16px;background:currentColor;
339
+ .gramlot-chat-send-icon { pointer-events:none;display:block;flex:none;width:16px;height:16px;background:currentColor;
340
340
  mask:center/contain no-repeat url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 19V5M5 12l7-7 7 7' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); }
341
341
 
342
342
  /* Reusable VisualEditor composition. */