@ryinner/web-socket-manager
Version:
simple ws manager
42 lines • 1.44 kB
JavaScript
import WebSocketManager from './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 WebSocketManager(wsSettings));
}
}
}
getConnection(id) {
const webSocket = this.webSockets.get(id);
if (webSocket !== undefined) {
return webSocket;
}
throw new Error(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 WebSocketManager(webSocketSettingsOneOrMultiply);
}
else {
return new WebSocketList(webSocketSettingsOneOrMultiply);
}
}
function isWebSocketSettings(settings) {
return 'url' in settings && typeof settings.url === 'string';
}
export default createWebSocket;
export const WS_DOESNT_EXISTS = 'WebSocket doesn\'t exists';
//# sourceMappingURL=websocketFactory.js.map