3.0.3 • Published 4 years ago

@soncodi/ee v3.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

EE (Event Emitter)

Build Status Coverage Status Dependency Status npm version

Tiny, typed event emitter utility for Node.js and browsers. No dependencies.

Need a single event and type, only? Check out Signal instead

Installation

npm install @soncodi/ee --save

Usage (TypeScript)

import { EE } from '@soncodi/ee';

// map event names to their callback param types
interface Events {
  a: number;
  b: string;
  c: undefined;
}

const ee = new EE<Events>();

const cb = (num: number) => console.log('A', num);

ee.on('a', cb);
ee.on('b', str => console.log('B', str));
ee.once('c', () => console.log('C'));

ee.emit('a', 123);
ee.emit('b', 'hello');
ee.emit('c');

ee.off('a', cb);

ee.event('b', 123); // error
ee.event('d', 123); // error

Methods

on(event, fn)

Attaches an event handler to be called whenever the event fires.

once(event, fn)

Attaches a one-time handler which is unbound after it fires the first time.

off(event, fn?)

Detaches one instance of a given handler from the event emitter. If no handler is provided, detaches all handlers.

emit(event, arg)

Fires the event synchronously, triggering any attached handlers with the given arg.

event(event, arg)

Fires the event asynchronously, triggering any attached handlers with the given arg. Useful when attaching handlers later in the same event loop turn.

3.0.3

4 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.1

6 years ago