async plugins fixed

This commit is contained in:
andrei-mihnea-cerbu
2024-11-12 18:05:50 +02:00
parent 4fb58b9727
commit 44df437425
22 changed files with 1690 additions and 2262 deletions
@@ -1,40 +0,0 @@
import { ParsedMessage } from '../message_handler';
import { OperationHandler } from './operation_handler';
import { OperationPlugin } from './operation_plugin';
export abstract class OperationBase implements OperationPlugin {
// Shared operation codes
public static readonly operationCodes = {
OK: 'OK',
ERR: 'ERR',
END: 'END',
};
// Default handler for OK operation
public static handleOk(parsedMessage: ParsedMessage): ParsedMessage {
return parsedMessage; // Typically, you would just acknowledge with OK, returning as-is
}
// Default handler for ERR operation
public static handleErr(parsedMessage: ParsedMessage): ParsedMessage {
return parsedMessage; // Typically, you would log the error and return
}
// Default handler for END operation
public static handleEnd(parsedMessage: ParsedMessage): ParsedMessage {
return {
operationCode: OperationBase.operationCodes.END,
metaInfo: { message: 'Connection ended.' },
};
}
// Register the common OK, ERR, and END handlers
public static registerCommonOperations(operationHandler: OperationHandler): void {
operationHandler.registerHandler(OperationBase.operationCodes.OK, OperationBase.handleOk);
operationHandler.registerHandler(OperationBase.operationCodes.ERR, OperationBase.handleErr);
operationHandler.registerHandler(OperationBase.operationCodes.END, OperationBase.handleEnd);
}
// Abstract register method that will be implemented by subclasses
public abstract register(operationHandler: OperationHandler): void;
}
@@ -2,14 +2,14 @@
import { ParsedMessage, MessageHandler } from '../message_handler';
import { OperationPlugin } from './operation_plugin';
type OperationHandlerFunction = (parsedMessage: ParsedMessage) => ParsedMessage;
// Define handler function type to return Promise<ParsedMessage>
type OperationHandlerFunction = (parsedMessage: ParsedMessage) => Promise<ParsedMessage>;
export class OperationHandler {
private static instance: OperationHandler;
private handlers: { [operationCode: string]: OperationHandlerFunction } = {};
private constructor() {
// Register only the unknown command handler on initialization
this.registerHandler('UNKNOWN_COMMAND', this.handleUnknownCommand);
}
@@ -26,25 +26,24 @@ export class OperationHandler {
this.handlers[operationCode] = handler;
}
// Handle operation request
public handleOperation(rawMessage: string): ParsedMessage {
// Parse and validate the message
// Handle operation request asynchronously
public async handleOperation(rawMessage: string): Promise<ParsedMessage> {
const parsedMessage = MessageHandler.parseMessage(rawMessage);
if (!parsedMessage || !MessageHandler.validateMessage(parsedMessage)) {
return this.handleUnknownCommand(parsedMessage);
}
// Dispatch the handler for the given operation code
// Retrieve the handler for the operation code and invoke it asynchronously
const handler = this.handlers[parsedMessage.operationCode];
if (handler) {
return handler(parsedMessage);
return await handler(parsedMessage);
} else {
return this.handleUnknownCommand(parsedMessage);
}
}
// Default handler for unknown commands
private handleUnknownCommand(parsedMessage: ParsedMessage): ParsedMessage {
private async handleUnknownCommand(parsedMessage: ParsedMessage): Promise<ParsedMessage> {
return {
operationCode: 'UNKNOWN_COMMAND',
metaInfo: { message: 'Unknown command received.' },