@tapsioss/client-socket-manager
Version:
<div align="center">
115 lines (114 loc) • 3.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* 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 {
/**
* Creates a new stubbed ClientSocketManager.
*
* @param _uri - Optional URI string, ignored in stub.
* @param options - Optional configuration object containing event handlers.
*/
constructor(_uri, options) {
var _a;
this._connected = false;
this._disposed = false;
this._inputListeners = (_a = options === null || options === void 0 ? void 0 : options.eventHandlers) !== null && _a !== void 0 ? _a : {};
}
/**
* A static session identifier.
* Returns a mock ID if connected, otherwise null.
*/
get id() {
return this._connected ? "__id__" : null;
}
/**
* Whether the stub is considered connected.
*/
get connected() {
return this._connected;
}
/**
* Whether the connection has been recovered after interruption.
* Always returns false in the stub.
*/
get recovered() {
return false;
}
/**
* Whether the client attempts reconnection automatically.
* Always returns false in the stub.
*/
get autoReconnectable() {
return false;
}
/**
* Whether this instance has been disposed.
*/
get disposed() {
return this._disposed;
}
/**
* Emits a message to the server.
* No-op in stub.
*
* @param _args - Event name and payload, ignored in stub.
*/
emit() { }
/**
* 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.
*/
subscribe(_channel, _cb, _options) { }
/**
* Unsubscribes from a socket channel.
* No-op in stub.
*
* @param _channel - Channel name.
* @param _cb - Callback function to remove.
*/
unsubscribe(_channel, _cb) { }
/**
* Simulates connecting to a socket.
* Triggers the `onSocketConnection` event handler if defined.
*/
connect() {
var _a;
this._connected = true;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
(_a = this._inputListeners.onSocketConnection) === null || _a === void 0 ? void 0 : _a.call(this);
}
/**
* Simulates disconnecting the socket.
* Triggers the `onSocketDisconnection` event handler if defined.
*/
disconnect() {
var _a;
this._connected = false;
(_a = this._inputListeners.onSocketDisconnection) === null || _a === void 0 ? void 0 : _a.call(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
this, "io client disconnect");
}
/**
* Cleans up the instance by disconnecting and clearing handlers.
*/
dispose() {
this.disconnect();
this._disposed = true;
this._inputListeners = {};
}
}
/**
* Indicates this is a mock/stub implementation.
*/
ClientSocketManagerStub.__mock__ = true;
exports.default = ClientSocketManagerStub;