New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@eggjs/eventbus-decorator

Package Overview
Dependencies
Maintainers
10
Versions
255
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eggjs/eventbus-decorator

tegg eventbus decorator

Source
npmnpm
Version
4.0.2
Version published
Weekly downloads
790
-79.96%
Maintainers
10
Weekly downloads
 
Created
Source

@eggjs/eventbus-decorator

NPM version Known Vulnerabilities npm download Node.js Version

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);
  }
}

Keywords

egg

FAQs

Package last updated on 06 Oct 2026

Related posts