TeaWeb/shared/js/ConnectionManager.ts

143 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-09-24 09:24:31 +00:00
import {ConnectionHandler, DisconnectReason} from "./ConnectionHandler";
import {Registry} from "./events";
2020-09-24 13:51:22 +00:00
import {Stage} from "tc-loader";
2021-01-10 15:26:45 +00:00
import * as loader from "tc-loader";
import {assertMainApplication} from "tc-shared/ui/utils";
assertMainApplication();
export interface ConnectionManagerEvents {
notify_handler_created: {
handlerId: string,
handler: ConnectionHandler
},
/* This will also trigger when a connection gets deleted. So if you're just interested to connect event handler to the active connection,
unregister them from the old handler and register them for the new handler every time */
notify_active_handler_changed: {
oldHandler: ConnectionHandler | undefined,
newHandler: ConnectionHandler | undefined,
oldHandlerId: string | undefined,
newHandlerId: string | undefined
},
/* Will never fire on an active connection handler! */
notify_handler_deleted: {
handlerId: string,
handler: ConnectionHandler
},
notify_handler_order_changed: { }
}
export class ConnectionManager {
private readonly events_: Registry<ConnectionManagerEvents>;
private connectionHandlers: ConnectionHandler[] = [];
private activeConnectionHandler: ConnectionHandler | undefined;
2019-04-04 19:47:52 +00:00
2020-09-25 16:29:42 +00:00
constructor() {
this.events_ = new Registry<ConnectionManagerEvents>();
this.events_.enableDebug("connection-manager");
2021-01-10 15:26:45 +00:00
this.setActiveConnectionHandler(undefined);
}
events() : Registry<ConnectionManagerEvents> {
return this.events_;
2019-04-04 19:47:52 +00:00
}
2021-01-10 15:26:45 +00:00
spawnConnectionHandler() : ConnectionHandler {
2019-04-04 19:47:52 +00:00
const handler = new ConnectionHandler();
2021-04-05 21:05:44 +00:00
handler.initializeHandlerState(this.activeConnectionHandler);
this.connectionHandlers.push(handler);
this.events_.fire("notify_handler_created", { handler: handler, handlerId: handler.handlerId });
2019-04-04 19:47:52 +00:00
return handler;
}
2021-01-10 15:26:45 +00:00
destroyConnectionHandler(handler: ConnectionHandler) {
if(this.connectionHandlers.length <= 1) {
throw "cannot deleted the last connection handler";
}
if(!this.connectionHandlers.remove(handler)) {
throw "unknown connection handler";
}
2019-04-04 19:47:52 +00:00
if(handler.serverConnection) {
const connected = handler.connected;
handler.serverConnection.disconnect("handler destroyed");
handler.handleDisconnect(DisconnectReason.HANDLER_DESTROYED, connected);
}
if(handler === this.activeConnectionHandler) {
2021-01-10 15:26:45 +00:00
this.doSetActiveConnectionHandler(this.connectionHandlers[0]);
}
this.events_.fire("notify_handler_deleted", { handler: handler, handlerId: handler.handlerId });
2019-08-21 08:00:01 +00:00
/* destroy all elements */
handler.destroy();
2019-04-04 19:47:52 +00:00
}
2021-01-10 15:26:45 +00:00
setActiveConnectionHandler(handler: ConnectionHandler) {
if(handler && this.connectionHandlers.indexOf(handler) == -1) {
2019-08-21 08:00:01 +00:00
throw "Handler hasn't been registered or is already obsolete!";
}
if(handler === this.activeConnectionHandler) {
return;
}
2021-01-10 15:26:45 +00:00
this.doSetActiveConnectionHandler(handler);
}
private doSetActiveConnectionHandler(handler: ConnectionHandler) {
const oldHandler = this.activeConnectionHandler;
this.activeConnectionHandler = handler;
this.events_.fire("notify_active_handler_changed", {
oldHandler: oldHandler,
newHandler: handler,
oldHandlerId: oldHandler?.handlerId,
newHandlerId: handler?.handlerId
});
}
2019-04-04 19:47:52 +00:00
swapHandlerOrder(handlerA: ConnectionHandler, handlerB: ConnectionHandler) {
const indexA = this.connectionHandlers.findIndex(handler => handlerA === handler);
const indexB = this.connectionHandlers.findIndex(handler => handlerB === handler);
if(indexA === -1 || indexB === -1 || indexA === indexB) {
return;
}
let temp = this.connectionHandlers[indexA];
this.connectionHandlers[indexA] = this.connectionHandlers[indexB];
this.connectionHandlers[indexB] = temp;
this.events().fire("notify_handler_order_changed");
}
findConnection(handlerId: string) : ConnectionHandler | undefined {
return this.connectionHandlers.find(e => e.handlerId === handlerId);
}
2019-04-04 19:47:52 +00:00
2021-01-10 15:26:45 +00:00
getActiveConnectionHandler() : ConnectionHandler | undefined {
return this.activeConnectionHandler;
2019-04-04 19:47:52 +00:00
}
2021-01-10 15:26:45 +00:00
getAllConnectionHandlers() : ConnectionHandler[] {
return this.connectionHandlers;
2020-12-29 15:53:04 +00:00
}
}
2021-01-10 15:26:45 +00:00
export let server_connections: ConnectionManager;
2020-09-24 13:51:22 +00:00
loader.register_task(Stage.JAVASCRIPT_INITIALIZING, {
name: "server manager init",
function: async () => {
2020-09-25 16:29:42 +00:00
server_connections = new ConnectionManager();
(window as any).server_connections = server_connections;
2020-09-24 13:51:22 +00:00
},
priority: 80
});