Created
January 14, 2023 23:50
-
-
Save usirin/e53987e26f7ac41760e21c0891865061 to your computer and use it in GitHub Desktop.
typescript port of github.com/usirin/simple-kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface Bootstrapper<T> { | |
bootstrap(context: T): Promise<any> | any; | |
} | |
export interface KernelOptions<T> { | |
bootstrappers: Array<Bootstrapper<T>>; | |
} | |
export function createKernel<T>( | |
options: KernelOptions<T>, | |
initialContext: Partial<T> = {}, | |
) { | |
const context: Partial<T> = { ...initialContext }; | |
return new Kernel<T>(options, context); | |
} | |
class Kernel<T> { | |
private readonly bootstrappers: Array<Bootstrapper<T>>; | |
private context: T; | |
constructor(options: KernelOptions<T>, context: Partial<T>) { | |
this.bootstrappers = options.bootstrappers; | |
this.context = context as T; | |
} | |
public async boot() { | |
const context = { ...this.context }; | |
for (const bootstrapper of this.bootstrappers) { | |
await bootstrapper.bootstrap(context as T); | |
} | |
this.context = context; | |
return this.context; | |
} | |
public getContext() { | |
return this.context; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment