@emeryld/rrroutes-client 2.8.8 → 2.8.10

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.
@@ -30,8 +30,8 @@ export type UseSocketConnectionDebugOptions = {
30
30
  };
31
31
  export type UseSocketRoomsArgs<C extends SocketConnectionConfigOutput> = {
32
32
  rooms?: Rooms;
33
- joinMeta: SocketSchemaOutput<C['joinMetaMessage']>;
34
- leaveMeta: SocketSchemaOutput<C['leaveMetaMessage']>;
33
+ joinMeta?: SocketSchemaOutput<C['joinMetaMessage']>;
34
+ leaveMeta?: SocketSchemaOutput<C['leaveMetaMessage']>;
35
35
  autoJoin?: boolean;
36
36
  autoLeave?: boolean;
37
37
  enabled?: boolean;
@@ -34,8 +34,8 @@ export declare function buildSocketProvider<T extends EventMap, C extends Socket
34
34
  }): {
35
35
  SocketProvider: (props: React.PropsWithChildren<ProviderRuntimeSocket & {
36
36
  fallback?: React.ReactNode;
37
- destroyLeaveMeta: SocketSchemaOutput<C["leaveMetaMessage"]>;
38
- }>) => import("react/jsx-runtime").JSX.Element;
37
+ destroyLeaveMeta?: SocketSchemaOutput<C["leaveMetaMessage"]>;
38
+ }>) => React.JSX.Element;
39
39
  useSocketClient: () => SocketClient<T, C> | null;
40
40
  useSocketRooms: (p: UseSocketRoomsArgs<C>) => void;
41
41
  useSocketEvent: <K extends keyof T & string>(p: UseSocketEventArgs<T, K, C>) => void;
@@ -43,6 +43,28 @@ export type HeartbeatClientOptions = {
43
43
  /** Give up waiting for pong after this many ms. Default 7_500. */
44
44
  timeoutMs?: number;
45
45
  };
46
+ export type SocketReconnectionMode = 'managed' | 'manual' | 'inherit';
47
+ /**
48
+ * Controls reconnect ownership independently from room restoration.
49
+ *
50
+ * - `managed`: RRRoutes configures Socket.IO's Manager.
51
+ * - `manual`: RRRoutes disables Manager retries; the consumer calls
52
+ * `client.connect()` (or `socket.connect()`) with its own policy.
53
+ * - `inherit`: RRRoutes leaves the injected Socket.IO Manager untouched.
54
+ *
55
+ * Desired rooms are restored after every successful reconnect by default in
56
+ * all modes, including consumer-managed reconnects.
57
+ */
58
+ export type SocketReconnectionOptions = {
59
+ mode?: SocketReconnectionMode;
60
+ restoreRooms?: boolean;
61
+ attempts?: number;
62
+ delayMs?: number;
63
+ delayMaxMs?: number;
64
+ randomizationFactor?: number;
65
+ timeoutMs?: number | false;
66
+ };
67
+ export type SocketReconnectionConfig = boolean | SocketReconnectionOptions;
46
68
  export type SocketClientOptions<T extends EventMap = EventMap, C extends SocketConnectionConfigOutput = SocketConnectionConfigOutput> = {
47
69
  /** Inject an existing socket.io-client Socket. Can be null while bootstrapping. */
48
70
  socket: MaybeSocket;
@@ -63,6 +85,13 @@ export type SocketClientOptions<T extends EventMap = EventMap, C extends SocketC
63
85
  debug?: SocketClientDebugOptions;
64
86
  /** Optional heartbeat configuration. If provided, the client will start the heartbeat loop on the default 'sys:connect' handler. */
65
87
  heartbeat?: HeartbeatClientOptions;
88
+ /**
89
+ * Reconnect behavior. `true` enables RRRoutes-managed Socket.IO defaults;
90
+ * `false` disables Manager retries for a consumer-managed policy. Omit it
91
+ * to inherit the injected socket's Manager configuration. Room restoration
92
+ * remains enabled unless `restoreRooms` is explicitly false.
93
+ */
94
+ reconnection?: SocketReconnectionConfig;
66
95
  sys: SysEventMap<T, C>;
67
96
  };
68
97
  export declare class SocketClient<T extends EventMap, C extends SocketConnectionConfigOutput = SocketConnectionConfigOutput> {
@@ -74,6 +103,7 @@ export declare class SocketClient<T extends EventMap, C extends SocketConnection
74
103
  private readonly sysEvents;
75
104
  private readonly roomJoinSchema;
76
105
  private readonly roomLeaveSchema;
106
+ private readonly reconnection;
77
107
  private readonly hb;
78
108
  private hbTimer;
79
109
  /** keep references so we can .off() later */
@@ -83,7 +113,9 @@ export declare class SocketClient<T extends EventMap, C extends SocketConnection
83
113
  private readonly onConnectError;
84
114
  private readonly onPong;
85
115
  private readonly roomCounts;
116
+ private readonly roomJoinMeta;
86
117
  private readonly handlerMap;
118
+ private hasConnected;
87
119
  private snapshotSocketConfig;
88
120
  private logSocketConfigSnapshot;
89
121
  private getValidationDetails;
@@ -92,9 +124,17 @@ export declare class SocketClient<T extends EventMap, C extends SocketConnection
92
124
  private getNamespace;
93
125
  constructor(events: T, opts: SocketClientOptions<T, C>);
94
126
  private getSysEvent;
127
+ private resolveReconnectionOptions;
128
+ private configureReconnection;
95
129
  private dbg;
96
130
  /** internal stats snapshot */
97
131
  stats(): ClientStatsSnapshot;
132
+ /**
133
+ * Re-emit joins for all rooms with active references without incrementing
134
+ * their reference counts. This is safe to call after any externally or
135
+ * internally managed reconnect.
136
+ */
137
+ restoreRooms(): Promise<string[]>;
98
138
  private toArray;
99
139
  private rollbackJoinIncrement;
100
140
  private rollbackLeaveDecrement;
@@ -113,8 +153,8 @@ export declare class SocketClient<T extends EventMap, C extends SocketConnection
113
153
  */
114
154
  stopHeartbeat(reason?: string): void;
115
155
  emit<K extends keyof T & string>(event: K, payload: Payload<T, K>, metadata?: Record<string, unknown>): void;
116
- joinRooms(rooms: string[] | string, meta: SocketSchemaOutput<C['joinMetaMessage']>): Promise<() => Promise<void>>;
117
- leaveRooms(rooms: string[] | string, meta: SocketSchemaOutput<C['leaveMetaMessage']>): Promise<void>;
156
+ joinRooms(rooms: string[] | string, meta?: SocketSchemaOutput<C['joinMetaMessage']>): Promise<() => Promise<void>>;
157
+ leaveRooms(rooms: string[] | string, meta?: SocketSchemaOutput<C['leaveMetaMessage']>): Promise<void>;
118
158
  on<K extends keyof T & string>(event: K, handler: (payload: T[K]['__out'], meta?: {
119
159
  envelope: ServerEnvelope<T[K]['__out']>;
120
160
  ctx: ClientCtx;
@@ -123,7 +163,7 @@ export declare class SocketClient<T extends EventMap, C extends SocketConnection
123
163
  * Remove all listeners, stop timers, and leave rooms.
124
164
  * Call when disposing the client instance.
125
165
  */
126
- destroy(leaveMeta: SocketSchemaOutput<C['leaveMetaMessage']>): Promise<void>;
166
+ destroy(leaveMeta?: SocketSchemaOutput<C['leaveMetaMessage']>): Promise<void>;
127
167
  /** Pass-throughs. Managing connection is the caller’s responsibility. */
128
168
  disconnect(): void;
129
169
  connect(): void;
@@ -20,7 +20,7 @@ type DebugDetails = {
20
20
  };
21
21
  export type SocketClientDebugEvent<K extends string = string> = ({
22
22
  type: 'connection';
23
- phase: 'connect_event' | 'reconnect_event' | 'disconnect_event' | 'connect_error_event' | 'connect' | 'disconnect';
23
+ phase: 'connect_event' | 'reconnect_event' | 'reconnect_config' | 'disconnect_event' | 'connect_error_event' | 'connect' | 'disconnect';
24
24
  id?: string;
25
25
  attempt?: number;
26
26
  reason?: string;
@@ -38,7 +38,7 @@ export type SocketClientDebugEvent<K extends string = string> = ({
38
38
  err?: string;
39
39
  } & DebugDetails['details']) | ({
40
40
  type: 'room';
41
- phase: 'join' | 'leave';
41
+ phase: 'join' | 'leave' | 'restore';
42
42
  rooms: string[];
43
43
  err?: string;
44
44
  } & DebugDetails) | ({
@@ -43,14 +43,14 @@ export type SysEventMap<T extends EventMap, C extends SocketConnectionConfigOutp
43
43
  socket: Socket;
44
44
  client: SocketClient<T, C>;
45
45
  rooms: string | string[];
46
- meta: SocketSchemaOutput<C['joinMetaMessage']>;
46
+ meta?: SocketSchemaOutput<C['joinMetaMessage']>;
47
47
  }) => Promise<boolean> | boolean;
48
48
  /** Room leave event, triggered when the client attempts to leave one or more rooms. Return true to allow the leave, or false to deny it. */
49
49
  'sys:room_leave': (args: {
50
50
  socket: Socket;
51
51
  client: SocketClient<T, C>;
52
52
  rooms: string | string[];
53
- meta: SocketSchemaOutput<C['leaveMetaMessage']>;
53
+ meta?: SocketSchemaOutput<C['leaveMetaMessage']>;
54
54
  }) => Promise<boolean> | boolean;
55
55
  };
56
56
  declare const roomValueSchema: z.ZodUnion<readonly [z.ZodArray<z.ZodString>, z.ZodString]>;
@@ -6,11 +6,14 @@ export type RoomState<C extends SocketConnectionConfigOutput> = {
6
6
  joinMeta?: SocketSchemaOutput<C['joinMetaMessage']>;
7
7
  leaveMeta?: SocketSchemaOutput<C['leaveMetaMessage']>;
8
8
  };
9
+ type MetaField<K extends string, V> = undefined extends V ? {
10
+ [P in K]?: V;
11
+ } : {
12
+ [P in K]: V;
13
+ };
9
14
  export type ToRoomsResult<C extends SocketConnectionConfigOutput> = {
10
15
  rooms: RoomsInput;
11
- joinMeta: SocketSchemaOutput<C['joinMetaMessage']>;
12
- leaveMeta: SocketSchemaOutput<C['leaveMetaMessage']>;
13
- };
16
+ } & MetaField<'joinMeta', SocketSchemaOutput<C['joinMetaMessage']>> & MetaField<'leaveMeta', SocketSchemaOutput<C['leaveMetaMessage']>>;
14
17
  export type ToRoomsMeta<AppContext = undefined> = {
15
18
  appContext?: AppContext;
16
19
  };
@@ -22,3 +25,4 @@ export type ToRoomsMapper<L extends AnyLeafLowProfile, C extends SocketConnectio
22
25
  export declare function roomStateEqual<C extends SocketConnectionConfigOutput>(prev: RoomState<C>, next: RoomState<C>): boolean;
23
26
  export declare function mergeRoomState<L extends AnyLeafLowProfile, C extends SocketConnectionConfigOutput>(prev: RoomState<C>, toRoomsResult: ToRoomsResult<C>): RoomState<C>;
24
27
  export declare function roomsFromData<L extends AnyLeafLowProfile, C extends SocketConnectionConfigOutput, AppContext = undefined>(data: DataShape<L> | undefined, args: ArgsFor<L> | undefined, toRooms: ToRoomsMapper<L, C, AppContext>, meta?: ToRoomsMeta<AppContext>): RoomState<C>;
28
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emeryld/rrroutes-client",
3
- "version": "2.8.8",
3
+ "version": "2.8.10",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "devDependencies": {
32
32
  "@jest/globals": "^30.4.1",
33
- "@types/react": "^18.3.28",
33
+ "@types/react": "^18.3.31",
34
34
  "@types/react-native": "^0.73.0"
35
35
  },
36
36
  "repository": {