Skip to content

Latest commit

 

History

History
33 lines (23 loc) · 731 Bytes

README.md

File metadata and controls

33 lines (23 loc) · 731 Bytes

Emitter

A simple event emitter class, similar to tiny-emitter and mitt.

npm install @tmbr/emitter
import Emitter from '@tmbr/emitter';

const emitter = new Emitter();

function callback(e) {
  alert(`Hello, ${e.name}!`);
}

// subscribe to an event
emitter.on('example', callback);

// dispatch an event with data
emitter.emit('example', {name: 'World'});

// unsubscribe from an event
emitter.off('example', callback);

// unsubscribe from an event with the returned cleanup function
const off = emitter.on('example', e => alert(`Hello, ${e.name}!`));
off();

// destroy and remove all subscriptions
emitter.destroy();