import { Status } from '../const/status.ts' import { Collection } from '../collection/Collection.ts' import {container} from '../../../di/src/global.ts' import {isInstantiable} from '../../../di/src/type/Instantiable.ts' import AppClass from './AppClass.ts' /** * Returns true if the given item is a lifecycle unit. * @param something * @return boolean */ const isLifecycleUnit = (something: any): something is (typeof LifecycleUnit) => { return isInstantiable(something) && something.prototype instanceof LifecycleUnit } /** * Base class representing a single unit of the application lifecycle, responsible * for booting and stopping some piece of the application. * @extends AppClass */ export default abstract class LifecycleUnit extends AppClass { /** * The current status of the unit. * @type Status */ private _status = Status.Stopped /** * Get the current status of the unit. * @type Status */ public get status() { return this._status } /** * Set the current status of the unit. * @param {Status} status */ public set status(status) { this._status = status } /** * Method called to boot and start the unit when the application is starting. * @return Promise */ public async up(): Promise {}; /** * Method called to stop the unit when the application is stopping. * @return Promise */ public async down(): Promise {}; /** * Returns a collection of lifecycle units that this lifecycle unit depends on. * @return Collection */ public static get_dependencies(): Collection { if ( isInstantiable(this) ) { const deps = new Collection() for ( const dependency of container.get_dependencies(this) ) { if ( isLifecycleUnit(dependency) ) deps.push(dependency) } return deps } return new Collection() } }