@emeryld/rrroutes-client 2.1.0 → 2.1.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.mjs CHANGED
@@ -442,7 +442,6 @@ var buildRoomPayloadSchema = (metaSchema) => z.object({
442
442
 
443
443
  // src/sockets/socket.client.context.tsx
444
444
  import * as React from "react";
445
- import { Socket } from "socket.io-client";
446
445
  import { jsx } from "react/jsx-runtime";
447
446
  var SocketCtx = React.createContext(null);
448
447
  function dbg(dbgOpts, e) {
@@ -450,16 +449,31 @@ function dbg(dbgOpts, e) {
450
449
  if (!dbgOpts[e.type]) return;
451
450
  dbgOpts.logger(e);
452
451
  }
452
+ function isProbablySocket(value) {
453
+ if (!value || typeof value !== "object") return false;
454
+ const anyVal = value;
455
+ const ctorName = anyVal.constructor?.name;
456
+ if (ctorName === "Socket") return true;
457
+ return ("connected" in anyVal || "recovered" in anyVal) && typeof anyVal.on === "function" && typeof anyVal.emit === "function";
458
+ }
459
+ function describeSocketLike(value) {
460
+ if (!value) return null;
461
+ const id = value.id ?? "unknown";
462
+ const connected = value.connected ?? false;
463
+ const recovered = typeof value.recovered === "boolean" ? ` recovered=${value.recovered}` : "";
464
+ return `[Socket id=${id} connected=${connected}${recovered}]`;
465
+ }
453
466
  function safeDescribeHookValue(value) {
454
467
  if (value == null) return value;
455
468
  const valueType = typeof value;
456
- if (valueType === "string" || valueType === "number" || valueType === "boolean") return value;
469
+ if (valueType === "string" || valueType === "number" || valueType === "boolean") {
470
+ return value;
471
+ }
457
472
  if (valueType === "bigint" || valueType === "symbol") return String(value);
458
473
  if (valueType === "function") return `[function ${value.name || "anonymous"}]`;
459
474
  if (Array.isArray(value)) return `[array length=${value.length}]`;
460
- if (value instanceof Socket) {
461
- const socket = value;
462
- return `[Socket id=${socket.id ?? "unknown"} connected=${socket.connected ?? false}]`;
475
+ if (isProbablySocket(value)) {
476
+ return describeSocketLike(value);
463
477
  }
464
478
  const ctorName = value.constructor?.name ?? "object";
465
479
  const keys = Object.keys(value);
@@ -467,20 +481,56 @@ function safeDescribeHookValue(value) {
467
481
  const suffix = keys.length > 4 ? ",\u2026" : "";
468
482
  return `[${ctorName} keys=${keyPreview}${suffix}]`;
469
483
  }
484
+ function summarizeEvents(events) {
485
+ if (!events || typeof events !== "object") return safeDescribeHookValue(events);
486
+ const keys = Object.keys(events);
487
+ if (!keys.length) return "[events empty]";
488
+ const preview = keys.slice(0, 4).join(",");
489
+ const suffix = keys.length > 4 ? ",\u2026" : "";
490
+ return `[events count=${keys.length} keys=${preview}${suffix}]`;
491
+ }
492
+ function summarizeBaseOptions(options) {
493
+ if (!options || typeof options !== "object") return safeDescribeHookValue(options);
494
+ const obj = options;
495
+ const keys = Object.keys(obj);
496
+ if (!keys.length) return "[baseOptions empty]";
497
+ const preview = keys.slice(0, 4).join(",");
498
+ const suffix = keys.length > 4 ? ",\u2026" : "";
499
+ const hasDebug = "debug" in obj;
500
+ return `[baseOptions keys=${preview}${suffix} debug=${hasDebug}]`;
501
+ }
502
+ function summarizeMeta(meta, label) {
503
+ if (meta == null) return null;
504
+ if (typeof meta !== "object") return safeDescribeHookValue(meta);
505
+ const keys = Object.keys(meta);
506
+ if (!keys.length) return `[${label} empty]`;
507
+ const preview = keys.slice(0, 4).join(",");
508
+ const suffix = keys.length > 4 ? ",\u2026" : "";
509
+ return `[${label} keys=${preview}${suffix}]`;
510
+ }
470
511
  function createHookDebugEvent(prev, next, hook) {
471
512
  const reason = prev ? "change" : "init";
472
513
  const changed = Object.keys(next).reduce((acc, dependency) => {
473
514
  const prevValue = prev ? prev[dependency] : void 0;
474
515
  const nextValue = next[dependency];
475
516
  if (!prev || !Object.is(prevValue, nextValue)) {
476
- acc.push({ dependency, previous: safeDescribeHookValue(prevValue), next: safeDescribeHookValue(nextValue) });
517
+ acc.push({
518
+ dependency,
519
+ previous: safeDescribeHookValue(prevValue),
520
+ next: safeDescribeHookValue(nextValue)
521
+ });
477
522
  }
478
523
  return acc;
479
524
  }, []);
480
525
  if (!changed.length) return null;
481
526
  return { type: "hook", hook, reason, changes: changed };
482
527
  }
483
- function trackHookTrigger({ ref, hook, providerDebug, snapshot }) {
528
+ function trackHookTrigger({
529
+ ref,
530
+ hook,
531
+ providerDebug,
532
+ snapshot
533
+ }) {
484
534
  const prev = ref.current;
485
535
  ref.current = snapshot;
486
536
  if (!providerDebug?.logger || !providerDebug?.hook) return;
@@ -495,6 +545,7 @@ function buildSocketProvider(args) {
495
545
  {
496
546
  events,
497
547
  baseOptions,
548
+ providerDebug: baseOptions.debug,
498
549
  ...props
499
550
  }
500
551
  ),
@@ -516,7 +567,9 @@ function SocketProvider(props) {
516
567
  ref: resolveEffectDebugRef,
517
568
  hook: "resolve_effect",
518
569
  providerDebug: providerDebugRef.current,
519
- snapshot: { resolvedSocket }
570
+ snapshot: {
571
+ resolvedSocket: describeSocketLike(resolvedSocket)
572
+ }
520
573
  });
521
574
  if (!("getSocket" in props)) return;
522
575
  let cancelled = false;
@@ -528,7 +581,7 @@ function SocketProvider(props) {
528
581
  return;
529
582
  }
530
583
  if (!s) {
531
- dbg(providerDebugRef.current, { type: "resolve", phase: "missing" });
584
+ dbg(providerDebugRef.current, { type: "resolve", phase: "socketMissing" });
532
585
  return;
533
586
  }
534
587
  setResolvedSocket(s);
@@ -546,7 +599,11 @@ function SocketProvider(props) {
546
599
  ref: clientMemoDebugRef,
547
600
  hook: "client_memo",
548
601
  providerDebug: providerDebugRef.current,
549
- snapshot: { events, baseOptions, socket }
602
+ snapshot: {
603
+ events: summarizeEvents(events),
604
+ baseOptions: summarizeBaseOptions(baseOptions),
605
+ socket: describeSocketLike(socket)
606
+ }
550
607
  });
551
608
  const client = React.useMemo(() => {
552
609
  if (!socket) {
@@ -562,7 +619,10 @@ function SocketProvider(props) {
562
619
  ref: destroyEffectDebugRef,
563
620
  hook: "destroy_effect",
564
621
  providerDebug: providerDebugRef.current,
565
- snapshot: { client, destroyLeaveMeta }
622
+ snapshot: {
623
+ hasClient: !!client,
624
+ destroyLeaveMeta: summarizeMeta(destroyLeaveMeta, "destroyLeaveMeta")
625
+ }
566
626
  });
567
627
  return () => {
568
628
  if (client) {
@@ -627,23 +687,20 @@ var SocketClient = class {
627
687
  this.dbg({
628
688
  type: "lifecycle",
629
689
  phase: "init",
630
- socketId: this.socket.id,
631
- details: {
632
- heartbeatIntervalMs: this.hb.intervalMs,
633
- heartbeatTimeoutMs: this.hb.timeoutMs,
634
- environment: this.environment
635
- }
690
+ heartbeatIntervalMs: this.hb.intervalMs,
691
+ heartbeatTimeoutMs: this.hb.timeoutMs,
692
+ environment: this.environment
636
693
  });
637
694
  this.logSocketConfigSnapshot("constructor");
638
695
  }
639
696
  this.onConnect = () => {
640
697
  if (!this.socket) {
641
- this.dbg({ type: "connection", phase: "connect", err: "Socket is null" });
698
+ this.dbg({ type: "connection", phase: "connect_event", err: "Socket is null" });
642
699
  throw new Error("Socket is null in onConnect handler");
643
700
  }
644
701
  this.dbg({
645
702
  type: "connection",
646
- phase: "connect",
703
+ phase: "connect_event",
647
704
  id: this.socket.id,
648
705
  details: {
649
706
  nsp: this.getNamespace(this.socket)
@@ -657,12 +714,12 @@ var SocketClient = class {
657
714
  };
658
715
  this.onReconnect = (attempt) => {
659
716
  if (!this.socket) {
660
- this.dbg({ type: "connection", phase: "reconnect", err: "Socket is null" });
717
+ this.dbg({ type: "connection", phase: "reconnect_event", err: "Socket is null" });
661
718
  throw new Error("Socket is null in onReconnect handler");
662
719
  }
663
720
  this.dbg({
664
721
  type: "connection",
665
- phase: "reconnect",
722
+ phase: "reconnect_event",
666
723
  attempt,
667
724
  id: this.socket.id,
668
725
  details: {
@@ -678,12 +735,12 @@ var SocketClient = class {
678
735
  };
679
736
  this.onDisconnect = (reason) => {
680
737
  if (!this.socket) {
681
- this.dbg({ type: "connection", phase: "disconnect", err: "Socket is null" });
738
+ this.dbg({ type: "connection", phase: "disconnect_event", err: "Socket is null" });
682
739
  throw new Error("Socket is null in onDisconnect handler");
683
740
  }
684
741
  this.dbg({
685
742
  type: "connection",
686
- phase: "disconnect",
743
+ phase: "disconnect_event",
687
744
  reason: String(reason),
688
745
  details: {
689
746
  roomsTracked: this.roomCounts.size
@@ -698,12 +755,12 @@ var SocketClient = class {
698
755
  };
699
756
  this.onConnectError = (err) => {
700
757
  if (!this.socket) {
701
- this.dbg({ type: "connection", phase: "connect_error", err: "Socket is null" });
758
+ this.dbg({ type: "connection", phase: "connect_error_event", err: "Socket is null" });
702
759
  throw new Error("Socket is null in onConnectError handler");
703
760
  }
704
761
  this.dbg({
705
762
  type: "connection",
706
- phase: "connect_error",
763
+ phase: "connect_error_event",
707
764
  err: String(err),
708
765
  details: this.getVerboseDetails({ rawError: err })
709
766
  });
@@ -852,7 +909,7 @@ var SocketClient = class {
852
909
  * call it yourself from any sysHandler to change when heartbeats start.
853
910
  */
854
911
  startHeartbeat() {
855
- this.stopHeartbeat();
912
+ this.stopHeartbeat("stop_before_start");
856
913
  if (!this.socket) {
857
914
  this.dbg({
858
915
  type: "heartbeat",
@@ -913,7 +970,7 @@ var SocketClient = class {
913
970
  * This is called by the default 'sys:disconnect' handler, but you can also
914
971
  * call it yourself from any sysHandler to fully control heartbeat lifecycle.
915
972
  */
916
- stopHeartbeat() {
973
+ stopHeartbeat(reason) {
917
974
  const hadTimer = Boolean(this.hbTimer);
918
975
  if (this.hbTimer) {
919
976
  clearInterval(this.hbTimer);
@@ -922,9 +979,8 @@ var SocketClient = class {
922
979
  this.dbg({
923
980
  type: "heartbeat",
924
981
  phase: "stop",
925
- details: {
926
- hadTimer
927
- }
982
+ reason,
983
+ hadTimer
928
984
  });
929
985
  }
930
986
  emit(event, payload, metadata) {
@@ -1118,7 +1174,7 @@ var SocketClient = class {
1118
1174
  * Call when disposing the client instance.
1119
1175
  */
1120
1176
  destroy(leaveMeta) {
1121
- this.stopHeartbeat();
1177
+ this.stopHeartbeat("destroy");
1122
1178
  const socket = this.socket;
1123
1179
  this.dbg({
1124
1180
  type: "lifecycle",
@@ -1169,7 +1225,7 @@ var SocketClient = class {
1169
1225
  });
1170
1226
  return;
1171
1227
  }
1172
- this.stopHeartbeat();
1228
+ this.stopHeartbeat("disconnect");
1173
1229
  this.socket.disconnect();
1174
1230
  this.dbg({
1175
1231
  type: "connection",
@@ -1207,6 +1263,8 @@ export {
1207
1263
  buildRoomPayloadSchema,
1208
1264
  buildSocketProvider,
1209
1265
  createRouteClient,
1210
- defaultFetcher
1266
+ defaultFetcher,
1267
+ useSocketClient,
1268
+ useSocketConnection
1211
1269
  };
1212
1270
  //# sourceMappingURL=index.mjs.map