import * as Bluebird from 'bluebird'; import AbstractController from '../controllers/abstract-controller'; import AbstractAdapter, { IAbstractAdapter } from './abstract-adapter'; /** * IListenableAdapter * @abstract class */ export abstract class IListenableAdapter extends IAbstractAdapter { // HACK: 모든 abstract adapter의 initialize가 호출 된 다음에 호출된다 abstract postInitialize(): any | Promise; abstract listen(): any | Promise; } /** * Abstract adapter class for back-end service. * @abstract * @class * @extends AbstractAdapter * @implements IListenableAdapter */ export default abstract class ListenableAdapter extends AbstractAdapter implements IListenableAdapter { private _controllersClasses: {new(...args: any[]): AbstractController}[] = []; private _controllers: AbstractController[] = []; /** * @param {AbstractController} Class */ public registerController(Class: {new(...args: any[]): AbstractController}) { this._controllersClasses.push(Class); } /** * @returns {Promise} * @final */ public postInitialize(): Promise { return Promise.all(this._controllersClasses.map(ControllerClass => { const c = new ControllerClass(this._adaptee); this._controllers.push(c); return Bluebird.try(() => c.initialize()).then(() => c.onInitialized()); })); } public async destroy(): Promise { await Promise.all(this._controllers.map(c => Bluebird.try(() => c.destroy()))); await Promise.all(this._controllers.map(c => Bluebird.try(() => c.onDestroy()))); this._controllersClasses = []; this._controllers = []; } /** * @abstract * @returns {Promise} */ abstract listen(): any | Promise; }