@johnhenry/browsermesh-transport 0.5.0 → 0.5.1
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 +1 -1
- package/src/websocket.mjs +12 -2
package/package.json
CHANGED
package/src/websocket.mjs
CHANGED
|
@@ -165,6 +165,9 @@ export class WebSocketTransport {
|
|
|
165
165
|
/** @type {Set<*>} Outstanding jitter timers, cleared on close */
|
|
166
166
|
#jitterTimers = new Set();
|
|
167
167
|
|
|
168
|
+
/** FIFO delivery is independent of timer scheduling and clock rounding. */
|
|
169
|
+
#jitterQueue = [];
|
|
170
|
+
|
|
168
171
|
/** @type {number} Earliest time the next jittered send may fire (keeps order) */
|
|
169
172
|
#nextSendAt = 0;
|
|
170
173
|
|
|
@@ -320,10 +323,16 @@ export class WebSocketTransport {
|
|
|
320
323
|
const now = Date.now();
|
|
321
324
|
const at = Math.max(now + Math.random() * this.#jitterMs, this.#nextSendAt);
|
|
322
325
|
this.#nextSendAt = at;
|
|
326
|
+
const entry = { wire, ws, ready: false };
|
|
327
|
+
this.#jitterQueue.push(entry);
|
|
323
328
|
const timer = setTimeout(() => {
|
|
324
329
|
this.#jitterTimers.delete(timer);
|
|
325
|
-
|
|
326
|
-
|
|
330
|
+
entry.ready = true;
|
|
331
|
+
while (this.#jitterQueue[0]?.ready) {
|
|
332
|
+
const next = this.#jitterQueue.shift();
|
|
333
|
+
if (this.#ws !== next.ws || !this.connected) continue;
|
|
334
|
+
try { next.ws.send(next.wire); } catch (e) { this._fireEvent('error', e); }
|
|
335
|
+
}
|
|
327
336
|
}, Math.max(0, at - now));
|
|
328
337
|
this.#jitterTimers.add(timer);
|
|
329
338
|
}
|
|
@@ -332,6 +341,7 @@ export class WebSocketTransport {
|
|
|
332
341
|
#clearJitter() {
|
|
333
342
|
for (const t of this.#jitterTimers) clearTimeout(t);
|
|
334
343
|
this.#jitterTimers.clear();
|
|
344
|
+
this.#jitterQueue.length = 0;
|
|
335
345
|
this.#nextSendAt = 0;
|
|
336
346
|
}
|
|
337
347
|
|