2.0.0 • Published 9 months ago

@garzj/event-subscriber v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@garzj/event-subscriber

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

Features

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

npm i @garzj/event-subscriber

Usage

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!
2.0.0

9 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.4

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago