Documentation chapters
You're looking at legacy documentation.
New multi-language documentation available at https://docs.deepkit.io
Framework

Events

Event listeners are services that allow you to listen for and respond to dispatched events. You can dispatch events using event tokens via the EventDispatcher.

You can create your own events or use already defined one, for example from the @deepkit/framework package.

Event token

Event token is a way to specify an event: An event id and the actual event. For example a "user added" event has an id of "user-added" and an event of type "UserEvent".

import { EventToken, BaseEvent } from '@deepkit/event';

interface User {
    username: string;
}

class UserEvent extends BaseEvent {
    constructor(public user: User) {
        super();
    }
}

const UserAdded = new EventToken('user-added', UserEvent);
A more simple event token without a custom event class would look like that:
import { EventToken, DataEvent } from '@deepkit/event';

interface User {
    username: string;
}

const UserAdded = new EventToken('user-added', DataEvent<User>);

Dispatch event

An event token can be dispatched from services, controllers, or event listeners. Basically from everywhere where the dependency injection container is used. Let's use the TestCommand from the Getting Started chapter again.

import { cli, Command } from '@deepkit/app';
import { EventDispatcher } from '@deepkit/event';

@cli.controller('test')
export class TestCommand implements Command {
    constructor(protected eventDispatcher: EventDispatcher) {
    }

    async execute() {
        this.eventDispatcher.dispatch(UserAdded, new UserEvent({ username: 'Peter' }));
    }
}

Note: Controllers are handled and instantiated by the dependency injection container, like services, and have thus access to all other registered services (like the EventDispatcher). See the chapter Dependency injection for more details.

Event listener

The last part is now to listen and react on dispatched events. Create a new class, add one or multiple methods with the @eventDispatcher.listen decorator, and register it under listeners in your application or module.

Event listeners are handled and instantiated by the dependency injection container, like services and controllers, and have thus access to all other registered services. See the chapter Dependency injection for more details.

import { eventDispatcher } from '@deepkit/event';

class MyListener {
    @eventDispatcher.listen(UserAdded)
    onUserAdded(event: typeof UserAdded.event) {
        console.log('User added!', event.user.username);
    }
}

new App({
    listeners: [MyListener],
}).run();

The full example of an event in action based on the code above:

#!/usr/bin/env ts-node-script
import { App } from '@deepkit/app';
import { cli, Command } from '@deepkit/app';
import { EventToken, BaseEvent, EventDispatcher, eventDispatcher } from '@deepkit/event';

@cli.controller('test')
export class TestCommand implements Command {
    constructor(protected eventDispatcher: EventDispatcher) {
    }

    async execute() {
        this.eventDispatcher.dispatch(UserAdded, new UserEvent({ username: 'Peter' }));
    }
}


interface User {
    username: string;
}

class UserEvent extends BaseEvent {
    constructor(public user: User) {
        super();
    }
}

const UserAdded = new EventToken('user-added', UserEvent);

class MyListener {
    @eventDispatcher.listen(UserAdded)
    onUserAdded(event: typeof UserAdded.event) {
        console.log('User added!', event.user.username);
    }
}

new App({
    controllers: [TestCommand],
    listeners: [MyListener],
}).run();

Framework events

Deepkit Framework itself has several events from the application server you can listen to.

import { App } from '@deepkit/app';
import { onServerMainBootstrap } from '@deepkit/framework';
import { eventDispatcher } from '@deepkit/event';

class MyListener {
    @eventDispatcher.listen(onServerMainBootstrap)
    onUserAdded(event: typeof onServerMainBootstrap.event) {
        console.log('Server bootstrapped!');
    }
}

new App({
    listeners: [MyListener],
}).run();
NameDescription
onServerBootstrapCalled only once for application server bootstrap (for main process and workers).
onServerBootstrapDoneCalled only once for application server bootstrap (for main process and workers) as soon as the application server has started.
onServerMainBootstrapCalled only once for application server bootstrap (in the main process).
onServerMainBootstrapDoneCalled only once for application server bootstrap (in the main process) as soon as the application server has started
onServerWorkerBootstrapCalled only once for application server bootstrap (in the worker process).
onServerWorkerBootstrapDoneCalled only once for application server bootstrap (in the worker process) as soon as the application server has started.
ServerShutdownEventCalled when application server shuts down (in master process and each worker).
onServerMainShutdownCalled when application server shuts down in the main process.
onServerWorkerShutdownCalled when application server shuts down in the worker process.
Made in Germany