@emeryld/rrroutes-client 2.6.4 → 2.6.6

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.
@@ -1,9 +1,9 @@
1
1
  import type { EventMap, SocketConnectionConfigOutput, SocketSchemaOutput } from '@emeryld/rrroutes-contract';
2
2
  import * as React from 'react';
3
3
  import type { Socket } from 'socket.io-client';
4
- import { SocketClient, type SocketClientOptions } from './socket.client.core';
5
4
  import { type UseSocketConnectionArgs } from './socket.client.context.connection';
6
5
  import { SocketProviderDebugOptions } from './socket.client.context.debug';
6
+ import { SocketClient, type SocketClientOptions } from './socket.client.core';
7
7
  type BaseOptions<T extends EventMap, C extends SocketConnectionConfigOutput> = Omit<SocketClientOptions<T, C>, 'socket'>;
8
8
  type ProviderRuntimeSocket = {
9
9
  socket: Socket | null;
@@ -11,7 +11,9 @@ type ProviderRuntimeSocket = {
11
11
  getSocket: () => Socket | Promise<Socket>;
12
12
  };
13
13
  export declare function buildSocketProvider<T extends EventMap, C extends SocketConnectionConfigOutput>(args: {
14
+ /** The events map for the SocketClient. This is required to ensure type safety when using the client and connection hooks. */
14
15
  events: T;
16
+ /** The base options for the SocketClient. This includes all options except the socket instance and debug options. */
15
17
  options: Omit<BaseOptions<T, C>, 'debug'> & {
16
18
  debug: BaseOptions<T, C>['debug'] & SocketProviderDebugOptions;
17
19
  };
@@ -46,9 +46,22 @@ export type HeartbeatClientOptions = {
46
46
  export type SocketClientOptions<T extends EventMap = EventMap, C extends SocketConnectionConfigOutput = SocketConnectionConfigOutput> = {
47
47
  /** Inject an existing socket.io-client Socket. Can be null while bootstrapping. */
48
48
  socket: MaybeSocket;
49
+ /** The configuration of the ping/pong payload schemas and room join/leave meta message schemas. */
49
50
  config: C;
51
+ /** Optional debug configuration. */
50
52
  environment?: 'development' | 'production';
53
+ /** Optional debug configuration.
54
+ * emit: when emitting an event
55
+ * receive: when receiving an event
56
+ * connection: on connect/reconnect/disconnect/connect_error
57
+ * lifecycle: on client init and destroy
58
+ * heartbeat: on ping emit, pong receive, and validation failures
59
+ * config: when logging the socket config snapshot (only if debug.logger is set)
60
+ * room: on room join and leave attempts
61
+ * register: when registering and unregistering event handlers
62
+ */
51
63
  debug?: SocketClientDebugOptions<keyof T & string>;
64
+ /** Optional heartbeat configuration. If provided, the client will start the heartbeat loop on the default 'sys:connect' handler. */
52
65
  heartbeat?: HeartbeatClientOptions;
53
66
  sys: SysEventMap<T, C>;
54
67
  };
@@ -75,6 +88,7 @@ export declare class SocketClient<T extends EventMap, C extends SocketConnection
75
88
  private logSocketConfigSnapshot;
76
89
  private getValidationDetails;
77
90
  private getVerboseDetails;
91
+ private formatError;
78
92
  private getNamespace;
79
93
  constructor(events: T, opts: SocketClientOptions<T, C>);
80
94
  private getSysEvent;
@@ -1,43 +1,51 @@
1
+ import type { EventMap, SocketConnectionConfigOutput, SocketSchemaOutput } from '@emeryld/rrroutes-contract';
1
2
  import type { Socket } from 'socket.io-client';
2
3
  import { z } from 'zod';
3
- import type { EventMap, SocketConnectionConfigOutput, SocketSchemaOutput } from '@emeryld/rrroutes-contract';
4
4
  import type { SocketClient } from './socket.client.core';
5
5
  export type MaybeSocket = Socket | null;
6
6
  export type SysEventMap<T extends EventMap, C extends SocketConnectionConfigOutput = SocketConnectionConfigOutput> = {
7
+ /** Connect event, triggered when the socket successfully connects to the server. */
7
8
  'sys:connect': (args: {
8
9
  socket: Socket;
9
10
  client: SocketClient<T, C>;
10
11
  }) => Promise<void> | void;
12
+ /** Disconnect event, triggered when the socket disconnects from the server. */
11
13
  'sys:disconnect': (args: {
12
14
  socket: Socket;
13
15
  client: SocketClient<T, C>;
14
16
  reason: string;
15
17
  }) => Promise<void> | void;
18
+ /** Reconnect attempt event, triggered when the socket attempts to reconnect after a disconnection. */
16
19
  'sys:reconnect': (args: {
17
20
  socket: Socket;
18
21
  attempt: number;
19
22
  client: SocketClient<T, C>;
20
23
  }) => Promise<void> | void;
24
+ /** Connect error event, triggered when the socket encounters an error while trying to connect to the server. */
21
25
  'sys:connect_error': (args: {
22
26
  socket: Socket;
23
27
  client: SocketClient<T, C>;
24
28
  error: string;
25
29
  }) => Promise<void> | void;
30
+ /** Ping event, triggered by the server to check if the client is still responsive. The client can respond with a pong event, optionally including a payload defined by the server. */
26
31
  'sys:ping': (args: {
27
32
  socket: Socket;
28
33
  client: SocketClient<T, C>;
29
34
  }) => Promise<SocketSchemaOutput<C['pingPayload']>> | SocketSchemaOutput<C['pingPayload']>;
35
+ /** Pong event, triggered in response to a ping event, allowing the client to send a payload back to the server. */
30
36
  'sys:pong': (args: {
31
37
  socket: Socket;
32
38
  client: SocketClient<T, C>;
33
39
  payload: SocketSchemaOutput<C['pongPayload']>;
34
40
  }) => Promise<void> | void;
41
+ /** Room join event, triggered when the client attempts to join one or more rooms. Return true to allow the join, or false to deny it. */
35
42
  'sys:room_join': (args: {
36
43
  socket: Socket;
37
44
  client: SocketClient<T, C>;
38
45
  rooms: string | string[];
39
46
  meta: SocketSchemaOutput<C['joinMetaMessage']>;
40
47
  }) => Promise<boolean> | boolean;
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. */
41
49
  'sys:room_leave': (args: {
42
50
  socket: Socket;
43
51
  client: SocketClient<T, C>;
@@ -8,8 +8,11 @@ type SocketedRouteResult<L extends AnyLeafLowProfile> = (L['cfg']['feed'] extend
8
8
  rooms: string[];
9
9
  };
10
10
  export type SocketedRouteOptions<L extends AnyLeafLowProfile, Events extends EventMap, C extends SocketConnectionConfigOutput> = {
11
+ /** The built route to compose with socket wiring. This should be a route built with the `buildRoute` function */
11
12
  built: SocketedBuilt<L>;
13
+ /** A function that derives the rooms to join from the endpoint data. This will be called on initial render and whenever the endpoint data changes. */
12
14
  toRooms: ToRoomsMapper<L, C>;
15
+ /** An object mapping socket event names to functions that apply incoming socket messages to the endpoint data. Each function receives the previous data, the socket message payload, and an optional meta object containing the server envelope and client context. The function should return the new data (new referrence), or null to indicate no change. */
13
16
  applySocket: {
14
17
  [K in keyof Events & string]?: (prev: DataShape<L> | undefined, payload: Payload<Events, K>, meta: {
15
18
  envelope?: ServerEnvelope<Events, keyof Events & string>;
@@ -17,7 +20,14 @@ export type SocketedRouteOptions<L extends AnyLeafLowProfile, Events extends Eve
17
20
  args: UseEndpointArgs<L>;
18
21
  }) => DataShape<L> | undefined | null;
19
22
  };
23
+ /** A hook that returns the SocketClient instance. This is required to access the socket client within the route and apply socket messages. */
20
24
  useSocketClient: () => SocketClient<Events, C>;
25
+ /** Debug options for the socketed route. This is optional and can be used to enable debug logging for the route's internal operations, such as room joining and socket message application.
26
+ * client: Logs when the socket client becomes available or unavailable.
27
+ * render: Logs on each render of the route, including whether the client is available and the current rooms.
28
+ * hook: Logs when key hooks are triggered, such as receiving endpoint data or applying socket messages, along with snapshots of relevant state.
29
+ * room: Logs attempts to join or leave rooms, including reasons for skipping or errors.
30
+ */
21
31
  debug?: SocketedRouteDebugOptions;
22
32
  };
23
33
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emeryld/rrroutes-client",
3
- "version": "2.6.4",
3
+ "version": "2.6.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",