Skip to content

Latest commit

 

History

History
39 lines (24 loc) · 867 Bytes

event.md

File metadata and controls

39 lines (24 loc) · 867 Bytes

Event

1 What Does an Event Do?

Event is the core concept of Nodejs, highly recommended to read Nodejs documentation first, and then use this api.

2 Usage

// define an event emitter

@Service()
class CustomEventEmitter extends EventEmitter {}

// define an event subscriber

@Subscriber()
class CustomEventSubscriber {

  private property = "a property";

  @On('newEvent', CustomEventEmitter)
  public onEventEmit() {
    // `this` is the subscriber not event emitter

    this.property === "a property" // => true
  }
}

const emitter = DependencyRegistry.get(CustomEventEmitter);

emitter.emit('newEvent');

When emitter emit an event, the subscriber listener will trigger automatically. IMPORTANT: the this in the listener is the subscriber itself.