@emeryld/rrroutes-client 2.7.9 → 2.7.11

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
@@ -1578,9 +1578,12 @@ var React3 = __toESM(require("react"), 1);
1578
1578
 
1579
1579
  // src/sockets/socket.client.context.client.ts
1580
1580
  var React = __toESM(require("react"), 1);
1581
- var SocketCtx = React.createContext(null);
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
+ }
1584
1587
  if (!ctx) {
1585
1588
  return null;
1586
1589
  }
@@ -1596,13 +1599,48 @@ function useSocketConnection(args) {
1596
1599
  onMessage,
1597
1600
  onCleanup,
1598
1601
  autoJoin = true,
1599
- autoLeave = true
1602
+ autoLeave = true,
1603
+ debug
1600
1604
  } = args;
1601
1605
  const client = useSocketClient();
1602
1606
  const normalizedRooms = React2.useMemo(
1603
1607
  () => rooms == null ? [] : Array.isArray(rooms) ? rooms : [rooms],
1604
1608
  [rooms]
1605
1609
  );
1610
+ const normalizedRoomsKey = React2.useMemo(
1611
+ () => normalizedRooms.join(""),
1612
+ [normalizedRooms]
1613
+ );
1614
+ const joinMetaKey = React2.useMemo(
1615
+ () => JSON.stringify(args.joinMeta ?? null),
1616
+ [args.joinMeta]
1617
+ );
1618
+ const leaveMetaKey = React2.useMemo(
1619
+ () => JSON.stringify(args.leaveMeta ?? null),
1620
+ [args.leaveMeta]
1621
+ );
1622
+ const missingClientWarnedRef = React2.useRef(false);
1623
+ const emitDebug = React2.useCallback(
1624
+ (event2) => {
1625
+ if (!debug?.enabled) return;
1626
+ if (debug[event2.type] === false) return;
1627
+ const logger = debug.logger;
1628
+ if (logger) {
1629
+ try {
1630
+ logger(event2);
1631
+ } catch (error) {
1632
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
1633
+ console.warn("[socket] useSocketConnection debug logger threw", error);
1634
+ }
1635
+ }
1636
+ return;
1637
+ }
1638
+ if (typeof console !== "undefined" && typeof console.log === "function") {
1639
+ console.log("[socket] useSocketConnection", event2);
1640
+ }
1641
+ },
1642
+ [debug]
1643
+ );
1606
1644
  const reportAsyncError = React2.useCallback(
1607
1645
  (phase, error) => {
1608
1646
  if (typeof console !== "undefined" && typeof console.warn === "function") {
@@ -1612,26 +1650,118 @@ function useSocketConnection(args) {
1612
1650
  []
1613
1651
  );
1614
1652
  React2.useEffect(() => {
1615
- if (!client) return;
1616
- if (autoJoin && normalizedRooms.length > 0)
1617
- void client.joinRooms(normalizedRooms, args.joinMeta).catch((error) => reportAsyncError("joinRooms", error));
1653
+ emitDebug({
1654
+ type: "lifecycle",
1655
+ phase: "effect_start",
1656
+ event,
1657
+ rooms: normalizedRooms,
1658
+ autoJoin,
1659
+ autoLeave
1660
+ });
1661
+ if (!client) {
1662
+ emitDebug({
1663
+ type: "lifecycle",
1664
+ phase: "client_missing",
1665
+ event,
1666
+ rooms: normalizedRooms,
1667
+ autoJoin,
1668
+ autoLeave
1669
+ });
1670
+ if (debug?.throwIfClientMissing) {
1671
+ throw new Error(
1672
+ `useSocketConnection("${event}") missing SocketClient. Wrap with <SocketProvider>.`
1673
+ );
1674
+ }
1675
+ if (debug?.warnIfClientMissing && !missingClientWarnedRef.current && typeof console !== "undefined" && typeof console.warn === "function") {
1676
+ missingClientWarnedRef.current = true;
1677
+ console.warn(
1678
+ `[socket] useSocketConnection("${event}") skipped because SocketClient is null.`
1679
+ );
1680
+ }
1681
+ return;
1682
+ }
1683
+ missingClientWarnedRef.current = false;
1684
+ emitDebug({ type: "subscription", phase: "register", event });
1685
+ if (autoJoin && normalizedRooms.length > 0) {
1686
+ emitDebug({
1687
+ type: "room",
1688
+ phase: "join_attempt",
1689
+ rooms: normalizedRooms
1690
+ });
1691
+ void client.joinRooms(normalizedRooms, args.joinMeta).then(() => {
1692
+ emitDebug({ type: "room", phase: "join_ok", rooms: normalizedRooms });
1693
+ }).catch((error) => {
1694
+ emitDebug({
1695
+ type: "room",
1696
+ phase: "join_error",
1697
+ rooms: normalizedRooms,
1698
+ err: String(error)
1699
+ });
1700
+ reportAsyncError("joinRooms", error);
1701
+ });
1702
+ } else {
1703
+ emitDebug({
1704
+ type: "room",
1705
+ phase: "join_skip",
1706
+ rooms: normalizedRooms,
1707
+ reason: autoJoin ? "no_rooms" : "auto_disabled"
1708
+ });
1709
+ }
1618
1710
  const unsubscribe = client.on(event, (payload, meta) => {
1711
+ emitDebug({ type: "subscription", phase: "message", event });
1619
1712
  onMessage(payload, meta);
1620
1713
  });
1621
1714
  return () => {
1715
+ emitDebug({ type: "subscription", phase: "unregister", event });
1622
1716
  unsubscribe();
1623
- if (autoLeave && normalizedRooms.length > 0)
1624
- void client.leaveRooms(normalizedRooms, args.leaveMeta).catch((error) => reportAsyncError("leaveRooms", error));
1717
+ if (autoLeave && normalizedRooms.length > 0) {
1718
+ emitDebug({
1719
+ type: "room",
1720
+ phase: "leave_attempt",
1721
+ rooms: normalizedRooms
1722
+ });
1723
+ void client.leaveRooms(normalizedRooms, args.leaveMeta).then(() => {
1724
+ emitDebug({ type: "room", phase: "leave_ok", rooms: normalizedRooms });
1725
+ }).catch((error) => {
1726
+ emitDebug({
1727
+ type: "room",
1728
+ phase: "leave_error",
1729
+ rooms: normalizedRooms,
1730
+ err: String(error)
1731
+ });
1732
+ reportAsyncError("leaveRooms", error);
1733
+ });
1734
+ } else {
1735
+ emitDebug({
1736
+ type: "room",
1737
+ phase: "leave_skip",
1738
+ rooms: normalizedRooms,
1739
+ reason: autoLeave ? "no_rooms" : "auto_disabled"
1740
+ });
1741
+ }
1625
1742
  if (onCleanup) onCleanup();
1743
+ emitDebug({
1744
+ type: "lifecycle",
1745
+ phase: "effect_cleanup",
1746
+ event,
1747
+ rooms: normalizedRooms,
1748
+ autoJoin,
1749
+ autoLeave
1750
+ });
1626
1751
  };
1627
1752
  }, [
1628
1753
  client,
1629
1754
  event,
1630
1755
  onMessage,
1756
+ onCleanup,
1631
1757
  autoJoin,
1632
1758
  autoLeave,
1759
+ debug,
1760
+ emitDebug,
1633
1761
  reportAsyncError,
1634
- ...normalizedRooms
1762
+ normalizedRoomsKey,
1763
+ joinMetaKey,
1764
+ leaveMetaKey
1635
1765
  ]);
1636
1766
  }
1637
1767