Skip to content

Function: OnEvent()

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

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

Marks an injectable service method as a provision-scoped event handler.

Parameters

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

Returns

OnEventDecorator

Method decorator.

Remarks

The handler is registered when the owning container is provisioned and unregistered when that provision cycle ends. Register EventsPlugin on the container, or on an ancestor container, to enable event handlers.

Omit types to receive every event emitted on the active event bus. Repeated types are deduplicated for one decorated method.

Example

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

interface User {
  id: string;
}

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