@basictech/react 0.8.0-beta.3 → 0.8.0-beta.4

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.
@@ -38,6 +38,7 @@ export const syncProtocol = function () {
38
38
  var requestId = 0;
39
39
  var acceptCallbacks = {};
40
40
  var refreshTimer = null;
41
+ var pendingTokenUpdate = null;
41
42
 
42
43
  // Connect the WebSocket to given url:
43
44
  log("Connecting to", url)
@@ -81,6 +82,13 @@ export const syncProtocol = function () {
81
82
  }
82
83
  }
83
84
 
85
+ function sendTokenUpdate(token) {
86
+ if (ws.readyState !== WebSocket.OPEN) return false;
87
+ pendingTokenUpdate = token;
88
+ ws.send(JSON.stringify({ type: "tokenUpdate", authToken: token }));
89
+ return true;
90
+ }
91
+
84
92
  // Resolve the getToken function from the module-level registry.
85
93
  // It's stored there (not in options) because dexie-syncable serializes
86
94
  // options into IndexedDB, and functions can't survive structured clone.
@@ -103,10 +111,8 @@ export const syncProtocol = function () {
103
111
  refreshTimer = setTimeout(async function () {
104
112
  try {
105
113
  var newToken = await resolveGetToken()({ forceRefresh: true });
106
- if (ws.readyState === WebSocket.OPEN) {
114
+ if (sendTokenUpdate(newToken)) {
107
115
  log("Sending tokenUpdate on existing WebSocket");
108
- ws.send(JSON.stringify({ type: "tokenUpdate", authToken: newToken }));
109
- scheduleTokenRefresh(newToken);
110
116
  }
111
117
  } catch (err) {
112
118
  log("Proactive token refresh failed (non-fatal):", err);
@@ -145,10 +151,7 @@ export const syncProtocol = function () {
145
151
  if (document.visibilityState === 'visible' && ws.readyState === WebSocket.OPEN) {
146
152
  log("Page became visible - refreshing token for WebSocket");
147
153
  resolveGetToken()({ forceRefresh: true }).then(function(newToken) {
148
- if (ws.readyState === WebSocket.OPEN) {
149
- ws.send(JSON.stringify({ type: "tokenUpdate", authToken: newToken }));
150
- scheduleTokenRefresh(newToken);
151
- }
154
+ sendTokenUpdate(newToken);
152
155
  }).catch(function(err) {
153
156
  log("Token refresh on visibility resume failed:", err);
154
157
  });
@@ -246,6 +249,19 @@ export const syncProtocol = function () {
246
249
  });
247
250
  isFirstRound = false;
248
251
  }
252
+ } else if (requestFromServer.type == "tokenUpdateAck") {
253
+ if (requestFromServer.ok) {
254
+ scheduleTokenRefresh(requestFromServer.authToken || pendingTokenUpdate);
255
+ pendingTokenUpdate = null;
256
+ } else {
257
+ log("tokenUpdate rejected by server:", requestFromServer.code || requestFromServer.message);
258
+ pendingTokenUpdate = null;
259
+ ws.close(4001, requestFromServer.code || "token_update_failed");
260
+ onError(
261
+ requestFromServer.message || "Authentication refresh failed",
262
+ RECONNECT_DELAY,
263
+ );
264
+ }
249
265
  } else if (requestFromServer.type == "ack") {
250
266
  var requestId = requestFromServer.requestId;
251
267
  var acceptCallback = acceptCallbacks[requestId.toString()];