Skip to content

Instantly share code, notes, and snippets.

@usirin
Created January 14, 2023 23:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usirin/e53987e26f7ac41760e21c0891865061 to your computer and use it in GitHub Desktop.
Save usirin/e53987e26f7ac41760e21c0891865061 to your computer and use it in GitHub Desktop.
typescript port of github.com/usirin/simple-kernel
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