@emeryld/rrroutes-client 2.7.11 → 2.7.12

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 CHANGED
@@ -1581,10 +1581,7 @@ var React = __toESM(require("react"), 1);
1581
1581
  var SocketCtx = React.createContext(void 0);
1582
1582
  function useSocketClient() {
1583
1583
  const ctx = React.useContext(SocketCtx);
1584
- if (typeof ctx === "undefined") {
1585
- throw new Error("SocketClient not found. Wrap with <SocketProvider>.");
1586
- }
1587
- if (!ctx) {
1584
+ if (typeof ctx === "undefined" || !ctx) {
1588
1585
  return null;
1589
1586
  }
1590
1587
  return ctx;
@@ -1620,11 +1617,32 @@ function useSocketConnection(args) {
1620
1617
  [args.leaveMeta]
1621
1618
  );
1622
1619
  const missingClientWarnedRef = React2.useRef(false);
1620
+ const onMessageRef = React2.useRef(onMessage);
1621
+ const onCleanupRef = React2.useRef(onCleanup);
1622
+ const joinMetaRef = React2.useRef(args.joinMeta);
1623
+ const leaveMetaRef = React2.useRef(args.leaveMeta);
1624
+ const debugRef = React2.useRef(debug);
1625
+ React2.useEffect(() => {
1626
+ onMessageRef.current = onMessage;
1627
+ }, [onMessage]);
1628
+ React2.useEffect(() => {
1629
+ onCleanupRef.current = onCleanup;
1630
+ }, [onCleanup]);
1631
+ React2.useEffect(() => {
1632
+ joinMetaRef.current = args.joinMeta;
1633
+ }, [args.joinMeta]);
1634
+ React2.useEffect(() => {
1635
+ leaveMetaRef.current = args.leaveMeta;
1636
+ }, [args.leaveMeta]);
1637
+ React2.useEffect(() => {
1638
+ debugRef.current = debug;
1639
+ }, [debug]);
1623
1640
  const emitDebug = React2.useCallback(
1624
1641
  (event2) => {
1625
- if (!debug?.enabled) return;
1626
- if (debug[event2.type] === false) return;
1627
- const logger = debug.logger;
1642
+ const dbg3 = debugRef.current;
1643
+ if (!dbg3?.enabled) return;
1644
+ if (dbg3[event2.type] === false) return;
1645
+ const logger = dbg3.logger;
1628
1646
  if (logger) {
1629
1647
  try {
1630
1648
  logger(event2);
@@ -1639,7 +1657,7 @@ function useSocketConnection(args) {
1639
1657
  console.log("[socket] useSocketConnection", event2);
1640
1658
  }
1641
1659
  },
1642
- [debug]
1660
+ []
1643
1661
  );
1644
1662
  const reportAsyncError = React2.useCallback(
1645
1663
  (phase, error) => {
@@ -1667,12 +1685,12 @@ function useSocketConnection(args) {
1667
1685
  autoJoin,
1668
1686
  autoLeave
1669
1687
  });
1670
- if (debug?.throwIfClientMissing) {
1688
+ if (debugRef.current?.throwIfClientMissing) {
1671
1689
  throw new Error(
1672
1690
  `useSocketConnection("${event}") missing SocketClient. Wrap with <SocketProvider>.`
1673
1691
  );
1674
1692
  }
1675
- if (debug?.warnIfClientMissing && !missingClientWarnedRef.current && typeof console !== "undefined" && typeof console.warn === "function") {
1693
+ if (debugRef.current?.warnIfClientMissing && !missingClientWarnedRef.current && typeof console !== "undefined" && typeof console.warn === "function") {
1676
1694
  missingClientWarnedRef.current = true;
1677
1695
  console.warn(
1678
1696
  `[socket] useSocketConnection("${event}") skipped because SocketClient is null.`
@@ -1688,7 +1706,7 @@ function useSocketConnection(args) {
1688
1706
  phase: "join_attempt",
1689
1707
  rooms: normalizedRooms
1690
1708
  });
1691
- void client.joinRooms(normalizedRooms, args.joinMeta).then(() => {
1709
+ void client.joinRooms(normalizedRooms, joinMetaRef.current).then(() => {
1692
1710
  emitDebug({ type: "room", phase: "join_ok", rooms: normalizedRooms });
1693
1711
  }).catch((error) => {
1694
1712
  emitDebug({
@@ -1709,7 +1727,7 @@ function useSocketConnection(args) {
1709
1727
  }
1710
1728
  const unsubscribe = client.on(event, (payload, meta) => {
1711
1729
  emitDebug({ type: "subscription", phase: "message", event });
1712
- onMessage(payload, meta);
1730
+ onMessageRef.current(payload, meta);
1713
1731
  });
1714
1732
  return () => {
1715
1733
  emitDebug({ type: "subscription", phase: "unregister", event });
@@ -1720,7 +1738,7 @@ function useSocketConnection(args) {
1720
1738
  phase: "leave_attempt",
1721
1739
  rooms: normalizedRooms
1722
1740
  });
1723
- void client.leaveRooms(normalizedRooms, args.leaveMeta).then(() => {
1741
+ void client.leaveRooms(normalizedRooms, leaveMetaRef.current).then(() => {
1724
1742
  emitDebug({ type: "room", phase: "leave_ok", rooms: normalizedRooms });
1725
1743
  }).catch((error) => {
1726
1744
  emitDebug({
@@ -1739,7 +1757,7 @@ function useSocketConnection(args) {
1739
1757
  reason: autoLeave ? "no_rooms" : "auto_disabled"
1740
1758
  });
1741
1759
  }
1742
- if (onCleanup) onCleanup();
1760
+ if (onCleanupRef.current) onCleanupRef.current();
1743
1761
  emitDebug({
1744
1762
  type: "lifecycle",
1745
1763
  phase: "effect_cleanup",
@@ -1752,11 +1770,8 @@ function useSocketConnection(args) {
1752
1770
  }, [
1753
1771
  client,
1754
1772
  event,
1755
- onMessage,
1756
- onCleanup,
1757
1773
  autoJoin,
1758
1774
  autoLeave,
1759
- debug,
1760
1775
  emitDebug,
1761
1776
  reportAsyncError,
1762
1777
  normalizedRoomsKey,