@emeryld/rrroutes-client 2.6.7 → 2.6.9

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
@@ -1383,11 +1383,12 @@ function createRouteClient(opts) {
1383
1383
  const encodedLeaf = encodeLeafKey(leaf);
1384
1384
  keyByAlias.set(alias, encodedLeaf);
1385
1385
  const branchInput = input[alias];
1386
- const args = branchInput?.args;
1386
+ const query = branchInput?.query;
1387
+ const params = branchInput?.params;
1387
1388
  const body = branchInput?.body;
1388
1389
  payload[encodedLeaf] = {
1389
- ...args?.params !== void 0 ? { params: args.params } : {},
1390
- ...args?.query !== void 0 ? { query: args.query } : {},
1390
+ ...params !== void 0 ? { params } : {},
1391
+ ...query !== void 0 ? { query } : {},
1391
1392
  ...body !== void 0 ? { body } : {}
1392
1393
  };
1393
1394
  }
@@ -1496,6 +1497,168 @@ function buildRouter(routeClient, routes) {
1496
1497
  ));
1497
1498
  }
1498
1499
 
1500
+ // src/sockets/socket.client.context.provider.tsx
1501
+ var React3 = __toESM(require("react"), 1);
1502
+
1503
+ // src/sockets/socket.client.context.client.ts
1504
+ var React = __toESM(require("react"), 1);
1505
+ var SocketCtx = React.createContext(null);
1506
+ function useSocketClient() {
1507
+ const ctx = React.useContext(SocketCtx);
1508
+ if (!ctx)
1509
+ throw new Error("SocketClient not found. Wrap with <SocketProvider>.");
1510
+ return ctx;
1511
+ }
1512
+
1513
+ // src/sockets/socket.client.context.connection.ts
1514
+ var React2 = __toESM(require("react"), 1);
1515
+ function useSocketConnection(args) {
1516
+ const {
1517
+ event,
1518
+ rooms,
1519
+ onMessage,
1520
+ onCleanup,
1521
+ autoJoin = true,
1522
+ autoLeave = true
1523
+ } = args;
1524
+ const client = useSocketClient();
1525
+ const normalizedRooms = React2.useMemo(
1526
+ () => rooms == null ? [] : Array.isArray(rooms) ? rooms : [rooms],
1527
+ [rooms]
1528
+ );
1529
+ const reportAsyncError = React2.useCallback(
1530
+ (phase, error) => {
1531
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
1532
+ console.warn(`[socket] useSocketConnection ${phase} failed`, error);
1533
+ }
1534
+ },
1535
+ []
1536
+ );
1537
+ React2.useEffect(() => {
1538
+ if (autoJoin && normalizedRooms.length > 0)
1539
+ void client.joinRooms(normalizedRooms, args.joinMeta).catch((error) => reportAsyncError("joinRooms", error));
1540
+ const unsubscribe = client.on(event, (payload, meta) => {
1541
+ onMessage(payload, meta);
1542
+ });
1543
+ return () => {
1544
+ unsubscribe();
1545
+ if (autoLeave && normalizedRooms.length > 0)
1546
+ void client.leaveRooms(normalizedRooms, args.leaveMeta).catch((error) => reportAsyncError("leaveRooms", error));
1547
+ if (onCleanup) onCleanup();
1548
+ };
1549
+ }, [
1550
+ client,
1551
+ event,
1552
+ onMessage,
1553
+ autoJoin,
1554
+ autoLeave,
1555
+ reportAsyncError,
1556
+ ...normalizedRooms
1557
+ ]);
1558
+ }
1559
+
1560
+ // src/sockets/socket.client.context.debug.ts
1561
+ function dbg(dbgOpts, e) {
1562
+ if (!dbgOpts?.logger) return;
1563
+ if (!dbgOpts[e.type]) return;
1564
+ try {
1565
+ dbgOpts.logger(e);
1566
+ } catch (error) {
1567
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
1568
+ console.warn("[socket] provider debug logger threw", error);
1569
+ }
1570
+ }
1571
+ }
1572
+ function isProbablySocket(value) {
1573
+ if (!value || typeof value !== "object") return false;
1574
+ const anyVal = value;
1575
+ const ctorName = anyVal.constructor?.name;
1576
+ if (ctorName === "Socket") return true;
1577
+ return ("connected" in anyVal || "recovered" in anyVal) && typeof anyVal.on === "function" && typeof anyVal.emit === "function";
1578
+ }
1579
+ function describeSocketLike(value) {
1580
+ if (!value) return null;
1581
+ const id = value.id ?? "unknown";
1582
+ const connected = value.connected ?? false;
1583
+ const recovered = typeof value.recovered === "boolean" ? ` recovered=${value.recovered}` : "";
1584
+ return `[Socket id=${id} connected=${connected}${recovered}]`;
1585
+ }
1586
+ function safeDescribeHookValue(value) {
1587
+ if (value == null) return value;
1588
+ const valueType = typeof value;
1589
+ if (valueType === "string" || valueType === "number" || valueType === "boolean") {
1590
+ return value;
1591
+ }
1592
+ if (valueType === "bigint" || valueType === "symbol") return String(value);
1593
+ if (valueType === "function")
1594
+ return `[function ${value.name || "anonymous"}]`;
1595
+ if (Array.isArray(value)) return `[array length=${value.length}]`;
1596
+ if (isProbablySocket(value)) return describeSocketLike(value);
1597
+ const ctorName = value.constructor?.name ?? "object";
1598
+ const keys = Object.keys(value);
1599
+ const keyPreview = keys.slice(0, 4).join(",");
1600
+ const suffix = keys.length > 4 ? ",\u2026" : "";
1601
+ return `[${ctorName} keys=${keyPreview}${suffix}]`;
1602
+ }
1603
+ function summarizeEvents(events) {
1604
+ if (!events || typeof events !== "object")
1605
+ return safeDescribeHookValue(events);
1606
+ const keys = Object.keys(events);
1607
+ if (!keys.length) return "[events empty]";
1608
+ const preview = keys.slice(0, 4).join(",");
1609
+ const suffix = keys.length > 4 ? ",\u2026" : "";
1610
+ return `[events count=${keys.length} keys=${preview}${suffix}]`;
1611
+ }
1612
+ function summarizeBaseOptions(options) {
1613
+ if (!options || typeof options !== "object")
1614
+ return safeDescribeHookValue(options);
1615
+ const obj = options;
1616
+ const keys = Object.keys(obj);
1617
+ if (!keys.length) return "[baseOptions empty]";
1618
+ const preview = keys.slice(0, 4).join(",");
1619
+ const suffix = keys.length > 4 ? ",\u2026" : "";
1620
+ const hasDebug = "debug" in obj;
1621
+ return `[baseOptions keys=${preview}${suffix} debug=${hasDebug}]`;
1622
+ }
1623
+ function summarizeMeta(meta, label) {
1624
+ if (meta == null) return null;
1625
+ if (typeof meta !== "object") return safeDescribeHookValue(meta);
1626
+ const keys = Object.keys(meta);
1627
+ if (!keys.length) return `[${label} empty]`;
1628
+ const preview = keys.slice(0, 4).join(",");
1629
+ const suffix = keys.length > 4 ? ",\u2026" : "";
1630
+ return `[${label} keys=${preview}${suffix}]`;
1631
+ }
1632
+ function createHookDebugEvent(prev, next, hook) {
1633
+ const reason = prev ? "change" : "init";
1634
+ const changed = Object.keys(next).reduce((acc, dependency) => {
1635
+ const prevValue = prev ? prev[dependency] : void 0;
1636
+ const nextValue = next[dependency];
1637
+ if (!prev || !Object.is(prevValue, nextValue)) {
1638
+ acc.push({
1639
+ dependency,
1640
+ previous: safeDescribeHookValue(prevValue),
1641
+ next: safeDescribeHookValue(nextValue)
1642
+ });
1643
+ }
1644
+ return acc;
1645
+ }, []);
1646
+ if (!changed.length) return null;
1647
+ return { type: "hook", phase: hook, reason, changes: changed };
1648
+ }
1649
+ function trackHookTrigger({
1650
+ ref,
1651
+ hook,
1652
+ providerDebug,
1653
+ snapshot
1654
+ }) {
1655
+ const prev = ref.current;
1656
+ ref.current = snapshot;
1657
+ if (!providerDebug?.logger || !providerDebug?.hook) return;
1658
+ const event = createHookDebugEvent(prev, snapshot, hook);
1659
+ if (event) dbg(providerDebug, event);
1660
+ }
1661
+
1499
1662
  // src/sockets/socket.client.sys.ts
1500
1663
  var import_zod = require("zod");
1501
1664
  var roomValueSchema = import_zod.z.union([import_zod.z.array(import_zod.z.string()), import_zod.z.string()]);
@@ -2299,168 +2462,6 @@ var SocketClient = class {
2299
2462
  }
2300
2463
  };
2301
2464
 
2302
- // src/sockets/socket.client.context.provider.tsx
2303
- var React3 = __toESM(require("react"), 1);
2304
-
2305
- // src/sockets/socket.client.context.client.ts
2306
- var React = __toESM(require("react"), 1);
2307
- var SocketCtx = React.createContext(null);
2308
- function useSocketClient() {
2309
- const ctx = React.useContext(SocketCtx);
2310
- if (!ctx)
2311
- throw new Error("SocketClient not found. Wrap with <SocketProvider>.");
2312
- return ctx;
2313
- }
2314
-
2315
- // src/sockets/socket.client.context.connection.ts
2316
- var React2 = __toESM(require("react"), 1);
2317
- function useSocketConnection(args) {
2318
- const {
2319
- event,
2320
- rooms,
2321
- onMessage,
2322
- onCleanup,
2323
- autoJoin = true,
2324
- autoLeave = true
2325
- } = args;
2326
- const client = useSocketClient();
2327
- const normalizedRooms = React2.useMemo(
2328
- () => rooms == null ? [] : Array.isArray(rooms) ? rooms : [rooms],
2329
- [rooms]
2330
- );
2331
- const reportAsyncError = React2.useCallback(
2332
- (phase, error) => {
2333
- if (typeof console !== "undefined" && typeof console.warn === "function") {
2334
- console.warn(`[socket] useSocketConnection ${phase} failed`, error);
2335
- }
2336
- },
2337
- []
2338
- );
2339
- React2.useEffect(() => {
2340
- if (autoJoin && normalizedRooms.length > 0)
2341
- void client.joinRooms(normalizedRooms, args.joinMeta).catch((error) => reportAsyncError("joinRooms", error));
2342
- const unsubscribe = client.on(event, (payload, meta) => {
2343
- onMessage(payload, meta);
2344
- });
2345
- return () => {
2346
- unsubscribe();
2347
- if (autoLeave && normalizedRooms.length > 0)
2348
- void client.leaveRooms(normalizedRooms, args.leaveMeta).catch((error) => reportAsyncError("leaveRooms", error));
2349
- if (onCleanup) onCleanup();
2350
- };
2351
- }, [
2352
- client,
2353
- event,
2354
- onMessage,
2355
- autoJoin,
2356
- autoLeave,
2357
- reportAsyncError,
2358
- ...normalizedRooms
2359
- ]);
2360
- }
2361
-
2362
- // src/sockets/socket.client.context.debug.ts
2363
- function dbg(dbgOpts, e) {
2364
- if (!dbgOpts?.logger) return;
2365
- if (!dbgOpts[e.type]) return;
2366
- try {
2367
- dbgOpts.logger(e);
2368
- } catch (error) {
2369
- if (typeof console !== "undefined" && typeof console.warn === "function") {
2370
- console.warn("[socket] provider debug logger threw", error);
2371
- }
2372
- }
2373
- }
2374
- function isProbablySocket(value) {
2375
- if (!value || typeof value !== "object") return false;
2376
- const anyVal = value;
2377
- const ctorName = anyVal.constructor?.name;
2378
- if (ctorName === "Socket") return true;
2379
- return ("connected" in anyVal || "recovered" in anyVal) && typeof anyVal.on === "function" && typeof anyVal.emit === "function";
2380
- }
2381
- function describeSocketLike(value) {
2382
- if (!value) return null;
2383
- const id = value.id ?? "unknown";
2384
- const connected = value.connected ?? false;
2385
- const recovered = typeof value.recovered === "boolean" ? ` recovered=${value.recovered}` : "";
2386
- return `[Socket id=${id} connected=${connected}${recovered}]`;
2387
- }
2388
- function safeDescribeHookValue(value) {
2389
- if (value == null) return value;
2390
- const valueType = typeof value;
2391
- if (valueType === "string" || valueType === "number" || valueType === "boolean") {
2392
- return value;
2393
- }
2394
- if (valueType === "bigint" || valueType === "symbol") return String(value);
2395
- if (valueType === "function")
2396
- return `[function ${value.name || "anonymous"}]`;
2397
- if (Array.isArray(value)) return `[array length=${value.length}]`;
2398
- if (isProbablySocket(value)) return describeSocketLike(value);
2399
- const ctorName = value.constructor?.name ?? "object";
2400
- const keys = Object.keys(value);
2401
- const keyPreview = keys.slice(0, 4).join(",");
2402
- const suffix = keys.length > 4 ? ",\u2026" : "";
2403
- return `[${ctorName} keys=${keyPreview}${suffix}]`;
2404
- }
2405
- function summarizeEvents(events) {
2406
- if (!events || typeof events !== "object")
2407
- return safeDescribeHookValue(events);
2408
- const keys = Object.keys(events);
2409
- if (!keys.length) return "[events empty]";
2410
- const preview = keys.slice(0, 4).join(",");
2411
- const suffix = keys.length > 4 ? ",\u2026" : "";
2412
- return `[events count=${keys.length} keys=${preview}${suffix}]`;
2413
- }
2414
- function summarizeBaseOptions(options) {
2415
- if (!options || typeof options !== "object")
2416
- return safeDescribeHookValue(options);
2417
- const obj = options;
2418
- const keys = Object.keys(obj);
2419
- if (!keys.length) return "[baseOptions empty]";
2420
- const preview = keys.slice(0, 4).join(",");
2421
- const suffix = keys.length > 4 ? ",\u2026" : "";
2422
- const hasDebug = "debug" in obj;
2423
- return `[baseOptions keys=${preview}${suffix} debug=${hasDebug}]`;
2424
- }
2425
- function summarizeMeta(meta, label) {
2426
- if (meta == null) return null;
2427
- if (typeof meta !== "object") return safeDescribeHookValue(meta);
2428
- const keys = Object.keys(meta);
2429
- if (!keys.length) return `[${label} empty]`;
2430
- const preview = keys.slice(0, 4).join(",");
2431
- const suffix = keys.length > 4 ? ",\u2026" : "";
2432
- return `[${label} keys=${preview}${suffix}]`;
2433
- }
2434
- function createHookDebugEvent(prev, next, hook) {
2435
- const reason = prev ? "change" : "init";
2436
- const changed = Object.keys(next).reduce((acc, dependency) => {
2437
- const prevValue = prev ? prev[dependency] : void 0;
2438
- const nextValue = next[dependency];
2439
- if (!prev || !Object.is(prevValue, nextValue)) {
2440
- acc.push({
2441
- dependency,
2442
- previous: safeDescribeHookValue(prevValue),
2443
- next: safeDescribeHookValue(nextValue)
2444
- });
2445
- }
2446
- return acc;
2447
- }, []);
2448
- if (!changed.length) return null;
2449
- return { type: "hook", phase: hook, reason, changes: changed };
2450
- }
2451
- function trackHookTrigger({
2452
- ref,
2453
- hook,
2454
- providerDebug,
2455
- snapshot
2456
- }) {
2457
- const prev = ref.current;
2458
- ref.current = snapshot;
2459
- if (!providerDebug?.logger || !providerDebug?.hook) return;
2460
- const event = createHookDebugEvent(prev, snapshot, hook);
2461
- if (event) dbg(providerDebug, event);
2462
- }
2463
-
2464
2465
  // src/sockets/socket.client.context.provider.tsx
2465
2466
  var import_jsx_runtime = require("react/jsx-runtime");
2466
2467
  function buildSocketProvider(args) {
@@ -2777,11 +2778,17 @@ function buildSocketedRoute(options) {
2777
2778
  );
2778
2779
  const renderCountRef = (0, import_react5.useRef)(0);
2779
2780
  const clientReadyRef = (0, import_react5.useRef)(null);
2781
+ const endpointDataRef = (0, import_react5.useRef)(
2782
+ endpointResult.data
2783
+ );
2784
+ const toRoomsRef = (0, import_react5.useRef)(toRooms);
2780
2785
  const onReceiveEffectDebugRef = (0, import_react5.useRef)(null);
2781
2786
  const deriveRoomsEffectDebugRef = (0, import_react5.useRef)(null);
2782
2787
  const joinRoomsEffectDebugRef = (0, import_react5.useRef)(null);
2783
2788
  const applySocketEffectDebugRef = (0, import_react5.useRef)(null);
2784
2789
  renderCountRef.current += 1;
2790
+ endpointDataRef.current = endpointResult.data;
2791
+ toRoomsRef.current = toRooms;
2785
2792
  const roomsKey = (0, import_react5.useMemo)(() => roomState.rooms.join("|"), [roomState.rooms]);
2786
2793
  const joinMetaKey = (0, import_react5.useMemo)(
2787
2794
  () => safeJsonKey(roomState.joinMeta ?? null),
@@ -2816,35 +2823,31 @@ function buildSocketedRoute(options) {
2816
2823
  phase: "endpoint_on_receive_effect",
2817
2824
  debug,
2818
2825
  snapshot: {
2819
- endpointResultRef: describeObjectReference(endpointResult),
2820
- endpointDataRef: describeObjectReference(endpointResult.data),
2821
- toRoomsRef: describeObjectReference(toRooms)
2826
+ onReceiveRef: describeObjectReference(endpointResult.onReceive)
2822
2827
  }
2823
2828
  });
2824
2829
  const unsubscribe = endpointResult.onReceive((data) => {
2825
2830
  setRoomState((prev) => {
2826
- const next = mergeRoomState(prev, toRooms(data));
2831
+ const next = mergeRoomState(prev, toRoomsRef.current(data));
2827
2832
  return roomStateEqual(prev, next) ? prev : next;
2828
2833
  });
2829
2834
  });
2830
2835
  return unsubscribe;
2831
- }, [endpointResult, toRooms, debug]);
2836
+ }, [endpointResult.onReceive]);
2832
2837
  (0, import_react5.useEffect)(() => {
2833
2838
  trackHookTrigger2({
2834
2839
  ref: deriveRoomsEffectDebugRef,
2835
2840
  phase: "derive_rooms_effect",
2836
2841
  debug,
2837
2842
  snapshot: {
2838
- endpointDataRef: describeObjectReference(endpointResult.data),
2843
+ argsKey,
2844
+ endpointDataRef: describeObjectReference(endpointDataRef.current),
2839
2845
  toRoomsRef: describeObjectReference(toRooms)
2840
2846
  }
2841
2847
  });
2842
- const next = roomsFromData(
2843
- endpointResult.data,
2844
- toRooms
2845
- );
2848
+ const next = roomsFromData(endpointDataRef.current, toRooms);
2846
2849
  setRoomState((prev) => roomStateEqual(prev, next) ? prev : next);
2847
- }, [endpointResult.data, toRooms, debug]);
2850
+ }, [argsKey, toRooms, debug]);
2848
2851
  (0, import_react5.useEffect)(() => {
2849
2852
  trackHookTrigger2({
2850
2853
  ref: joinRoomsEffectDebugRef,