MicroEE: A Tiny EventEmitter Implementation

0.0.6 · abandoned · verified Tue Apr 21

MicroEE is an extremely lightweight, EventEmitter-like library designed for both client and server-side event routing. At approximately 50 lines of code and ~1200 characters, it was created to be significantly smaller than other event emitter implementations of its time. The package's current stable version is 0.0.6, which was published nearly a decade ago, indicating it is no longer actively maintained. Its primary differentiators were its minimal footprint and a simple API largely based on Node.js's native EventEmitter, with additions like `emitter.when()` for conditional listener removal and `microee.mixin()` for easily extending objects to become event emitters. It lacks modern features like `async` event handling or native ES module support.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use `microee.mixin` to make a class an event emitter, along with basic `on`, `emit`, `once`, and `when` methods.

const MicroEE = require('microee');

function MyClass() {
  // Constructor logic
}
MicroEE.mixin(MyClass);

MyClass.prototype.foo = function() {
  console.log('foo method called');
  this.emit('fooCalled', 'data from foo');
};

const obj = new MyClass();

// Add a listener for 'event'
obj.on('event', function(arg1, arg2) {
  console.log(`'event' fired with args: ${arg1}, ${arg2}`);
});

// Add a one-time listener for 'onceEvent'
obj.once('onceEvent', function() {
  console.log("'onceEvent' fired - this will only happen once.");
});

// Add a listener that removes itself conditionally
let counter = 0;
obj.when('conditionalEvent', function() {
  counter++;
  console.log(`'conditionalEvent' fired, counter: ${counter}`);
  return counter >= 2; // Remove listener after 2 fires
});

// Emit events
obj.emit('event', 'hello', 'world'); // Triggers 'event' listener
obj.emit('onceEvent');             // Triggers 'onceEvent' listener once
obj.emit('onceEvent');             // Does nothing now
obj.emit('conditionalEvent');      // Triggers 'conditionalEvent' (counter 1)
obj.emit('conditionalEvent');      // Triggers 'conditionalEvent' (counter 2) and removes itself
obj.emit('conditionalEvent');      // Does nothing now

obj.foo(); // Calls foo, which emits 'fooCalled'
obj.on('fooCalled', (data) => console.log(`'fooCalled' received: ${data}`));
obj.foo();

view raw JSON →