move ProtocolManager into its own unit
This commit is contained in:
@@ -40,7 +40,7 @@ export interface ProtocolFlag {
|
||||
}
|
||||
|
||||
// Protocol handlers. This is implemented by a layer that wants to listen to CollabVM protocol messages.
|
||||
export interface IProtocolHandlers {
|
||||
export interface IProtocolMessageHandler {
|
||||
onNop(user: User): void;
|
||||
|
||||
onNoFlag(user: User): void;
|
||||
@@ -95,7 +95,7 @@ export interface IProtocol {
|
||||
dispose(): void;
|
||||
|
||||
// Sets handler object.
|
||||
setHandler(handlers: IProtocolHandlers): void;
|
||||
setHandler(handlers: IProtocolMessageHandler): void;
|
||||
|
||||
// Protocol implementation stuff
|
||||
|
||||
@@ -152,7 +152,7 @@ export interface IProtocol {
|
||||
|
||||
// Base mixin for all concrete protocols to use. Inherit from this!
|
||||
export class ProtocolBase {
|
||||
protected handlers: IProtocolHandlers | null = null;
|
||||
protected handlers: IProtocolMessageHandler | null = null;
|
||||
protected user: User | null = null;
|
||||
|
||||
init(u: User): void {
|
||||
@@ -164,32 +164,7 @@ export class ProtocolBase {
|
||||
this.handlers = null;
|
||||
}
|
||||
|
||||
setHandler(handlers: IProtocolHandlers): void {
|
||||
setHandler(handlers: IProtocolMessageHandler): void {
|
||||
this.handlers = handlers;
|
||||
}
|
||||
}
|
||||
|
||||
// The protocol manager. Holds protocol factories, and provides the ability
|
||||
// to create a protocol by name. Avoids direct dependency on a given list of protocols,
|
||||
// and allows (relatively simple) expansion.
|
||||
export class ProtocolManager {
|
||||
private protocols = new Map<String, () => IProtocol>();
|
||||
|
||||
// Registers a protocol with the given name.
|
||||
registerProtocol(name: string, protocolFactory: () => IProtocol) {
|
||||
if (!this.protocols.has(name)) this.protocols.set(name, protocolFactory);
|
||||
}
|
||||
|
||||
// Creates an instance of a given protocol for a user.
|
||||
createProtocol(name: string, user: User): IProtocol {
|
||||
if (!this.protocols.has(name)) throw new Error(`ProtocolManager does not have protocol \"${name}\"`);
|
||||
|
||||
let factory = this.protocols.get(name)!;
|
||||
let proto = factory();
|
||||
proto.init(user);
|
||||
return proto;
|
||||
}
|
||||
}
|
||||
|
||||
/// Global protocol manager
|
||||
export let TheProtocolManager = new ProtocolManager();
|
||||
|
||||
Reference in New Issue
Block a user