@emeryld/rrroutes-client 2.2.4 → 2.2.5
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 +30 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +31 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -691,6 +691,10 @@ function createSocketedRouteHook(args) {
|
|
|
691
691
|
const { buildRoute, useClient = () => useSocketClient() } = args;
|
|
692
692
|
return function useSocketedRoute(params) {
|
|
693
693
|
const { routeKey, socketEvent, handlers, buildOptions, buildMeta, useOptions } = params;
|
|
694
|
+
const handlersRef = (0, import_react.useRef)(handlers);
|
|
695
|
+
(0, import_react.useEffect)(() => {
|
|
696
|
+
handlersRef.current = handlers;
|
|
697
|
+
}, [handlers]);
|
|
694
698
|
const endpoint = (0, import_react.useMemo)(
|
|
695
699
|
() => buildRoute(routeKey, buildOptions, buildMeta),
|
|
696
700
|
[buildRoute, routeKey, buildOptions, buildMeta]
|
|
@@ -700,23 +704,31 @@ function createSocketedRouteHook(args) {
|
|
|
700
704
|
const roomsKey = rooms.slice().sort().join("|");
|
|
701
705
|
const applyRooms = (0, import_react.useCallback)(
|
|
702
706
|
(page) => {
|
|
703
|
-
|
|
707
|
+
const onReceiveRooms = handlersRef.current.onReceiveRooms;
|
|
708
|
+
if (!onReceiveRooms) return;
|
|
704
709
|
setRooms((prev) => {
|
|
705
|
-
const next =
|
|
706
|
-
|
|
710
|
+
const next = onReceiveRooms(page, prev);
|
|
711
|
+
const unique = Array.from(new Set(next.filter(Boolean)));
|
|
712
|
+
if (prev.length === unique.length && prev.every((room, idx) => room === unique[idx])) {
|
|
713
|
+
return prev;
|
|
714
|
+
}
|
|
715
|
+
return unique;
|
|
707
716
|
});
|
|
708
717
|
},
|
|
709
|
-
[
|
|
718
|
+
[]
|
|
710
719
|
);
|
|
711
720
|
const routeArgs = useOptions?.args;
|
|
712
721
|
const callerOptions = useOptions?.options;
|
|
713
|
-
const mergedOptions =
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
722
|
+
const mergedOptions = (0, import_react.useMemo)(
|
|
723
|
+
() => ({
|
|
724
|
+
...callerOptions ?? {},
|
|
725
|
+
onReceive(page) {
|
|
726
|
+
callerOptions?.onReceive?.(page);
|
|
727
|
+
applyRooms(page);
|
|
728
|
+
}
|
|
729
|
+
}),
|
|
730
|
+
[callerOptions, applyRooms]
|
|
731
|
+
);
|
|
720
732
|
const endpointArgs = routeArgs ? [routeArgs, mergedOptions] : [mergedOptions];
|
|
721
733
|
const { data } = endpoint.useEndpoint(...endpointArgs);
|
|
722
734
|
(0, import_react.useEffect)(() => {
|
|
@@ -730,23 +742,24 @@ function createSocketedRouteHook(args) {
|
|
|
730
742
|
}, [data, applyRooms]);
|
|
731
743
|
(0, import_react.useEffect)(() => {
|
|
732
744
|
const stop = client.on(socketEvent, (payload, meta) => {
|
|
733
|
-
const updater = (prev) =>
|
|
745
|
+
const updater = (prev) => handlersRef.current.handleMessage(prev, payload, meta);
|
|
734
746
|
const setDataArgs = routeArgs ? [routeArgs] : [];
|
|
735
747
|
endpoint.setData(updater, ...setDataArgs);
|
|
736
748
|
});
|
|
737
749
|
return stop;
|
|
738
|
-
}, [client, socketEvent, endpoint,
|
|
750
|
+
}, [client, socketEvent, endpoint, routeArgs]);
|
|
739
751
|
(0, import_react.useEffect)(() => {
|
|
740
752
|
let cancelled = false;
|
|
753
|
+
const { joinMeta, leaveMeta } = handlersRef.current;
|
|
741
754
|
(async () => {
|
|
742
755
|
if (!rooms.length) return;
|
|
743
756
|
try {
|
|
744
|
-
await client.joinRooms(rooms,
|
|
757
|
+
await client.joinRooms(rooms, joinMeta);
|
|
745
758
|
} catch {
|
|
746
759
|
}
|
|
747
760
|
if (cancelled) {
|
|
748
761
|
try {
|
|
749
|
-
await client.leaveRooms(rooms,
|
|
762
|
+
await client.leaveRooms(rooms, leaveMeta);
|
|
750
763
|
} catch {
|
|
751
764
|
}
|
|
752
765
|
}
|
|
@@ -754,10 +767,10 @@ function createSocketedRouteHook(args) {
|
|
|
754
767
|
return () => {
|
|
755
768
|
cancelled = true;
|
|
756
769
|
if (!rooms.length) return;
|
|
757
|
-
client.leaveRooms(rooms,
|
|
770
|
+
client.leaveRooms(rooms, leaveMeta).catch(() => {
|
|
758
771
|
});
|
|
759
772
|
};
|
|
760
|
-
}, [client, roomsKey,
|
|
773
|
+
}, [client, roomsKey, rooms]);
|
|
761
774
|
return { data, rooms };
|
|
762
775
|
};
|
|
763
776
|
}
|