@emeryld/rrroutes-client 2.6.1 → 2.6.2
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/index.cjs +79 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +79 -29
- package/dist/index.mjs.map +1 -1
- package/dist/sockets/socket.client.context.d.ts +3 -3
- package/dist/sockets/socket.client.index.d.ts +2 -2
- package/dist/sockets/socketedRoute/socket.client.helper.d.ts +3 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1557,7 +1557,9 @@ function useSocketConnection(args) {
|
|
|
1557
1557
|
React.useEffect(() => {
|
|
1558
1558
|
if (autoJoin && normalizedRooms.length > 0)
|
|
1559
1559
|
client.joinRooms(normalizedRooms, args.joinMeta);
|
|
1560
|
-
const unsubscribe = client.on(event,
|
|
1560
|
+
const unsubscribe = client.on(event, (payload, meta) => {
|
|
1561
|
+
onMessage(payload, meta);
|
|
1562
|
+
});
|
|
1561
1563
|
return () => {
|
|
1562
1564
|
unsubscribe();
|
|
1563
1565
|
if (autoLeave && normalizedRooms.length > 0)
|
|
@@ -1894,28 +1896,58 @@ function buildSocketedRoute(options) {
|
|
|
1894
1896
|
}
|
|
1895
1897
|
});
|
|
1896
1898
|
if (!client) return;
|
|
1899
|
+
const queue = [];
|
|
1900
|
+
let draining = false;
|
|
1901
|
+
let active = true;
|
|
1902
|
+
const drainQueue = () => {
|
|
1903
|
+
if (!active || draining) return;
|
|
1904
|
+
draining = true;
|
|
1905
|
+
try {
|
|
1906
|
+
while (active && queue.length > 0) {
|
|
1907
|
+
const nextUpdate = queue.shift();
|
|
1908
|
+
if (!nextUpdate) continue;
|
|
1909
|
+
built.setData(
|
|
1910
|
+
(prev) => {
|
|
1911
|
+
const next = nextUpdate.fn(
|
|
1912
|
+
prev,
|
|
1913
|
+
nextUpdate.payload,
|
|
1914
|
+
nextUpdate.meta ? { ...nextUpdate.meta, args: useArgs } : { args: useArgs }
|
|
1915
|
+
);
|
|
1916
|
+
const nextRoomState = roomsFromData(
|
|
1917
|
+
next,
|
|
1918
|
+
toRooms
|
|
1919
|
+
);
|
|
1920
|
+
setRoomState(
|
|
1921
|
+
(prevRoomState) => roomStateEqual(prevRoomState, nextRoomState) ? prevRoomState : nextRoomState
|
|
1922
|
+
);
|
|
1923
|
+
return next;
|
|
1924
|
+
},
|
|
1925
|
+
...useArgs
|
|
1926
|
+
);
|
|
1927
|
+
}
|
|
1928
|
+
} finally {
|
|
1929
|
+
draining = false;
|
|
1930
|
+
if (active && queue.length > 0) drainQueue();
|
|
1931
|
+
}
|
|
1932
|
+
};
|
|
1897
1933
|
const entries = Object.entries(applySocket).filter(
|
|
1898
1934
|
([_event, fn]) => typeof fn === "function"
|
|
1899
1935
|
);
|
|
1900
1936
|
const unsubscribes = entries.map(
|
|
1901
1937
|
([ev, fn]) => client.on(ev, (payload, meta) => {
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
);
|
|
1909
|
-
setRoomState(
|
|
1910
|
-
(prevRoomState) => roomStateEqual(prevRoomState, nextRoomState) ? prevRoomState : nextRoomState
|
|
1911
|
-
);
|
|
1912
|
-
return next;
|
|
1913
|
-
},
|
|
1914
|
-
...useArgs
|
|
1915
|
-
);
|
|
1938
|
+
queue.push({
|
|
1939
|
+
fn,
|
|
1940
|
+
payload,
|
|
1941
|
+
...meta ? { meta } : {}
|
|
1942
|
+
});
|
|
1943
|
+
drainQueue();
|
|
1916
1944
|
})
|
|
1917
1945
|
);
|
|
1918
|
-
return () =>
|
|
1946
|
+
return () => {
|
|
1947
|
+
active = false;
|
|
1948
|
+
queue.length = 0;
|
|
1949
|
+
unsubscribes.forEach((u) => u?.());
|
|
1950
|
+
};
|
|
1919
1951
|
}, [client, applySocket, built, argsKey, toRooms, debug]);
|
|
1920
1952
|
return { ...endpointResult, rooms: roomState.rooms };
|
|
1921
1953
|
};
|
|
@@ -2428,9 +2460,9 @@ var SocketClient = class {
|
|
|
2428
2460
|
};
|
|
2429
2461
|
}
|
|
2430
2462
|
const socket = this.socket;
|
|
2431
|
-
const
|
|
2432
|
-
|
|
2433
|
-
const rawData =
|
|
2463
|
+
const toStringList = (value) => Array.isArray(value) ? value.filter((entry2) => typeof entry2 === "string") : [];
|
|
2464
|
+
const wrappedEnv = (envelope) => {
|
|
2465
|
+
const rawData = envelope.data;
|
|
2434
2466
|
const parsed = schema.safeParse(rawData);
|
|
2435
2467
|
if (!parsed.success) {
|
|
2436
2468
|
this.dbg({
|
|
@@ -2441,15 +2473,17 @@ var SocketClient = class {
|
|
|
2441
2473
|
});
|
|
2442
2474
|
return;
|
|
2443
2475
|
}
|
|
2476
|
+
const data = parsed.data;
|
|
2444
2477
|
const receivedAt = /* @__PURE__ */ new Date();
|
|
2445
|
-
const sentAt =
|
|
2478
|
+
const sentAt = envelope?.sentAt ? new Date(envelope.sentAt) : void 0;
|
|
2446
2479
|
const meta = {
|
|
2447
2480
|
envelope: {
|
|
2448
|
-
eventName:
|
|
2449
|
-
sentAt:
|
|
2450
|
-
sentTo:
|
|
2451
|
-
data
|
|
2452
|
-
metadata:
|
|
2481
|
+
eventName: typeof envelope?.eventName === "string" ? envelope.eventName : event,
|
|
2482
|
+
sentAt: envelope?.sentAt ?? receivedAt.toISOString(),
|
|
2483
|
+
sentTo: toStringList(envelope?.sentTo),
|
|
2484
|
+
data,
|
|
2485
|
+
metadata: envelope?.metadata,
|
|
2486
|
+
rooms: toStringList(envelope?.rooms)
|
|
2453
2487
|
},
|
|
2454
2488
|
ctx: {
|
|
2455
2489
|
receivedAt,
|
|
@@ -2469,22 +2503,38 @@ var SocketClient = class {
|
|
|
2469
2503
|
metadata: meta.envelope.metadata
|
|
2470
2504
|
} : void 0
|
|
2471
2505
|
});
|
|
2472
|
-
handler(
|
|
2506
|
+
handler(data, meta);
|
|
2507
|
+
};
|
|
2508
|
+
const wrappedDispatcher = (envelopeOrRaw) => {
|
|
2509
|
+
if (typeof envelopeOrRaw === "object" && envelopeOrRaw !== null && "eventName" in envelopeOrRaw && "sentAt" in envelopeOrRaw && "sentTo" in envelopeOrRaw && "data" in envelopeOrRaw) {
|
|
2510
|
+
wrappedEnv(envelopeOrRaw);
|
|
2511
|
+
} else {
|
|
2512
|
+
this.dbg({
|
|
2513
|
+
type: "receive",
|
|
2514
|
+
event,
|
|
2515
|
+
envelope: void 0
|
|
2516
|
+
});
|
|
2517
|
+
handler(envelopeOrRaw, void 0);
|
|
2518
|
+
}
|
|
2473
2519
|
};
|
|
2474
2520
|
const errorWrapped = (e) => {
|
|
2475
2521
|
this.dbg({ type: "receive", event, err: String(e) });
|
|
2476
2522
|
};
|
|
2477
|
-
socket.on(String(event),
|
|
2523
|
+
socket.on(String(event), wrappedDispatcher);
|
|
2478
2524
|
socket.on(`${String(event)}:error`, errorWrapped);
|
|
2479
2525
|
let set = this.handlerMap.get(String(event));
|
|
2480
2526
|
if (!set) {
|
|
2481
2527
|
set = /* @__PURE__ */ new Set();
|
|
2482
2528
|
this.handlerMap.set(String(event), set);
|
|
2483
2529
|
}
|
|
2484
|
-
const entry = {
|
|
2530
|
+
const entry = {
|
|
2531
|
+
orig: handler,
|
|
2532
|
+
wrapped: wrappedDispatcher,
|
|
2533
|
+
errorWrapped
|
|
2534
|
+
};
|
|
2485
2535
|
set.add(entry);
|
|
2486
2536
|
return () => {
|
|
2487
|
-
socket.off(String(event),
|
|
2537
|
+
socket.off(String(event), wrappedDispatcher);
|
|
2488
2538
|
socket.off(`${String(event)}:error`, errorWrapped);
|
|
2489
2539
|
const s = this.handlerMap.get(String(event));
|
|
2490
2540
|
if (s) {
|