# @garzj/event-subscriber

> An event proxy allowing for easy removal of subscriptions on the EventEmitter class

Latest version **2.1.1** (published 2026-02-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install @garzj/event-subscriber
pnpm add @garzj/event-subscriber
yarn add @garzj/event-subscriber
bun add @garzj/event-subscriber
```

## Health

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

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

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.1.1 |
| Published | 2026-02-02 |
| First published | 2023-06-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Author | Johannes Garz |
| Maintainers | garzj |
| Keywords | event, events, emitter, subscriber |

## Links

- npm: https://www.npmjs.com/package/@garzj/event-subscriber
- Repository: https://github.com/garzj/node-event-subscriber
- Homepage: https://github.com/garzj/node-event-subscriber#readme
- Issues: https://github.com/garzj/node-event-subscriber/issues
- npm.io page: https://npm.io/package/@garzj/event-subscriber

## 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.1.1 (latest) — 2026-02-02
- 2.1.0 — 2025-01-04
- 2.0.0 — 2024-10-05
- 1.1.1 — 2024-09-18
- 1.1.0 — 2024-09-18
- 1.0.4 — 2024-05-10
- 1.0.3 — 2023-08-25
- 1.0.2 — 2023-06-29
- 1.0.0 — 2023-06-29

## README

# @garzj/event-subscriber

Proxies subscriptions to an EventEmitter and stores references, allowing for an easy removal.

## Features

- TypeScript support
- Compatible with
  - [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
  - [EventEmitter](https://nodejs.org/en/learn/asynchronous-work/the-nodejs-event-emitter)
  - [typed-emitter](https://github.com/andywer/typed-emitter)
  - [tiny-typed-emitter](https://github.com/binier/tiny-typed-emitter)
  - more...

## Heads up

Due to a TypeScript limitation (there does not seem to be a reliable way to remap return types of function overloads), methods of the `EventSubscriber` class may look like they return the original event emitter (they do not, the subscriber is returned).

It's probably better to avoid chaines like `.on(a, b).on(c, d)` and prefer `sub.on(a, b); sub.on(c, d);` instead.

## Installation

```bash
npm i @garzj/event-subscriber
```

or with yarn

```bash
yarn add @garzj/event-subscriber
```

## Usage

### Subscribe to events

```ts
import { EventEmitter } from 'events';
import { EventSubscriber } from '@garzj/event-subscriber';

const myEmitter = new EventEmitter();

myEmitter.on('my-event', () => console.log('Always called!'));
myEmitter.emit('my-event');

const sub = new EventSubscriber(myEmitter);
sub.on('my-event', () => console.log('Temporarily called!'));
myEmitter.emit('my-event');
sub.off();

myEmitter.emit('my-event');
```

#### Output

```
Always called!
Always called!
Temporarily called!
Always called!
```

### Forward type declaration

```ts
import { EventSubscriber } from '@garzj/event-subscriber';

let subscriber: EventSubscriber<typeof process>;

subscriber = new EventSubscriber(process);
subscriber.on('exit', () => console.log('process exit'));
subscriber.off();
```

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