@emeryld/rrroutes-client 2.0.13 → 2.1.1

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
@@ -35,7 +35,9 @@ __export(index_exports, {
35
35
  buildRoomPayloadSchema: () => buildRoomPayloadSchema,
36
36
  buildSocketProvider: () => buildSocketProvider,
37
37
  createRouteClient: () => createRouteClient,
38
- defaultFetcher: () => defaultFetcher
38
+ defaultFetcher: () => defaultFetcher,
39
+ useSocketClient: () => useSocketClient,
40
+ useSocketConnection: () => useSocketConnection
39
41
  });
40
42
  module.exports = __toCommonJS(index_exports);
41
43
 
@@ -526,6 +528,7 @@ function buildSocketProvider(args) {
526
528
  {
527
529
  events,
528
530
  baseOptions,
531
+ providerDebug: baseOptions.debug,
529
532
  ...props
530
533
  }
531
534
  ),
@@ -559,7 +562,7 @@ function SocketProvider(props) {
559
562
  return;
560
563
  }
561
564
  if (!s) {
562
- dbg(providerDebugRef.current, { type: "resolve", phase: "missing" });
565
+ dbg(providerDebugRef.current, { type: "resolve", phase: "socketMissing" });
563
566
  return;
564
567
  }
565
568
  setResolvedSocket(s);
@@ -581,11 +584,11 @@ function SocketProvider(props) {
581
584
  });
582
585
  const client = React.useMemo(() => {
583
586
  if (!socket) {
584
- dbg(providerDebugRef.current, { type: "client", phase: "missing" });
587
+ dbg(providerDebugRef.current, { type: "client", phase: "init", missing: true });
585
588
  return null;
586
589
  }
587
590
  const c = new SocketClient(events, { ...baseOptions, socket });
588
- dbg(providerDebugRef.current, { type: "client", phase: "ready" });
591
+ dbg(providerDebugRef.current, { type: "client", phase: "init", missing: false });
589
592
  return c;
590
593
  }, [events, baseOptions, socket]);
591
594
  React.useEffect(() => {
@@ -602,12 +605,8 @@ function SocketProvider(props) {
602
605
  }
603
606
  };
604
607
  }, [client, destroyLeaveMeta]);
605
- if (!client) {
606
- dbg(providerDebugRef.current, { type: "render", phase: "waiting_for_socket" });
607
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback ?? children });
608
- }
609
- dbg(providerDebugRef.current, { type: "render", phase: "provide" });
610
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SocketCtx.Provider, { value: client, children });
608
+ dbg(providerDebugRef.current, { type: "render", hasClient: !!client });
609
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SocketCtx.Provider, { value: client, children: client == null ? fallback ?? children : children });
611
610
  }
612
611
  function useSocketClient() {
613
612
  const ctx = React.useContext(SocketCtx);
@@ -652,46 +651,36 @@ var SocketClient = class {
652
651
  intervalMs: hb.intervalMs ?? 15e3,
653
652
  timeoutMs: hb.timeoutMs ?? 7500
654
653
  };
655
- this.dbg({
656
- type: "lifecycle",
657
- phase: "init_start",
658
- socketId: this.socket?.id ?? void 0,
659
- details: {
660
- environment: this.environment
661
- }
662
- });
663
654
  if (!this.socket) {
664
655
  this.dbg({
665
656
  type: "lifecycle",
666
- phase: "init_socket_missing",
657
+ phase: "init",
667
658
  err: "Socket reference is null during initialization"
668
659
  });
669
660
  } else {
670
661
  this.dbg({
671
662
  type: "lifecycle",
672
- phase: "init_ready",
673
- socketId: this.socket.id,
674
- details: {
675
- heartbeatIntervalMs: this.hb.intervalMs,
676
- heartbeatTimeoutMs: this.hb.timeoutMs
677
- }
663
+ phase: "init",
664
+ heartbeatIntervalMs: this.hb.intervalMs,
665
+ heartbeatTimeoutMs: this.hb.timeoutMs,
666
+ environment: this.environment
678
667
  });
679
- this.logSocketConfigSnapshot("init", "constructor");
668
+ this.logSocketConfigSnapshot("constructor");
680
669
  }
681
670
  this.onConnect = () => {
682
671
  if (!this.socket) {
683
- this.dbg({ type: "connection", phase: "connect", err: "Socket is null" });
672
+ this.dbg({ type: "connection", phase: "connect_event", err: "Socket is null" });
684
673
  throw new Error("Socket is null in onConnect handler");
685
674
  }
686
675
  this.dbg({
687
676
  type: "connection",
688
- phase: "connect",
677
+ phase: "connect_event",
689
678
  id: this.socket.id,
690
679
  details: {
691
680
  nsp: this.getNamespace(this.socket)
692
681
  }
693
682
  });
694
- this.logSocketConfigSnapshot("update", "connect_event");
683
+ this.logSocketConfigSnapshot("connect_event");
695
684
  this.getSysEvent("sys:connect")({
696
685
  socket: this.socket,
697
686
  client: this
@@ -699,19 +688,19 @@ var SocketClient = class {
699
688
  };
700
689
  this.onReconnect = (attempt) => {
701
690
  if (!this.socket) {
702
- this.dbg({ type: "connection", phase: "reconnect", err: "Socket is null" });
691
+ this.dbg({ type: "connection", phase: "reconnect_event", err: "Socket is null" });
703
692
  throw new Error("Socket is null in onReconnect handler");
704
693
  }
705
694
  this.dbg({
706
695
  type: "connection",
707
- phase: "reconnect",
696
+ phase: "reconnect_event",
708
697
  attempt,
709
698
  id: this.socket.id,
710
699
  details: {
711
700
  nsp: this.getNamespace(this.socket)
712
701
  }
713
702
  });
714
- this.logSocketConfigSnapshot("update", "reconnect_event");
703
+ this.logSocketConfigSnapshot("reconnect_event");
715
704
  this.getSysEvent("sys:reconnect")({
716
705
  attempt,
717
706
  socket: this.socket,
@@ -720,18 +709,18 @@ var SocketClient = class {
720
709
  };
721
710
  this.onDisconnect = (reason) => {
722
711
  if (!this.socket) {
723
- this.dbg({ type: "connection", phase: "disconnect", err: "Socket is null" });
712
+ this.dbg({ type: "connection", phase: "disconnect_event", err: "Socket is null" });
724
713
  throw new Error("Socket is null in onDisconnect handler");
725
714
  }
726
715
  this.dbg({
727
716
  type: "connection",
728
- phase: "disconnect",
717
+ phase: "disconnect_event",
729
718
  reason: String(reason),
730
719
  details: {
731
720
  roomsTracked: this.roomCounts.size
732
721
  }
733
722
  });
734
- this.logSocketConfigSnapshot("update", "disconnect_event");
723
+ this.logSocketConfigSnapshot("disconnect_event");
735
724
  this.getSysEvent("sys:disconnect")({
736
725
  reason: String(reason),
737
726
  socket: this.socket,
@@ -740,16 +729,16 @@ var SocketClient = class {
740
729
  };
741
730
  this.onConnectError = (err) => {
742
731
  if (!this.socket) {
743
- this.dbg({ type: "connection", phase: "connect_error", err: "Socket is null" });
732
+ this.dbg({ type: "connection", phase: "connect_error_event", err: "Socket is null" });
744
733
  throw new Error("Socket is null in onConnectError handler");
745
734
  }
746
735
  this.dbg({
747
736
  type: "connection",
748
- phase: "connect_error",
737
+ phase: "connect_error_event",
749
738
  err: String(err),
750
739
  details: this.getVerboseDetails({ rawError: err })
751
740
  });
752
- this.logSocketConfigSnapshot("update", "connect_error_event");
741
+ this.logSocketConfigSnapshot("connect_error_event");
753
742
  this.getSysEvent("sys:connect_error")({
754
743
  error: String(err),
755
744
  socket: this.socket,
@@ -758,14 +747,14 @@ var SocketClient = class {
758
747
  };
759
748
  this.onPong = (raw) => {
760
749
  if (!this.socket) {
761
- this.dbg({ type: "heartbeat", action: "pong_recv", err: "Socket is null" });
750
+ this.dbg({ type: "heartbeat", phase: "pong_recv", err: "Socket is null" });
762
751
  throw new Error("Socket is null in onPong handler");
763
752
  }
764
753
  const parsed = this.config.pongPayload.safeParse(raw);
765
754
  if (!parsed.success) {
766
755
  this.dbg({
767
756
  type: "heartbeat",
768
- action: "validation_failed",
757
+ phase: "validation_failed",
769
758
  err: `pong payload validation failed: ${parsed.error.message}`,
770
759
  details: this.getValidationDetails(parsed.error)
771
760
  });
@@ -774,7 +763,7 @@ var SocketClient = class {
774
763
  const validated = parsed.data;
775
764
  this.dbg({
776
765
  type: "heartbeat",
777
- action: "pong_recv",
766
+ phase: "pong_recv",
778
767
  payload: validated
779
768
  });
780
769
  this.getSysEvent("sys:pong")({
@@ -821,16 +810,16 @@ var SocketClient = class {
821
810
  secure: typeof opts.secure === "boolean" ? opts.secure : void 0
822
811
  };
823
812
  }
824
- logSocketConfigSnapshot(phase, reason) {
813
+ logSocketConfigSnapshot(reason) {
825
814
  if (!this.debug.logger || !this.debug.config) return;
826
815
  const snapshot = this.snapshotSocketConfig(this.socket);
827
816
  this.dbg({
828
817
  type: "config",
829
- phase,
830
- reason,
831
- socketId: this.socket?.id,
832
- snapshot: snapshot ?? void 0,
833
- err: snapshot ? void 0 : "Socket unavailable for config snapshot"
818
+ phase: reason,
819
+ ...snapshot == null ? { err: "Socket is missing. " } : {
820
+ socketId: this.socket?.id,
821
+ snapshot
822
+ }
834
823
  });
835
824
  }
836
825
  getValidationDetails(error) {
@@ -894,11 +883,11 @@ var SocketClient = class {
894
883
  * call it yourself from any sysHandler to change when heartbeats start.
895
884
  */
896
885
  startHeartbeat() {
897
- this.stopHeartbeat();
886
+ this.stopHeartbeat("stop_before_start");
898
887
  if (!this.socket) {
899
888
  this.dbg({
900
889
  type: "heartbeat",
901
- action: "start",
890
+ phase: "start",
902
891
  err: "Socket is null"
903
892
  });
904
893
  return;
@@ -906,7 +895,7 @@ var SocketClient = class {
906
895
  const socket = this.socket;
907
896
  this.dbg({
908
897
  type: "heartbeat",
909
- action: "start",
898
+ phase: "start",
910
899
  details: {
911
900
  intervalMs: this.hb.intervalMs,
912
901
  timeoutMs: this.hb.timeoutMs
@@ -916,7 +905,7 @@ var SocketClient = class {
916
905
  if (!socket) {
917
906
  this.dbg({
918
907
  type: "heartbeat",
919
- action: "tick_skip",
908
+ phase: "tick_skip",
920
909
  err: "Socket missing during heartbeat tick"
921
910
  });
922
911
  return;
@@ -929,7 +918,7 @@ var SocketClient = class {
929
918
  if (!check.success) {
930
919
  this.dbg({
931
920
  type: "heartbeat",
932
- action: "validation_failed",
921
+ phase: "validation_failed",
933
922
  err: "ping payload validation failed",
934
923
  details: this.getValidationDetails(check.error)
935
924
  });
@@ -942,7 +931,7 @@ var SocketClient = class {
942
931
  socket.emit("sys:ping", dataToSend);
943
932
  this.dbg({
944
933
  type: "heartbeat",
945
- action: "ping_emit",
934
+ phase: "ping_emit",
946
935
  payload: dataToSend
947
936
  });
948
937
  };
@@ -955,7 +944,7 @@ var SocketClient = class {
955
944
  * This is called by the default 'sys:disconnect' handler, but you can also
956
945
  * call it yourself from any sysHandler to fully control heartbeat lifecycle.
957
946
  */
958
- stopHeartbeat() {
947
+ stopHeartbeat(reason) {
959
948
  const hadTimer = Boolean(this.hbTimer);
960
949
  if (this.hbTimer) {
961
950
  clearInterval(this.hbTimer);
@@ -963,10 +952,9 @@ var SocketClient = class {
963
952
  }
964
953
  this.dbg({
965
954
  type: "heartbeat",
966
- action: "stop",
967
- details: {
968
- hadTimer
969
- }
955
+ phase: "stop",
956
+ reason,
957
+ hadTimer
970
958
  });
971
959
  }
972
960
  emit(event, payload, metadata) {
@@ -994,7 +982,7 @@ var SocketClient = class {
994
982
  }
995
983
  joinRooms(rooms, meta) {
996
984
  if (!this.socket) {
997
- this.dbg({ type: "room", action: "join", rooms: this.toArray(rooms), err: "Socket is null" });
985
+ this.dbg({ type: "room", phase: "join", rooms: this.toArray(rooms), err: "Socket is null" });
998
986
  throw new Error("Socket is null in joinRooms method");
999
987
  }
1000
988
  if (!this.getSysEvent("sys:room_join")({
@@ -1003,7 +991,7 @@ var SocketClient = class {
1003
991
  socket: this.socket,
1004
992
  client: this
1005
993
  })) {
1006
- this.dbg({ type: "room", action: "join", rooms: this.toArray(rooms), err: "sys:room_join handler aborted join" });
994
+ this.dbg({ type: "room", phase: "join", rooms: this.toArray(rooms), err: "sys:room_join handler aborted join" });
1007
995
  return;
1008
996
  }
1009
997
  const list = this.toArray(rooms);
@@ -1022,7 +1010,7 @@ var SocketClient = class {
1022
1010
  this.rollbackJoinIncrement(toJoin);
1023
1011
  this.dbg({
1024
1012
  type: "room",
1025
- action: "join",
1013
+ phase: "join",
1026
1014
  rooms: toJoin,
1027
1015
  err: "payload validation failed",
1028
1016
  details: this.getValidationDetails(payloadResult.error)
@@ -1032,12 +1020,12 @@ var SocketClient = class {
1032
1020
  const payload = payloadResult.data;
1033
1021
  const normalizedRooms = this.toArray(payload.rooms);
1034
1022
  this.socket.emit("sys:room_join", payload);
1035
- this.dbg({ type: "room", action: "join", rooms: normalizedRooms });
1023
+ this.dbg({ type: "room", phase: "join", rooms: normalizedRooms });
1036
1024
  }
1037
1025
  }
1038
1026
  leaveRooms(rooms, meta) {
1039
1027
  if (!this.socket) {
1040
- this.dbg({ type: "room", action: "leave", rooms: this.toArray(rooms), err: "Socket is null" });
1028
+ this.dbg({ type: "room", phase: "leave", rooms: this.toArray(rooms), err: "Socket is null" });
1041
1029
  throw new Error("Socket is null in leaveRooms method");
1042
1030
  }
1043
1031
  if (!this.getSysEvent("sys:room_leave")({
@@ -1046,7 +1034,7 @@ var SocketClient = class {
1046
1034
  socket: this.socket,
1047
1035
  client: this
1048
1036
  })) {
1049
- this.dbg({ type: "room", action: "leave", rooms: this.toArray(rooms), err: "sys:room_leave handler aborted leave" });
1037
+ this.dbg({ type: "room", phase: "leave", rooms: this.toArray(rooms), err: "sys:room_leave handler aborted leave" });
1050
1038
  return;
1051
1039
  }
1052
1040
  const list = this.toArray(rooms);
@@ -1067,7 +1055,7 @@ var SocketClient = class {
1067
1055
  this.rollbackLeaveDecrement(toLeave);
1068
1056
  this.dbg({
1069
1057
  type: "room",
1070
- action: "leave",
1058
+ phase: "leave",
1071
1059
  rooms: toLeave,
1072
1060
  err: "payload validation failed",
1073
1061
  details: this.getValidationDetails(payloadResult.error)
@@ -1077,14 +1065,14 @@ var SocketClient = class {
1077
1065
  const payload = payloadResult.data;
1078
1066
  const normalizedRooms = this.toArray(payload.rooms);
1079
1067
  this.socket.emit("sys:room_leave", payload);
1080
- this.dbg({ type: "room", action: "leave", rooms: normalizedRooms });
1068
+ this.dbg({ type: "room", phase: "leave", rooms: normalizedRooms });
1081
1069
  }
1082
1070
  }
1083
1071
  on(event, handler) {
1084
1072
  const schema = this.events[event].message;
1085
- this.dbg({ type: "register", action: "register", event });
1073
+ this.dbg({ type: "register", phase: "register", event });
1086
1074
  if (!this.socket) {
1087
- this.dbg({ type: "register", action: "register", event, err: "Socket is null" });
1075
+ this.dbg({ type: "register", phase: "register", event, err: "Socket is null" });
1088
1076
  return () => {
1089
1077
  };
1090
1078
  }
@@ -1152,7 +1140,7 @@ var SocketClient = class {
1152
1140
  s.delete(entry);
1153
1141
  if (s.size === 0) this.handlerMap.delete(String(event));
1154
1142
  }
1155
- this.dbg({ type: "register", action: "unregister", event });
1143
+ this.dbg({ type: "register", phase: "unregister", event });
1156
1144
  };
1157
1145
  }
1158
1146
  /**
@@ -1160,7 +1148,7 @@ var SocketClient = class {
1160
1148
  * Call when disposing the client instance.
1161
1149
  */
1162
1150
  destroy(leaveMeta) {
1163
- this.stopHeartbeat();
1151
+ this.stopHeartbeat("destroy");
1164
1152
  const socket = this.socket;
1165
1153
  this.dbg({
1166
1154
  type: "lifecycle",
@@ -1211,7 +1199,7 @@ var SocketClient = class {
1211
1199
  });
1212
1200
  return;
1213
1201
  }
1214
- this.stopHeartbeat();
1202
+ this.stopHeartbeat("disconnect");
1215
1203
  this.socket.disconnect();
1216
1204
  this.dbg({
1217
1205
  type: "connection",
@@ -1221,7 +1209,7 @@ var SocketClient = class {
1221
1209
  nsp: this.getNamespace(this.socket)
1222
1210
  }
1223
1211
  });
1224
- this.logSocketConfigSnapshot("update", "disconnect_call");
1212
+ this.logSocketConfigSnapshot("disconnect_call");
1225
1213
  }
1226
1214
  connect() {
1227
1215
  if (!this.socket) {
@@ -1241,7 +1229,7 @@ var SocketClient = class {
1241
1229
  nsp: this.getNamespace(this.socket)
1242
1230
  }
1243
1231
  });
1244
- this.logSocketConfigSnapshot("update", "connect_call");
1232
+ this.logSocketConfigSnapshot("connect_call");
1245
1233
  }
1246
1234
  };
1247
1235
  // Annotate the CommonJS export names for ESM import in node:
@@ -1250,6 +1238,8 @@ var SocketClient = class {
1250
1238
  buildRoomPayloadSchema,
1251
1239
  buildSocketProvider,
1252
1240
  createRouteClient,
1253
- defaultFetcher
1241
+ defaultFetcher,
1242
+ useSocketClient,
1243
+ useSocketConnection
1254
1244
  });
1255
1245
  //# sourceMappingURL=index.cjs.map