@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.cjs
CHANGED
|
@@ -1593,7 +1593,9 @@ function useSocketConnection(args) {
|
|
|
1593
1593
|
React.useEffect(() => {
|
|
1594
1594
|
if (autoJoin && normalizedRooms.length > 0)
|
|
1595
1595
|
client.joinRooms(normalizedRooms, args.joinMeta);
|
|
1596
|
-
const unsubscribe = client.on(event,
|
|
1596
|
+
const unsubscribe = client.on(event, (payload, meta) => {
|
|
1597
|
+
onMessage(payload, meta);
|
|
1598
|
+
});
|
|
1597
1599
|
return () => {
|
|
1598
1600
|
unsubscribe();
|
|
1599
1601
|
if (autoLeave && normalizedRooms.length > 0)
|
|
@@ -1930,28 +1932,58 @@ function buildSocketedRoute(options) {
|
|
|
1930
1932
|
}
|
|
1931
1933
|
});
|
|
1932
1934
|
if (!client) return;
|
|
1935
|
+
const queue = [];
|
|
1936
|
+
let draining = false;
|
|
1937
|
+
let active = true;
|
|
1938
|
+
const drainQueue = () => {
|
|
1939
|
+
if (!active || draining) return;
|
|
1940
|
+
draining = true;
|
|
1941
|
+
try {
|
|
1942
|
+
while (active && queue.length > 0) {
|
|
1943
|
+
const nextUpdate = queue.shift();
|
|
1944
|
+
if (!nextUpdate) continue;
|
|
1945
|
+
built.setData(
|
|
1946
|
+
(prev) => {
|
|
1947
|
+
const next = nextUpdate.fn(
|
|
1948
|
+
prev,
|
|
1949
|
+
nextUpdate.payload,
|
|
1950
|
+
nextUpdate.meta ? { ...nextUpdate.meta, args: useArgs } : { args: useArgs }
|
|
1951
|
+
);
|
|
1952
|
+
const nextRoomState = roomsFromData(
|
|
1953
|
+
next,
|
|
1954
|
+
toRooms
|
|
1955
|
+
);
|
|
1956
|
+
setRoomState(
|
|
1957
|
+
(prevRoomState) => roomStateEqual(prevRoomState, nextRoomState) ? prevRoomState : nextRoomState
|
|
1958
|
+
);
|
|
1959
|
+
return next;
|
|
1960
|
+
},
|
|
1961
|
+
...useArgs
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
} finally {
|
|
1965
|
+
draining = false;
|
|
1966
|
+
if (active && queue.length > 0) drainQueue();
|
|
1967
|
+
}
|
|
1968
|
+
};
|
|
1933
1969
|
const entries = Object.entries(applySocket).filter(
|
|
1934
1970
|
([_event, fn]) => typeof fn === "function"
|
|
1935
1971
|
);
|
|
1936
1972
|
const unsubscribes = entries.map(
|
|
1937
1973
|
([ev, fn]) => client.on(ev, (payload, meta) => {
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
);
|
|
1945
|
-
setRoomState(
|
|
1946
|
-
(prevRoomState) => roomStateEqual(prevRoomState, nextRoomState) ? prevRoomState : nextRoomState
|
|
1947
|
-
);
|
|
1948
|
-
return next;
|
|
1949
|
-
},
|
|
1950
|
-
...useArgs
|
|
1951
|
-
);
|
|
1974
|
+
queue.push({
|
|
1975
|
+
fn,
|
|
1976
|
+
payload,
|
|
1977
|
+
...meta ? { meta } : {}
|
|
1978
|
+
});
|
|
1979
|
+
drainQueue();
|
|
1952
1980
|
})
|
|
1953
1981
|
);
|
|
1954
|
-
return () =>
|
|
1982
|
+
return () => {
|
|
1983
|
+
active = false;
|
|
1984
|
+
queue.length = 0;
|
|
1985
|
+
unsubscribes.forEach((u) => u?.());
|
|
1986
|
+
};
|
|
1955
1987
|
}, [client, applySocket, built, argsKey, toRooms, debug]);
|
|
1956
1988
|
return { ...endpointResult, rooms: roomState.rooms };
|
|
1957
1989
|
};
|
|
@@ -2464,9 +2496,9 @@ var SocketClient = class {
|
|
|
2464
2496
|
};
|
|
2465
2497
|
}
|
|
2466
2498
|
const socket = this.socket;
|
|
2467
|
-
const
|
|
2468
|
-
|
|
2469
|
-
const rawData =
|
|
2499
|
+
const toStringList = (value) => Array.isArray(value) ? value.filter((entry2) => typeof entry2 === "string") : [];
|
|
2500
|
+
const wrappedEnv = (envelope) => {
|
|
2501
|
+
const rawData = envelope.data;
|
|
2470
2502
|
const parsed = schema.safeParse(rawData);
|
|
2471
2503
|
if (!parsed.success) {
|
|
2472
2504
|
this.dbg({
|
|
@@ -2477,15 +2509,17 @@ var SocketClient = class {
|
|
|
2477
2509
|
});
|
|
2478
2510
|
return;
|
|
2479
2511
|
}
|
|
2512
|
+
const data = parsed.data;
|
|
2480
2513
|
const receivedAt = /* @__PURE__ */ new Date();
|
|
2481
|
-
const sentAt =
|
|
2514
|
+
const sentAt = envelope?.sentAt ? new Date(envelope.sentAt) : void 0;
|
|
2482
2515
|
const meta = {
|
|
2483
2516
|
envelope: {
|
|
2484
|
-
eventName:
|
|
2485
|
-
sentAt:
|
|
2486
|
-
sentTo:
|
|
2487
|
-
data
|
|
2488
|
-
metadata:
|
|
2517
|
+
eventName: typeof envelope?.eventName === "string" ? envelope.eventName : event,
|
|
2518
|
+
sentAt: envelope?.sentAt ?? receivedAt.toISOString(),
|
|
2519
|
+
sentTo: toStringList(envelope?.sentTo),
|
|
2520
|
+
data,
|
|
2521
|
+
metadata: envelope?.metadata,
|
|
2522
|
+
rooms: toStringList(envelope?.rooms)
|
|
2489
2523
|
},
|
|
2490
2524
|
ctx: {
|
|
2491
2525
|
receivedAt,
|
|
@@ -2505,22 +2539,38 @@ var SocketClient = class {
|
|
|
2505
2539
|
metadata: meta.envelope.metadata
|
|
2506
2540
|
} : void 0
|
|
2507
2541
|
});
|
|
2508
|
-
handler(
|
|
2542
|
+
handler(data, meta);
|
|
2543
|
+
};
|
|
2544
|
+
const wrappedDispatcher = (envelopeOrRaw) => {
|
|
2545
|
+
if (typeof envelopeOrRaw === "object" && envelopeOrRaw !== null && "eventName" in envelopeOrRaw && "sentAt" in envelopeOrRaw && "sentTo" in envelopeOrRaw && "data" in envelopeOrRaw) {
|
|
2546
|
+
wrappedEnv(envelopeOrRaw);
|
|
2547
|
+
} else {
|
|
2548
|
+
this.dbg({
|
|
2549
|
+
type: "receive",
|
|
2550
|
+
event,
|
|
2551
|
+
envelope: void 0
|
|
2552
|
+
});
|
|
2553
|
+
handler(envelopeOrRaw, void 0);
|
|
2554
|
+
}
|
|
2509
2555
|
};
|
|
2510
2556
|
const errorWrapped = (e) => {
|
|
2511
2557
|
this.dbg({ type: "receive", event, err: String(e) });
|
|
2512
2558
|
};
|
|
2513
|
-
socket.on(String(event),
|
|
2559
|
+
socket.on(String(event), wrappedDispatcher);
|
|
2514
2560
|
socket.on(`${String(event)}:error`, errorWrapped);
|
|
2515
2561
|
let set = this.handlerMap.get(String(event));
|
|
2516
2562
|
if (!set) {
|
|
2517
2563
|
set = /* @__PURE__ */ new Set();
|
|
2518
2564
|
this.handlerMap.set(String(event), set);
|
|
2519
2565
|
}
|
|
2520
|
-
const entry = {
|
|
2566
|
+
const entry = {
|
|
2567
|
+
orig: handler,
|
|
2568
|
+
wrapped: wrappedDispatcher,
|
|
2569
|
+
errorWrapped
|
|
2570
|
+
};
|
|
2521
2571
|
set.add(entry);
|
|
2522
2572
|
return () => {
|
|
2523
|
-
socket.off(String(event),
|
|
2573
|
+
socket.off(String(event), wrappedDispatcher);
|
|
2524
2574
|
socket.off(`${String(event)}:error`, errorWrapped);
|
|
2525
2575
|
const s = this.handlerMap.get(String(event));
|
|
2526
2576
|
if (s) {
|