2025-06-15 15:03:13 -04:00
|
|
|
import { IProtocol } from './Protocol';
|
|
|
|
|
import { User } from '../User';
|
2024-08-22 04:29:17 -04:00
|
|
|
|
2025-06-15 15:03:13 -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 {
|
2025-06-15 15:03:13 -04:00
|
|
|
private protocols = new Map<String, IProtocol>();
|
2024-08-22 04:29:17 -04:00
|
|
|
|
2025-06-15 15:03:13 -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) {
|
2025-06-15 15:03:13 -04:00
|
|
|
if (!this.protocols.has(name)) this.protocols.set(name, protocolFactory());
|
2024-08-22 04:29:17 -04:00
|
|
|
}
|
|
|
|
|
|
2025-06-15 15:03:13 -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();
|