Files
collabvm-1.2.ts/cvmts/src/protocol/Manager.ts

27 lines
939 B
TypeScript
Raw Normal View History

import { IProtocol } from './Protocol';
import { User } from '../User';
2024-08-22 04:29:17 -04:00
// The protocol manager.
// Holds protocols, and provides the ability to obtain them by name.
//
// Avoids direct dependency on a given list of protocols,
// and allows (relatively simple) expansion of the supported protocols.
2024-08-22 04:29:17 -04:00
export class ProtocolManager {
private protocols = new Map<String, IProtocol>();
2024-08-22 04:29:17 -04:00
// Registers a protocol with the given name, creates it, and stores it for later use.
2024-08-22 04:29:17 -04:00
registerProtocol(name: string, protocolFactory: () => IProtocol) {
if (!this.protocols.has(name)) this.protocols.set(name, protocolFactory());
2024-08-22 04:29:17 -04:00
}
// Gets an instance of a protocol.
getProtocol(name: string): IProtocol {
let proto = this.protocols.get(name);
if (proto == undefined) throw new Error(`ProtocolManager does not have protocol \"${name}\"`);
2024-08-22 04:29:17 -04:00
return proto;
}
}
/// Global protocol manager
export let TheProtocolManager = new ProtocolManager();