UNPKG

@ryinner/web-socket-manager

Version:
48 lines 1.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WS_DOESNT_EXISTS = void 0; const websocket_1 = __importDefault(require("./websocket")); class WebSocketList { constructor(settings) { this.webSockets = new Map(); for (const id in settings) { if (Object.prototype.hasOwnProperty.call(settings, id)) { const wsSettings = settings[id]; this.webSockets.set(id, new websocket_1.default(wsSettings)); } } } getConnection(id) { const webSocket = this.webSockets.get(id); if (webSocket !== undefined) { return webSocket; } throw new Error(exports.WS_DOESNT_EXISTS); } addOperation(id, operationSetting) { this.getConnection(id).addOperation(operationSetting); } removeOperation(id, method) { this.getConnection(id).removeOperation(method); } removeHandler(id, method, handler) { this.getConnection(id).removeHandler(method, handler); } } function createWebSocket(webSocketSettingsOneOrMultiply) { if (isWebSocketSettings(webSocketSettingsOneOrMultiply)) { return new websocket_1.default(webSocketSettingsOneOrMultiply); } else { return new WebSocketList(webSocketSettingsOneOrMultiply); } } function isWebSocketSettings(settings) { return 'url' in settings && typeof settings.url === 'string'; } exports.default = createWebSocket; exports.WS_DOESNT_EXISTS = 'WebSocket doesn\'t exists'; //# sourceMappingURL=websocketFactory.js.map