EventEmitter

5.2.9 · abandoned · verified Sun Apr 19

wolfy87-eventemitter is a JavaScript event emitter library, primarily designed for use in web browsers but also usable in Node.js environments. It aims to replicate the event-driven paradigm found in Node.js, focusing on a lightweight footprint and fast execution. The library underwent a significant API remapping in its fourth major rewrite to improve clarity and extensibility. While npm shows `5.2.9` as the latest version, the project's GitHub repository has had no activity since 2016, with `v4.2.11` being the last officially tagged release. This indicates the project is no longer actively maintained, making `v4.x` the last stable and documented version.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic event handling: registering, emitting, one-time listeners, and removing listeners.

import EventEmitter from 'wolfy87-eventemitter';

const emitter = new EventEmitter();

// Register a listener for the 'data' event
emitter.on('data', (payload: string) => {
  console.log('Received data:', payload);
});

// Register a 'once' listener for the 'init' event
emitter.once('init', () => {
  console.log('Application initialized.');
});

// Emit the 'init' event
emitter.emit('init');
// This will not log again as 'once' listeners are removed after first emission
emitter.emit('init');

// Emit the 'data' event with a payload
emitter.emit('data', 'Hello Event World!');

// Remove a specific listener
const anotherListener = (num: number) => console.log('Number received:', num);
emitter.on('number', anotherListener);
emitter.emit('number', 42);
emitter.off('number', anotherListener);
emitter.emit('number', 100); // This will not log

// Remove all listeners for a specific event
emitter.on('cleanup', () => console.log('Cleaning up 1'));
emitter.on('cleanup', () => console.log('Cleaning up 2'));
emitter.emit('cleanup');
emitter.removeAllListeners('cleanup');
emitter.emit('cleanup'); // No output

view raw JSON →