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.
42 lines
1.3 KiB
42 lines
1.3 KiB
import { Status, isStatus } from '../const/status.ts'
|
|
import { Collection } from '../collection/Collection.ts'
|
|
import {container, make} from '../../../di/src/global.ts'
|
|
import {DependencyKey} from '../../../di/src/type/DependencyKey.ts'
|
|
import Instantiable, {isInstantiable} from '../../../di/src/type/Instantiable.ts'
|
|
|
|
const isLifecycleUnit = (something: any): something is (typeof LifecycleUnit) => {
|
|
return isInstantiable(something) && something.prototype instanceof LifecycleUnit
|
|
}
|
|
|
|
export default abstract class LifecycleUnit {
|
|
private _status = Status.Stopped
|
|
|
|
public get status() {
|
|
return this._status
|
|
}
|
|
|
|
public set status(status) {
|
|
this._status = status
|
|
}
|
|
|
|
public async up(): Promise<void> {};
|
|
public async down(): Promise<void> {};
|
|
|
|
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>()
|
|
}
|
|
|
|
protected make<T>(target: Instantiable<T>|DependencyKey, ...parameters: any[]) {
|
|
return make(target, ...parameters)
|
|
}
|
|
}
|