MicroEE: A Tiny EventEmitter Implementation
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
-
ReferenceError: MicroEE is not defined
cause Attempting to use `MicroEE` without correctly importing/requiring it in a CommonJS or browser environment, or trying to use `import` syntax.fixEnsure you are using `const MicroEE = require('microee');` in Node.js or confirming the `microee` global is available in your browser context. If using ES modules, you must configure your bundler to handle CommonJS modules or use a different library. -
TypeError: obj.on is not a function
cause An object intended to be an event emitter has not had the `MicroEE.mixin` applied to its prototype or is not an instance of a class that has applied the mixin.fixBefore calling `on` or `emit` methods, ensure your class or object's prototype has been extended with `MicroEE.mixin(MyClass)` for class-based usage, or that you are using an instance created by `new MicroEE()` directly.
Warnings
- breaking This package explicitly does not support RegExp matching for event names, unlike some other EventEmitter implementations. Event names must be exact strings.
- deprecated The `microee` package is abandoned. The last release (v0.0.6) was published 9 years ago, and there is no ongoing maintenance or development. It is not recommended for new projects.
- gotcha The package is CommonJS-only and relies on `require()` for Node.js or a global `microee` object in browsers. It does not provide native ES module (`import`) support.
Install
-
npm install microee -
yarn add microee -
pnpm add microee
Imports
- MicroEE
import { MicroEE } from 'microee';const MicroEE = require('microee'); - mixin
import { mixin } from 'microee';const MicroEE = require('microee'); MicroEE.mixin(MyClass); - emitter.on
const obj = new MyClass(); obj.on('event', listener);
Quickstart
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();