@synet/patterns
Version:
Robust, battle-tested collection of stable patterns used in Synet packages
35 lines (34 loc) • 1.1 kB
TypeScript
/**
* Represents a request to be handled by the mediator
*
* Docymentation:
* @see /docs/mediator.md for detailed documentation and examples
*
* Examples:
* @see /examples/mediator.ts for usage examples
*/
export interface IRequest<TResponse = void> {
type: string;
}
/**
* Interface for a handler that processes a specific request type
*/
export interface IRequestHandler<TRequest extends IRequest<TResponse>, TResponse> {
handle(request: TRequest): Promise<TResponse> | TResponse;
}
/**
* Core mediator implementation
* Dispatches requests to appropriate handlers
*/
export declare class Mediator {
private handlers;
/**
* Register a handler for a specific request type
*/
registerHandler<TRequest extends IRequest<TResponse>, TResponse>(requestType: string, handler: IRequestHandler<TRequest, TResponse>): void;
/**
* Send a request to be handled by its registered handler
* @returns Promise resolving to the handler's response
*/
send<TRequest extends IRequest<TResponse>, TResponse>(request: TRequest): Promise<TResponse>;
}