Skip to content

Async Application Kernel written in TypeScript

License

Notifications You must be signed in to change notification settings

hisorange/kernel

Repository files navigation

Async Application Kernel

Version Build NPM GitHub Last Update License Coverage Status

Just my personal take on an async application kernel. It has injection management, simple module system, and a few other things.

Notable features are:

  • Dependency injection
  • Module with lifecycle hooks and dependency order
  • Scheduled tasks with CRON and injections
  • Built in event bus and respective decorators

Getting Started

yarn add @hisorange/kernel

Usage

const kernel = new Kernel();
kernel.register([MyModule, SecondModule]);

await kernel.boostrap();
await kernel.start();

process.on(
  'SIGINT',
  kernel.stop().then(() => process.exit(0)),
);

Module

@Module({
  providers: [],
  imports: [ConfigModule],
  dependsOn: [DatabaseModule],
})
export class MyModule implements IModule {
  public async onBoot() {
    // Executed in dependency order, you can setup your module here.
    // And the dependencies are already booted.
  }

  public async onStart() {
    // Runs after every module is booted.
    // Can do any async tasks here.
  }

  public async onStop() {
    // Called when a stop signal is received.
  }
}

Scheduler

@Scheduler()
export class MyScheduler {
  constructor(private readonly myService: MyService) {}

  @Job({
    name: 'my-named-job',
    timings: '*/5 * * * * *',
  })
  public async myJob() {
    this.myService.doSomething();
  }
}

Event Handler

@Observer()
export class MyObserver {
  constructor(private readonly myService: MyService) {}

  @On('sql.query', {
    debounce: 1_000,
  })
  public async onSqlQUery() {
    this.myService.doSomething();
  }
}

Once I gotta write a proper readme, but for now, this is it.