# @yume-chan/event

> Event/EventEmitter

Latest version **2.0.0** (published 2025-04-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @yume-chan/event
pnpm add @yume-chan/event
yarn add @yume-chan/event
bun add @yume-chan/event
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-04-04 |
| First published | 2021-01-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 41.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 3251 |
| Author | Simon Chan |
| Maintainers | yume-chan |
| Keywords | event, eventemitter, typescript |

## Links

- npm: https://www.npmjs.com/package/@yume-chan/event
- Repository: https://github.com/yume-chan/ya-webadb
- Homepage: https://github.com/yume-chan/ya-webadb/tree/main/libraries/event#readme
- Issues: https://github.com/yume-chan/ya-webadb/issues
- npm.io page: https://npm.io/package/@yume-chan/event

## Dependencies (1)

- [@yume-chan/async](https://npm.io/package/@yume-chan/async.md) ^4.0.2

## 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

- 2.0.0 (latest) — 2025-04-04
- 3.0.0-beta.4 (beta) — 2026-10-09
- 3.0.0-beta.3 — 2026-09-17
- 3.0.0-beta.2 — 2026-08-11
- 3.0.0-beta.1 — 2026-08-01
- 3.0.0-beta.0 — 2026-07-28
- 1.0.0 — 2024-12-02
- 0.0.0-next-20241130050937 — 2024-11-30
- 0.0.0-next-20241129144018 — 2024-11-29
- 0.0.0-next-20240917062356 — 2024-09-17
- 0.0.0-20240714132542 — 2024-07-14
- 0.0.24 — 2024-06-18
- 0.0.23 — 2024-03-21
- 0.0.22 — 2023-12-13
- 0.0.21 — 2023-08-25
- … 14 more at https://npm.io/package/@yume-chan/event/versions

## README

# @yume-chan/event

Provides a strongly-typed EventEmitter/Event implementation.

Inspired by TypeScript, Visual Studio Code, and more.

- [Disposable](#disposable)
- [AutoDisposable](#autodisposable)
- [DisposableList](#disposablelist)
- [EventEmitter/Event](#eventemitterevent)

## Disposable

```ts
interface Disposable {
    dispose(): void;
}
```

Represents anything that need cleanup before the garbage collector recycle it.

## AutoDisposable

```ts
class AutoDisposable implements Disposable {
    private disposables;
    constructor();
    protected addDisposable<T extends Disposable>(disposable: T): T;
    dispose(): void;
}
```

A base class for objects that need to manage multiple `Disposable`s.

Calling `dispose` on it will automatically dispose all `Disposable`s added via `addDisposable`.

Usually works like:

```ts
class MyObject extends AutoDisposable {
    private event1 = this.addDisposable(new EventEmitter<void>());

    private event2 = this.addDisposable(new EventEmitter<void>());

    public dispose() {
        // If the derived class has its own dispose logic
        // Don't forget to call super's `dispose`
        super.dispose();

        // Clean up itself.
    }
}
```

## DisposableList

```ts
class DisposableList extends AutoDisposable {
    add<T extends Disposable>(disposable: T): T;
}
```

An `AutoDisposable` that can be used alone (i.e. not as a base class).

## EventEmitter/Event

```ts
interface EventListener<TEvent, TThis, TArgs extends unknown[], TResult> {
    (this: TThis, e: TEvent, ...args: TArgs): TResult;
}

interface RemoveEventListener extends Disposable {
    (): void;
}

interface Event<TEvent, TResult = unknown> {
    (listener: EventListener<TEvent, unknown, [], TResult>): RemoveEventListener;
    <TThis, TArgs extends unknown[]>(listener: EventListener<TEvent, TThis, TArgs, TResult>, thisArg: TThis, ...args: TArgs): RemoveEventListener;
}

class EventEmitter<TEvent, TResult = unknown> implements Disposable {
    protected readonly listeners: EventListenerInfo<TEvent, TResult>[];
    constructor();
    protected addEventListener(info: EventListenerInfo<TEvent, TResult>): RemoveEventListener;
    event: Event<TEvent, TResult>;
    fire(e: TEvent): void;
    dispose(): void;
}

```

|                                     | Node.js `EventEmitter` | This `EventEmitter` |
| ----------------------------------- | ---------------------- | ------------------- |
| Can emit multiple event types       | Yes                    | No                  |
| Only trusted source can emit events | No                     | Yes                 |
| Strongly-typed                      | No                     | Yes                 |

One `EventEmitter` for one event type. So for classes that have multiple event types, multiple `EventEmitter` can be created and assigned to multiple fields.

Usually classes keep `EventEmitter`s private (using TypeScript's `private` modifier, or ECMAScript private field), only expose the `Event` interface (via `EventEmitter.event`).

Everyone can subscribe to the event using the `Event` interface, but the event can only be emitted from the `EventEmitter` class.

```ts
const emitter = new EventEmitter<void>();
const subscribe = emitter.event;
const unsubscribe = subscribe(() => {});
emitter.fire();
unsubscribe();
```

The returned `unsubscribe` is both a function and a `Disposable`, so it can be used with `AutoDisposable` or `DisposableList`.

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