Skip to main content
zerotal

Documentation


Documentation / @zerotal/flow / index / on

Function: on()

on(eventName): (fn, context) => void

Defined in: flow/src/decorators.ts:775

Binds a method as a cross-component (realtime) event listener. Auto-exposes the method.

Parameters

eventName

string & object

The event to listen for; a known FlowEvents key or any string.

Returns

A method decorator that registers the listener.

(fn, context) => void

Remarks

When the named event is dispatched (via dispatch / dispatchTo / dispatchSelf, or an echo:… broadcast), the decorated method is invoked with the event payload. The event name autocompletes to the known FlowEvents contract while still accepting any string (for echo:… broadcasts and gradually-typed events). Annotate the handler's parameter with EventPayload<"name"> to type the payload against the same contract. Because it auto-exposes, no separate @expose is needed. Applies to a method.

Example

class Feed extends Component {
  @expose posts: Post[] = [];

  @on("post-created")
  onPostCreated(payload: EventPayload<"post-created">) {
    this.posts = [payload, ...this.posts];
  }
}