@mvcs/core
Version:
MV* common objects and interfaces required across the framework
89 lines (88 loc) • 3.56 kB
TypeScript
/**
* Listens for view events and reacts in them.
*/
export interface Mediator<V = any> {
/** View mediated by this instance. */
readonly view: V;
/**
* Set view to mediate by this instance.
* @param view - view to mediate.
*/
setView(view: V): void;
/**
* Initializes the mediator.
* This is run automatically when a mediator is created.
* Normally the initialize function is where you would add handlers.
*/
initialize?(): void;
/**
* Destroys the mediator.
* This is run automatically when a mediator is destroyed.
* You should clean up any handlers that were added directly.
*/
destroy?(): void;
}
export declare namespace Mediator {
/**
* Initialize new instance of the mediator.
* @see MediatorLifecycle.preInitialize
* @see MediatorLifecycle.setView
* @see MediatorLifecycle.initialize
* @see MediatorLifecycle.postInitialize
*/
function initialize<M extends MediatorLifecycle<V>, V>(mediator: M, view: V, errorHandler?: MediatorErrorHandlerObject): M;
/**
* Function to pass as iterative callback in order to destroy given mediator.
* @param mediator - mediator to destroy.
* @see Mediator#destroy
* @see MediatorLifecycle#postDestroy
*/
function destroyer(this: MediatorErrorHandlerObject, mediator: MediatorLifecycle): void;
/**
* Invoke hook on the given mediator.
* @param mediator - mediator that may optionally implement a hook.
* @param key - name of the hook to invoke.
* @param errorHandler - handler to use for catching errors.
* @param params - arguments to pass in a hook if any.
*/
function hook<//
M extends Mediator & Partial<Record<K, F>>, K extends keyof M, F extends (...params: P) => any, P extends any[]>(mediator: M, key: K, errorHandler?: MediatorErrorHandlerObject, ...params: P): void;
/**
* Invoke hook on every mediator.
* @param mediators - list of mediators that may optionally implement a hook.
* @param key - name of the hook to invoke.
* @param errorHandler - handler to use for catching errors.
* @param args - arguments to pass in a hook if any.
*/
function hookEach<//
M extends Mediator & Partial<Record<K, F>>, K extends keyof M, F extends (...args: any[]) => any>(mediators: M[], key: K, errorHandler?: MediatorErrorHandlerObject, ...args: Parameters<M[K]>): void;
}
export interface MediatorErrorHandlerObject {
/**
* Function to invoke for catching errors while invoking mediator hooks.
* @param error - error occurred while invoking mediator hook.
* @param hook - name of the hook throwing an error.
* @param mediator - mediator instance throwing an error.
*/
catchMediatorHookError(error: Error, hook: keyof any, mediator: Mediator): void;
}
/**
* Extra lifecycle methods of {@link Mediator} from internal use.
*/
export interface MediatorLifecycle<V = any> extends Mediator<V> {
/**
* Initializes the mediator, occurs just before {@link Mediator#initialize}.
* This is run automatically when a mediator is created.
*/
preInitialize?(): void;
/**
* Initializes the mediator, occurs just after {@link Mediator#initialize}.
* This is run automatically when a mediator is created.
*/
postInitialize?(): void;
/**
* Destroys the mediator, occurs just after {@link Mediator#destroy}.
* This is run automatically when a mediator is destroyed.
*/
postDestroy?(): void;
}