You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1 KiB

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<void>
*/
public async up(): Promise<void> {};
/**
* Method called to stop the unit when the application is stopping.
* @return Promise<void>
*/
public async down(): Promise<void> {};
/**
* Returns a collection of lifecycle units that this lifecycle unit depends on.
* @return Collection<typeof LifecycleUnit>
*/
public static get_dependencies(): Collection<typeof LifecycleUnit> {
if ( isInstantiable(this) ) {
const deps = new Collection<typeof LifecycleUnit>()
for ( const dependency of container.get_dependencies(this) ) {
if ( isLifecycleUnit(dependency) )
deps.push(dependency)
}
return deps
}
return new Collection<typeof LifecycleUnit>()
}
}