@ima/core
Version:
IMA.js framework for isomorphic javascript application
51 lines (50 loc) • 1.96 kB
JavaScript
import { PageHandler } from './PageHandler';
import { GenericError } from '../../error/GenericError';
import { SerialBatch } from '../../execution/SerialBatch';
export class PageHandlerRegistry extends PageHandler {
_pageHandlers;
_preManageHandlers;
_postManageHandlers;
static ExecutionMethod = SerialBatch;
/**
* Creates an instance of HandlerRegistry and creates `SerialBatch`
* instance for pre-handlers and post-handlers.
* @memberof HandlerRegistry
*/ constructor(...pageHandlers){
super();
this._pageHandlers = pageHandlers;
}
/**
* @inheritDoc
*/ init() {
this._pageHandlers.forEach((handler)=>handler.init());
this._preManageHandlers = Reflect.construct(PageHandlerRegistry.ExecutionMethod, [
this._pageHandlers.map((handler)=>handler.handlePreManagedState.bind(handler))
]);
this._postManageHandlers = Reflect.construct(PageHandlerRegistry.ExecutionMethod, [
this._pageHandlers.map((handler)=>handler.handlePostManagedState.bind(handler))
]);
}
/**
* Executes the pre-manage handlers with given arguments
*/ handlePreManagedState(managedPage, nextManagedPage, action) {
if (!this._preManageHandlers) {
throw new GenericError('You must call init first.');
}
return this._preManageHandlers.execute(managedPage, nextManagedPage, action);
}
/**
* Executes the post-manage handlers with given arguments
*/ handlePostManagedState(managedPage, previousManagedPage, action) {
return this?._postManageHandlers?.execute(managedPage, previousManagedPage, action);
}
/**
* @inheritDoc
*/ destroy() {
this._pageHandlers.forEach((handler)=>handler.destroy());
this._preManageHandlers = undefined;
this._postManageHandlers = undefined;
this._pageHandlers = [];
}
}
//# sourceMappingURL=PageHandlerRegistry.js.map