move some shared stuff into a new baseclass for protocols to implement

will make greenfield (i.e: brand new non-Guac or whatever) protocol implementation a bit less boilerplatey
This commit is contained in:
modeco80
2024-08-21 22:26:36 -04:00
parent 4583531fce
commit 1673f0abd7
2 changed files with 24 additions and 18 deletions

View File

@@ -76,12 +76,15 @@ export interface IProtocolHandlers {
// Abstracts away all of the CollabVM protocol details
export interface IProtocol {
// don't implement this yourself, extend from ProtocolBase
init(u: User): void;
dispose(): void;
// Sets handler object.
setHandler(handlers: IProtocolHandlers): void;
// Protocol implementation stuff
// Parses a single CollabVM protocol message and fires the given handler.
// This function does not catch any thrown errors; it is the caller's responsibility
// to handle errors. It should, however, catch invalid parameters without failing.
@@ -117,6 +120,25 @@ export interface IProtocol {
sendScreenUpdate(rect: ScreenRect): void;
}
// base mixin for all protocols to use
export class ProtocolBase {
protected handlers: IProtocolHandlers | null = null;
protected user: User | null = null;
init(u: User): void {
this.user = u;
}
dispose(): void {
this.user = null;
this.handlers = null;
}
setHandler(handlers: IProtocolHandlers): void {
this.handlers = handlers;
}
}
// Holds protocol factories.
export class ProtocolManager {
private protocols = new Map<String, () => IProtocol>();