@dekkai/event-emitter
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@dekkai/event-emitter
browser node deno opinion

@dekkai/event-emitter

An event emitter class, supports omni-listeners and symbol events. Tested on browsers, node.js and deno.

Check out the full API Documentation

Installation

Browser/NodeJS

$ yarn add @dekkai/event-emitter

Deno

// import from directly from a CDN, like unpkg.com
import {EventEmitter} from 'https://unpkg.com/@dekkai/event-emitter';

Usage

// import EventEmitter
import {EventEmitter} from '@dekkai/event-emitter';

// can be extended
class MyEmitter extends EventEmitter {
    // ...
}

// used as a mixin
const MyEmitter = EventEmitter.mixin(BaseClass);

// or injected in the inheritance chain
class MyEmitter extends EventEmitter.mixin(BaseClass) {
    // ...
}

// instantiate the emitter
const emitter = new MyEmitter();

// create an event handler
const handler = (evt, num0, num1) => console.log(`Event [${evt.toString()}] result: ${num0 + num1}`);

// register the handler
emitter.on('add', handler);

// events can also be symbols
const symbolEvent = Symbol('add-symbol');
emitter.on(symbolEvent, handler);

// emit events
emitter.emit('add', 4, 6); // Event [add] result: 10
emitter.emit(symbolEvent, 12, 8); // Event [Symbol(add-symbol)] result: 20

// remove previously added listeners
emitter.off('add', handler);
emitter.off(symbolEvent, handler);

// use omni-listeners to catch all events, register them using the `omniEvent` static property
const omniHandler = evt => console.log(evt.toString());
emitter.on(MyEmitter.omniEvent, omniHandler);

// the omni-listener will catch all events
emitter.emit('hello'); // hello
emitter.emit(Symbol('random symbol')); // Symbol(random symbol)

// removing omni-listeners works the same way as with regular listeners, but using the `omniEvent`
emitter.off(MyEmitter.omniEvent, omniHandler);

Check out the full API Documentation

Package Sidebar

Install

npm i @dekkai/event-emitter

Weekly Downloads

167

Version

1.1.0

License

MIT

Unpacked Size

33.8 kB

Total Files

18

Last publish

Collaborators

  • darionco