From 486b99a7c28781c93ef776bce207381a372cd402 Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Tue, 27 May 2025 17:43:04 +0330 Subject: [PATCH 1/9] refactor(stub): move client socket stub from test-helpers to core package --- .../core/src/ClientSocketManagerStub.ts | 6 +++--- packages/core/src/index.ts | 1 + test-helpers/src/index.ts | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) rename test-helpers/src/mocks/ClientSocketManager.ts => packages/core/src/ClientSocketManagerStub.ts (93%) diff --git a/test-helpers/src/mocks/ClientSocketManager.ts b/packages/core/src/ClientSocketManagerStub.ts similarity index 93% rename from test-helpers/src/mocks/ClientSocketManager.ts rename to packages/core/src/ClientSocketManagerStub.ts index b1fb06d..204a7b1 100644 --- a/test-helpers/src/mocks/ClientSocketManager.ts +++ b/packages/core/src/ClientSocketManagerStub.ts @@ -1,9 +1,9 @@ import type { ClientSocketManagerListenerOptions, ClientSocketManagerOptions, -} from "@tapsioss/client-socket-manager"; +} from "./types.ts"; -class ClientSocketManager { +class ClientSocketManagerStub { private _inputListeners: Partial; private _connected = false; private _disposed = false; @@ -72,4 +72,4 @@ class ClientSocketManager { } } -export default ClientSocketManager; +export default ClientSocketManagerStub; diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d796b66..877d33c 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,4 +1,5 @@ export { default as ClientSocketManager } from "./ClientSocketManager.ts"; +export { default as ClientSocketManagerStub } from "./ClientSocketManagerStub.ts"; export { ManagerReservedEvents, SocketReservedEvents } from "./constants.ts"; export type { ClientSocketManagerListenerOptions, diff --git a/test-helpers/src/index.ts b/test-helpers/src/index.ts index 2b7e4de..4e50670 100644 --- a/test-helpers/src/index.ts +++ b/test-helpers/src/index.ts @@ -1,4 +1,3 @@ export { act, render, screen } from "@testing-library/react"; -export { default as MockClientSocketManager } from "./mocks/ClientSocketManager.ts"; export { default as createPromiseResolvers } from "./promise-resolvers.ts"; export * from "./server.ts"; From 120bfd32f8d4d6138ec532388ea88bc0c7f75d27 Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Tue, 27 May 2025 17:45:51 +0330 Subject: [PATCH 2/9] refactor(stub): add jsdoc to ClientSocketManagerStub --- packages/core/src/ClientSocketManagerStub.ts | 83 +++++++++++++++++++- 1 file changed, 80 insertions(+), 3 deletions(-) diff --git a/packages/core/src/ClientSocketManagerStub.ts b/packages/core/src/ClientSocketManagerStub.ts index 204a7b1..7746ef2 100644 --- a/packages/core/src/ClientSocketManagerStub.ts +++ b/packages/core/src/ClientSocketManagerStub.ts @@ -3,39 +3,98 @@ import type { ClientSocketManagerOptions, } from "./types.ts"; +/** + * A stub implementation of `ClientSocketManager` intended for use in + * test environments or server-side rendering (SSR), where actual socket + * connections are unnecessary or undesired. + * + * Provides no-op methods and tracks basic connection/disposal state. + */ class ClientSocketManagerStub { + /** + * Internal storage for event handler callbacks. + */ private _inputListeners: Partial; + + /** + * Tracks whether the socket is currently connected. + */ private _connected = false; + + /** + * Tracks whether the socket manager has been disposed. + */ private _disposed = false; + /** + * Indicates this is a mock/stub implementation. + */ public static __mock__ = true; + /** + * Creates a new stubbed ClientSocketManager. + * + * @param _uri - Optional URI string, ignored in stub. + * @param options - Optional configuration object containing event handlers. + */ constructor(_uri?: string, options?: Partial) { this._inputListeners = options?.eventHandlers ?? {}; } + /** + * A static session identifier. + * Returns a mock ID if connected, otherwise null. + */ public get id(): string | null { return this._connected ? "__id__" : null; } + /** + * Whether the stub is considered connected. + */ public get connected(): boolean { return this._connected; } + /** + * Whether the connection has been recovered after interruption. + * Always returns false in the stub. + */ public get recovered(): boolean { return false; } + /** + * Whether the client attempts reconnection automatically. + * Always returns false in the stub. + */ public get autoReconnectable(): boolean { return false; } - public get disposed() { + /** + * Whether this instance has been disposed. + */ + public get disposed(): boolean { return this._disposed; } - public emit() {} - + /** + * Emits a message to the server. + * No-op in stub. + * + * @param _args - Event name and payload, ignored in stub. + */ + public emit(): void {} + + /** + * Subscribes to a socket channel. + * No-op in stub. + * + * @param _channel - Channel name. + * @param _cb - Callback function. + * @param _options - Optional configuration for signal and subscription completion. + */ public subscribe( _channel: string, _cb: () => void, @@ -45,8 +104,19 @@ class ClientSocketManagerStub { }, ): void {} + /** + * Unsubscribes from a socket channel. + * No-op in stub. + * + * @param _channel - Channel name. + * @param _cb - Callback function to remove. + */ public unsubscribe(_channel: string, _cb: () => void): void {} + /** + * Simulates connecting to a socket. + * Triggers the `onSocketConnection` event handler if defined. + */ public connect(): void { this._connected = true; @@ -54,6 +124,10 @@ class ClientSocketManagerStub { this._inputListeners.onSocketConnection?.call(this as any); } + /** + * Simulates disconnecting the socket. + * Triggers the `onSocketDisconnection` event handler if defined. + */ public disconnect(): void { this._connected = false; @@ -64,6 +138,9 @@ class ClientSocketManagerStub { ); } + /** + * Cleans up the instance by disconnecting and clearing handlers. + */ public dispose(): void { this.disconnect(); From 541c1a14afe9bec147f28bd58e1cafea1fdcae37 Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Tue, 27 May 2025 18:32:51 +0330 Subject: [PATCH 3/9] refactor(stub): use stub from core packages in tests --- .../react/src/SocketClientProvider.test.tsx | 17 +++++++++-------- packages/react/src/useSocketClient.test.tsx | 17 +++++++++-------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/react/src/SocketClientProvider.test.tsx b/packages/react/src/SocketClientProvider.test.tsx index c06c0bb..ca40a6a 100644 --- a/packages/react/src/SocketClientProvider.test.tsx +++ b/packages/react/src/SocketClientProvider.test.tsx @@ -1,11 +1,7 @@ -import { - MockClientSocketManager as __MockClientSocketManager__, - act, - render, - screen, -} from "@repo/test-helpers"; +import { act, render, screen } from "@repo/test-helpers"; import { ClientSocketManager, + ClientSocketManagerStub, type ClientSocketManagerOptions, } from "@tapsioss/client-socket-manager"; import { @@ -21,11 +17,16 @@ import { SocketContext } from "./Context.ts"; import SocketClientProvider from "./SocketClientProvider.tsx"; // Mock the ClientSocketManager class -vitest.mock("@tapsioss/client-socket-manager", () => { +vitest.mock("@tapsioss/client-socket-manager", async () => { + const actualImports = await vitest.importActual( + "@tapsioss/client-socket-manager", + ); + return { + ...actualImports, ClientSocketManager: vitest.fn( (uri: string, options?: Partial) => - new __MockClientSocketManager__(uri, options), + new ClientSocketManagerStub(uri, options), ), }; }); diff --git a/packages/react/src/useSocketClient.test.tsx b/packages/react/src/useSocketClient.test.tsx index 101e6d7..a728d68 100644 --- a/packages/react/src/useSocketClient.test.tsx +++ b/packages/react/src/useSocketClient.test.tsx @@ -1,11 +1,7 @@ -import { - MockClientSocketManager as __MockClientSocketManager__, - act, - render, - screen, -} from "@repo/test-helpers"; +import { act, render, screen } from "@repo/test-helpers"; import { ClientSocketManager, + ClientSocketManagerStub, type ClientSocketManagerOptions, } from "@tapsioss/client-socket-manager"; import { @@ -22,11 +18,16 @@ import { ConnectionStatus } from "./constants.ts"; import useSocketClient from "./useSocketClient.ts"; // Mock the ClientSocketManager class -vitest.mock("@tapsioss/client-socket-manager", () => { +vitest.mock("@tapsioss/client-socket-manager", async () => { + const actualImports = await vitest.importActual( + "@tapsioss/client-socket-manager", + ); + return { + ...actualImports, ClientSocketManager: vitest.fn( (uri: string, options?: Partial) => - new __MockClientSocketManager__(uri, options), + new ClientSocketManagerStub(uri, options), ), }; }); From aa00990c5b8c16d79ba5095f74ff38c599a08e08 Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Tue, 27 May 2025 18:41:26 +0330 Subject: [PATCH 4/9] feat(react): add `shouldUseStob` property to `SocketClientProvider` --- packages/react/src/Context.ts | 8 ++- packages/react/src/SocketClientProvider.tsx | 74 +++++++++++++-------- packages/react/src/types.ts | 3 + 3 files changed, 55 insertions(+), 30 deletions(-) diff --git a/packages/react/src/Context.ts b/packages/react/src/Context.ts index 8b60d2b..5601a77 100644 --- a/packages/react/src/Context.ts +++ b/packages/react/src/Context.ts @@ -1,11 +1,15 @@ import * as React from "react"; -import type { ConnectionStatusValues, SocketInstance } from "./types"; +import type { + ConnectionStatusValues, + SocketInstance, + SocketInstanceStub, +} from "./types"; export type SocketContextValue = { /** * The socket client instance. */ - socket: SocketInstance | null; + socket: SocketInstance | SocketInstanceStub | null; /** * The connection status of the socket instance. */ diff --git a/packages/react/src/SocketClientProvider.tsx b/packages/react/src/SocketClientProvider.tsx index f1eabd7..ad81441 100644 --- a/packages/react/src/SocketClientProvider.tsx +++ b/packages/react/src/SocketClientProvider.tsx @@ -1,4 +1,7 @@ -import { ClientSocketManager } from "@tapsioss/client-socket-manager"; +import { + ClientSocketManager as ClientSocketManagerOriginal, + ClientSocketManagerStub, +} from "@tapsioss/client-socket-manager"; import * as React from "react"; import { ConnectionStatus } from "./constants.ts"; import { SocketContext, type SocketContextValue } from "./Context.ts"; @@ -7,7 +10,12 @@ import type { SocketClientProviderProps, } from "./types"; -const __SINGLETON_REFS__: Record = {}; +const __SINGLETON_REFS__: Record< + string, + | InstanceType + | InstanceType + | null +> = {}; const SocketClientProvider = (props: SocketClientProviderProps) => { const { children, uri, ...options } = props; @@ -19,37 +27,47 @@ const SocketClientProvider = (props: SocketClientProviderProps) => { const [connectionStatus, setConnectionStatus] = React.useState(ConnectionStatus.DISCONNECTED); + const registerClientSocketManager = ( + client: SocketContextValue["socket"], + ) => { + setClientInstance(client); + + __SINGLETON_REFS__[uri] = client; + }; + React.useEffect(() => { if (!__SINGLETON_REFS__[uri]) { - const client = new ClientSocketManager(uri, { - ...options, - eventHandlers: { - ...(options.eventHandlers ?? {}), - onSocketConnection() { - options.eventHandlers?.onSocketConnection?.call(client); - - setConnectionStatus(ConnectionStatus.CONNECTED); - }, - onSocketDisconnection(reason, details) { - options.eventHandlers?.onSocketDisconnection?.call( - client, - reason, - details, - ); - - setConnectionStatus(ConnectionStatus.DISCONNECTED); - }, - onReconnecting(attempt) { - options.eventHandlers?.onReconnecting?.call(client, attempt); + if (props.shouldUseStob) { + registerClientSocketManager(new ClientSocketManagerStub(uri, {})); + } else { + const client = new ClientSocketManagerOriginal(uri, { + ...options, + eventHandlers: { + ...(options.eventHandlers ?? {}), + onSocketConnection() { + options.eventHandlers?.onSocketConnection?.call(client); - setConnectionStatus(ConnectionStatus.RECONNECTING); - }, - }, - }); + setConnectionStatus(ConnectionStatus.CONNECTED); + }, + onSocketDisconnection(reason, details) { + options.eventHandlers?.onSocketDisconnection?.call( + client, + reason, + details, + ); + + setConnectionStatus(ConnectionStatus.DISCONNECTED); + }, + onReconnecting(attempt) { + options.eventHandlers?.onReconnecting?.call(client, attempt); - setClientInstance(client); + setConnectionStatus(ConnectionStatus.RECONNECTING); + }, + }, + }); - __SINGLETON_REFS__[uri] = client; + registerClientSocketManager(client); + } } else { throw new Error( [ diff --git a/packages/react/src/types.ts b/packages/react/src/types.ts index 5746760..e6ae3d8 100644 --- a/packages/react/src/types.ts +++ b/packages/react/src/types.ts @@ -1,6 +1,7 @@ import type { ClientSocketManager, ClientSocketManagerOptions, + ClientSocketManagerStub, } from "@tapsioss/client-socket-manager"; import type * as React from "react"; import type { ConnectionStatus } from "./constants.ts"; @@ -11,10 +12,12 @@ type StatusType = typeof ConnectionStatus; export type ConnectionStatusValues = StatusType[keyof StatusType]; export type SocketInstance = ClientSocketManager; +export type SocketInstanceStub = ClientSocketManagerStub; export type SocketClientHookReturnType = SocketContextValue; export type SocketClientProviderProps = { children: React.ReactNode; uri: string; + shouldUseStob?: boolean; } & Partial; From 7469153de628faa1067d9d3eed35bfc43d4a778d Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Tue, 27 May 2025 19:22:05 +0330 Subject: [PATCH 5/9] docs: update jsdocs --- packages/react/src/SocketClientProvider.tsx | 26 +++++++++++++--- packages/react/src/types.ts | 34 +++++++++++++++++++++ packages/react/src/useSocketClient.ts | 17 +++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/packages/react/src/SocketClientProvider.tsx b/packages/react/src/SocketClientProvider.tsx index ad81441..7ff6d06 100644 --- a/packages/react/src/SocketClientProvider.tsx +++ b/packages/react/src/SocketClientProvider.tsx @@ -17,6 +17,28 @@ const __SINGLETON_REFS__: Record< | null > = {}; +/** + * React provider that initializes and manages a socket connection via `ClientSocketManager`. + * + * It optionally uses a stubbed socket manager in server-side rendering or testing environments. + * The provider ensures a singleton instance per URI and updates the connection status accordingly. + * + * Properties: + * - `children` (`React.ReactNode`): React children to render within the provider. + * - `uri` (`string`): The URI to connect the socket client to. + * - `shouldUseStob?` (`boolean`): Optional flag indicating whether to use the stubbed version of `ClientSocketManager` (useful for SSR or testing). + * - Additional props from `ClientSocketManagerOptions` can be provided, such as `eventHandlers`, `reconnectionDelay`, etc. + * + * @param props - Props for the provider, including connection URI, stub flag, and socket manager options. + * @returns A context provider that supplies the socket instance and its connection status. + * + * @example + * ```tsx + * + * + * + * ``` + */ const SocketClientProvider = (props: SocketClientProviderProps) => { const { children, uri, ...options } = props; @@ -31,7 +53,6 @@ const SocketClientProvider = (props: SocketClientProviderProps) => { client: SocketContextValue["socket"], ) => { setClientInstance(client); - __SINGLETON_REFS__[uri] = client; }; @@ -46,7 +67,6 @@ const SocketClientProvider = (props: SocketClientProviderProps) => { ...(options.eventHandlers ?? {}), onSocketConnection() { options.eventHandlers?.onSocketConnection?.call(client); - setConnectionStatus(ConnectionStatus.CONNECTED); }, onSocketDisconnection(reason, details) { @@ -55,12 +75,10 @@ const SocketClientProvider = (props: SocketClientProviderProps) => { reason, details, ); - setConnectionStatus(ConnectionStatus.DISCONNECTED); }, onReconnecting(attempt) { options.eventHandlers?.onReconnecting?.call(client, attempt); - setConnectionStatus(ConnectionStatus.RECONNECTING); }, }, diff --git a/packages/react/src/types.ts b/packages/react/src/types.ts index e6ae3d8..ba67266 100644 --- a/packages/react/src/types.ts +++ b/packages/react/src/types.ts @@ -7,17 +7,51 @@ import type * as React from "react"; import type { ConnectionStatus } from "./constants.ts"; import type { SocketContextValue } from "./Context.ts"; +/** + * Type representing the union of all possible connection status values + * defined in `ConnectionStatus`. + */ type StatusType = typeof ConnectionStatus; +/** + * All valid connection state values for the socket client. + */ export type ConnectionStatusValues = StatusType[keyof StatusType]; +/** + * The default ClientSocketManager implementation used in production. + */ export type SocketInstance = ClientSocketManager; + +/** + * A stubbed version of the ClientSocketManager, typically used in + * testing or server-side rendering (SSR) environments. + */ export type SocketInstanceStub = ClientSocketManagerStub; +/** + * The return type of the `useSocketClient` hook, + * derived from the context value shape. + */ export type SocketClientHookReturnType = SocketContextValue; +/** + * Props for the `SocketClientProvider` component. + */ export type SocketClientProviderProps = { + /** + * React children to render within the provider. + */ children: React.ReactNode; + + /** + * The URI to connect the socket client to. + */ uri: string; + + /** + * Optional flag indicating whether to use the stubbed version of + * ClientSocketManager. This is useful for SSR or tests. + */ shouldUseStob?: boolean; } & Partial; diff --git a/packages/react/src/useSocketClient.ts b/packages/react/src/useSocketClient.ts index 02bb9af..7ff403f 100644 --- a/packages/react/src/useSocketClient.ts +++ b/packages/react/src/useSocketClient.ts @@ -2,6 +2,23 @@ import * as React from "react"; import { SocketContext } from "./Context.ts"; import type { SocketClientHookReturnType } from "./types.ts"; +/** + * A React hook that provides access to the current socket client instance and its connection status. + * + * This hook must be used within a ``, otherwise it will throw an error. + * + * @throws Will throw an error if used outside of a ``. + * + * @returns {SocketClientHookReturnType} An object containing the socket client instance and its connection status. + * + * @example + * ```tsx + * const { socket, connectionStatus } = useSocketClient(); + * if (socket?.connected) { + * socket.emit("message", { text: "Hello" }); + * } + * ``` + */ const useSocketClient = (): SocketClientHookReturnType => { const ctx = React.useContext(SocketContext); From db272f91308ec2555fa101fd589711125696f021 Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Tue, 27 May 2025 19:22:54 +0330 Subject: [PATCH 6/9] docs: update readme files --- packages/core/README.md | 34 ++++++++++++++++++++++++++++++++++ packages/react/README.md | 4 +++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/core/README.md b/packages/core/README.md index 06f6e48..ce734b0 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -190,6 +190,40 @@ dispose(): void; Disposes of the socket, manager, and engine, ensuring all connections are closed and cleaned up. + +## `ClientSocketManagerStub` + +The package also exports a stubbed version of the socket manager for use in **testing** or **server-side rendering (SSR)** environments: + +```ts +import { ClientSocketManagerStub } from '@tapsioss/client-socket-manager'; + +const stub = new ClientSocketManagerStub("mock://url", { + eventHandlers: { + onSocketConnection() { + console.log("Simulated connection"); + }, + }, +}); + +stub.connect(); // Triggers onSocketConnection +stub.emit("message", "noop"); // No-op +stub.dispose(); // Marks the client as disposed +``` + +### Why use the stub? + +- Prevents actual network communication in unit tests and SSR. +- Mimics the API surface of the real `ClientSocketManager`. +- Triggers configured event handlers like `onSocketConnection` and `onSocketDisconnection`. + +### Stub Behavior Summary + +- Methods like `emit`, `subscribe`, and `unsubscribe` are no-ops. +- `connect()` and `disconnect()` simulate connection lifecycle events. +- The `connected`, `disposed`, `id`, and other properties behave consistently with a mock socket. + + ## License This project is licensed under the terms of the [MIT license](https://github.com/Tap30/client-socket-manager/blob/main/packages/core/LICENSE). diff --git a/packages/react/README.md b/packages/react/README.md index 52e60d4..6fec8a6 100644 --- a/packages/react/README.md +++ b/packages/react/README.md @@ -57,6 +57,7 @@ const App = () => { return ( Date: Wed, 28 May 2025 14:48:49 +0330 Subject: [PATCH 7/9] Update ClientSocketManagerStub.ts --- packages/core/src/ClientSocketManagerStub.ts | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/packages/core/src/ClientSocketManagerStub.ts b/packages/core/src/ClientSocketManagerStub.ts index 7746ef2..70489ca 100644 --- a/packages/core/src/ClientSocketManagerStub.ts +++ b/packages/core/src/ClientSocketManagerStub.ts @@ -11,19 +11,9 @@ import type { * Provides no-op methods and tracks basic connection/disposal state. */ class ClientSocketManagerStub { - /** - * Internal storage for event handler callbacks. - */ private _inputListeners: Partial; - - /** - * Tracks whether the socket is currently connected. - */ + private _connected = false; - - /** - * Tracks whether the socket manager has been disposed. - */ private _disposed = false; /** From cac3990cbb3523fd12d52433e8cb591f683570c9 Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Wed, 28 May 2025 17:30:38 +0330 Subject: [PATCH 8/9] fix: resolve lint issue --- packages/core/src/ClientSocketManagerStub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/ClientSocketManagerStub.ts b/packages/core/src/ClientSocketManagerStub.ts index 70489ca..ff2de33 100644 --- a/packages/core/src/ClientSocketManagerStub.ts +++ b/packages/core/src/ClientSocketManagerStub.ts @@ -12,7 +12,7 @@ import type { */ class ClientSocketManagerStub { private _inputListeners: Partial; - + private _connected = false; private _disposed = false; From 30f9f798c22ee29ce42267f81ff90b6631db583f Mon Sep 17 00:00:00 2001 From: Amirhossein Alibakhshi Date: Wed, 28 May 2025 17:36:37 +0330 Subject: [PATCH 9/9] docs: add changesets --- .changeset/open-moles-hug.md | 6 ++++++ .changeset/thick-lemons-say.md | 6 ++++++ 2 files changed, 12 insertions(+) create mode 100644 .changeset/open-moles-hug.md create mode 100644 .changeset/thick-lemons-say.md diff --git a/.changeset/open-moles-hug.md b/.changeset/open-moles-hug.md new file mode 100644 index 0000000..517c132 --- /dev/null +++ b/.changeset/open-moles-hug.md @@ -0,0 +1,6 @@ +--- +"@tapsioss/react-client-socket-manager": minor +--- + +Add `shouldUseStob` optional property to the `SocketClientProvider` for using the `ClientSocketManagerStub` instead of `ClientSocketManager` for SSR and tests. + \ No newline at end of file diff --git a/.changeset/thick-lemons-say.md b/.changeset/thick-lemons-say.md new file mode 100644 index 0000000..79a6468 --- /dev/null +++ b/.changeset/thick-lemons-say.md @@ -0,0 +1,6 @@ +--- +"@tapsioss/client-socket-manager": minor +--- + +Add `ClientSocketManagerStub` class to the package for SSR and tests. + \ No newline at end of file