Skip to content

Function: OnEvent()

ts
function OnEvent(types?: 
  | EventType
  | readonly EventType[]): MethodDecorator;

Defined in: src/wirestate-core/events/on-event.ts:35

Marks a service method as an event handler.

Parameters

ParameterTypeDescription
types?| EventType | readonly EventType[]Event token or tokens. Omit for all events.

Returns

MethodDecorator

Method decorator.

Remarks

The handler registers when the service activates and unregisters when the service deactivates.

Omit types to receive every event in the container.

Example

typescript
import { Event, Injectable, OnEvent } from "@wirestate/core";

@Injectable()
class MyService {
  @OnEvent("USER_LOGGED_IN")
  private onLogin(event: Event<User>): void {
    console.log(event.payload.id);
  }
}