@eggjs/eventbus-decorator
TypeScript icon, indicating that this package has built-in type declarations

3.39.0 • Public • Published

@eggjs/eventbus-decorator

Usage

emit event

import { EventBus } from '@eggjs/eventbus-decorator'

// Define event first.
// Ts can check event and args type for you.
declare module '@eggjs/eventbus-decorator' {
  interface Events {
    hello: (msg: string) => Promise<void>;
  }
}

class Foo {
  @Inject()
  private readonly eventBus: EventBus;

  bar() {
    this.eventBus.emit('hello', '01');
  }
}

cork events

Cache events in memory until uncork.

class Foo {
  @Inject()
  private readonly eventBus: ContextEventBus;

  bar() {
    this.eventBus.cork();
    // ...do something
    this.eventBus.emit('hello', '01');
    // ...do other things
    
    // emit all cached events
    this.eventBus.uncork();
  }
}

handle event

@Event('hello')
export class Foo {
  async handle(msg: string): Promise<void> {
    console.log('msg: ', msg);
  }
}

handle multiple event

@Event('hello')
@Event('hi')
export class Foo {
  async handle(msg: string): Promise<void> {
    console.log('msg: ', msg);
  }
}

inject event context

inject event context if you want to know which event is being handled. The context param must be the first param

@Event('hello')
@Event('hi')
export class Foo {
  async handle(@EventContext() ctx: IEventContext, msg: string):Promise<void> {
    console.log('eventName: ', ctx.eventName);
    console.log('msg: ', msg);
  }
}

Package Sidebar

Install

npm i @eggjs/eventbus-decorator

Weekly Downloads

625

Version

3.39.0

License

MIT

Unpacked Size

15.4 kB

Total Files

13

Last publish

Collaborators

  • gxkl
  • gemwuu
  • eggjs-admin
  • fengmk2
  • atian25
  • dead_horse
  • wanghx
  • hyj1991
  • killagu
  • coolme200
  • mansonchor.zzw
  • hubcarl