What is tiny-emitter?
The tiny-emitter package is a lightweight event emitter and listener library. It allows you to create an object that can emit events and have listeners that respond to those events. This is useful for creating a simple pub-sub or observer pattern implementation without the overhead of larger libraries.
What are tiny-emitter's main functionalities?
Event Emission
Emitting events with optional data. Listeners for that event will be invoked.
const Emitter = require('tiny-emitter');
let emitter = new Emitter();
emitter.emit('event', 'some data');
Event Listening
Adding a listener for an event. The listener will be called whenever the specified event is emitted.
const Emitter = require('tiny-emitter');
let emitter = new Emitter();
emitter.on('event', function(data) {
console.log(data);
});
One-time Event Listening
Adding a one-time listener for an event. The listener will be invoked only the first time the event is emitted.
const Emitter = require('tiny-emitter');
let emitter = new Emitter();
emitter.once('event', function(data) {
console.log(data);
});
Removing Event Listeners
Removing a specific listener from an event.
const Emitter = require('tiny-emitter');
let emitter = new Emitter();
function listener(data) {
console.log(data);
}
emitter.on('event', listener);
emitter.off('event', listener);
Other packages similar to tiny-emitter
eventemitter3
EventEmitter3 is a high performance EventEmitter. It has a similar API to tiny-emitter but is optimized for performance and has no dependencies.
wolfy87-eventemitter
Wolfy87's EventEmitter is an implementation of the EventEmitter module found in Node.js but for the browser. It offers additional features like namespacing events, but it's larger in size compared to tiny-emitter.
mitt
Mitt is a tiny functional event emitter/listener with a similar scope to tiny-emitter but with an even smaller footprint and no class syntax. It's ES6 module compatible and can be a good alternative for those who prefer functional paradigms.
tiny-emitter
A tiny (less than 1k) event emitter library.
Install
npm
npm install tiny-emitter --save
Usage
var Emitter = require('tiny-emitter');
var emitter = new Emitter();
emitter.on('some-event', function (arg1, arg2, arg3) {
});
emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');
Alternatively, you can skip the initialization step by requiring tiny-emitter/instance
instead. This pulls in an already initialized emitter.
var emitter = require('tiny-emitter/instance');
emitter.on('some-event', function (arg1, arg2, arg3) {
});
emitter.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value');
Instance Methods
on(event, callback[, context])
Subscribe to an event
event
- the name of the event to subscribe tocallback
- the function to call when event is emittedcontext
- (OPTIONAL) - the context to bind the event callback to
once(event, callback[, context])
Subscribe to an event only once
event
- the name of the event to subscribe tocallback
- the function to call when event is emittedcontext
- (OPTIONAL) - the context to bind the event callback to
off(event[, callback])
Unsubscribe from an event or all events. If no callback is provided, it unsubscribes you from all events.
event
- the name of the event to unsubscribe fromcallback
- the function used when binding to the event
emit(event[, arguments...])
Trigger a named event
event
- the event name to emitarguments...
- any number of arguments to pass to the event subscribers
Test and Build
Build (Tests, Browserifies, and minifies)
npm install
npm run build
Test
npm install
npm test
License
MIT