# @eggjs/eventbus-decorator

> tegg eventbus decorator

Latest version **4.1.0** (published 2026-10-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @eggjs/eventbus-decorator
pnpm add @eggjs/eventbus-decorator
yarn add @eggjs/eventbus-decorator
bun add @eggjs/eventbus-decorator
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; popular repo.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2026-10-07 |
| First published | 2021-07-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22.18.0 |
| Dependencies | 3 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 18985 |
| Author | killagu |
| Maintainers | gxkl, eggjs-admin, fengmk2, atian25, hyj1991, killagu, coolme200, akitasummer, elrrrrrrr, niania |
| Keywords | egg, eventbus, eventemitter, tegg, typescript |

## Links

- npm: https://www.npmjs.com/package/@eggjs/eventbus-decorator
- Repository: https://github.com/eggjs/egg
- Homepage: https://github.com/eggjs/egg/tree/next/tegg/core/eventbus-decorator
- Issues: https://github.com/eggjs/egg/issues
- npm.io page: https://npm.io/package/@eggjs/eventbus-decorator

## Dependencies (3)

- [@eggjs/tegg-types](https://npm.io/package/@eggjs/tegg-types.md) 4.1.0
- [@eggjs/core-decorator](https://npm.io/package/@eggjs/core-decorator.md) 4.1.0
- [@eggjs/tegg-common-util](https://npm.io/package/@eggjs/tegg-common-util.md) 4.1.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 4.1.0 (latest) — 2026-10-07
- 4.0.2-rc.1 (rc) — 2026-10-06
- 4.0.2-beta.27 (beta) — 2026-09-20
- 3.74.0-beta.3 (beta-v3) — 2026-03-29
- 3.57.15-alpha.0 (alpha) — 2025-06-27
- 4.0.2 — 2026-10-06
- 4.0.2-rc.0 — 2026-09-23
- 3.88.1 — 2026-09-08
- 3.87.0 — 2026-08-13
- 4.0.2-beta.26 — 2026-08-07
- 3.86.0 — 2026-08-05
- 3.85.2 — 2026-07-31
- 3.85.1 — 2026-07-29
- 3.85.0 — 2026-07-22
- 4.0.2-beta.25 — 2026-07-13
- … 240 more at https://npm.io/package/@eggjs/eventbus-decorator/versions

## README

# `@eggjs/eventbus-decorator`

[![NPM version][npm-image]][npm-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/@eggjs/eventbus-decorator.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/@eggjs/eventbus-decorator.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/eventbus-decorator
[snyk-image]: https://snyk.io/test/npm/@eggjs/eventbus-decorator/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/@eggjs/eventbus-decorator
[download-image]: https://img.shields.io/npm/dm/@eggjs/eventbus-decorator.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/eventbus-decorator

## Usage

### emit event

```ts
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.

```ts
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

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

### handle multiple event

```ts
@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

```ts
@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);
  }
}
```

---
_Source: https://npm.io/package/@eggjs/eventbus-decorator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
