What is nanoevents?
Nanoevents is a tiny (less than 1KB) event emitter library for JavaScript. It provides a simple and efficient way to handle events in your application, making it ideal for both client-side and server-side development.
What are nanoevents's main functionalities?
Event Emission
This feature allows you to emit events with associated data. The `emit` method triggers all listeners attached to the specified event.
const { createNanoEvents } = require('nanoevents');
const emitter = createNanoEvents();
emitter.emit('event', 'data');
Event Listening
This feature allows you to listen for specific events. The `on` method registers a callback function that will be called whenever the specified event is emitted.
const { createNanoEvents } = require('nanoevents');
const emitter = createNanoEvents();
emitter.on('event', data => {
console.log(data);
});
emitter.emit('event', 'data');
Event Unsubscription
This feature allows you to unsubscribe from events. The `on` method returns an unsubscribe function that can be called to remove the listener.
const { createNanoEvents } = require('nanoevents');
const emitter = createNanoEvents();
const unsubscribe = emitter.on('event', data => {
console.log(data);
});
emitter.emit('event', 'data');
unsubscribe();
emitter.emit('event', 'data');
Other packages similar to nanoevents
eventemitter3
EventEmitter3 is a high-performance event emitter for Node.js and the browser. It is more feature-rich compared to Nanoevents, offering wildcard event listeners and more advanced event handling capabilities.
mitt
Mitt is another tiny event emitter library. It is similar in size and simplicity to Nanoevents but offers a slightly different API. Mitt is also less than 1KB and is designed for minimal overhead.
events
The 'events' package is the Node.js core EventEmitter module extracted for use in browser environments. It is more heavyweight compared to Nanoevents but offers a robust set of features and is widely used in the Node.js ecosystem.
Nano Events
Small and simple events API.
- No node.js EventEmitter compatibility.
- Only 270 bytes (minified and gzipped). It uses Size Limit to control size.
on
and once
methods return unbind
function. You don’t need to save
callback to variable for removeListener
.- No aliases, just
emit
, on
, once
methods.
import NanoEvents from 'nanoevents'
const emitter = new NanoEvents()
const unbind = emitter.on('tick', volume => {
summary += volume
})
emitter.once('tick', () => {
works = true
})
function disable () {
unbind()
}
Usage
Mixing to Object
Because main Nano Events API has only 2 methods,
you could just create proxy methods in your class:
class Ticker {
constructor() {
this.emitter = new NanoEvents()
}
on() {
return this.emitter.on.apply(this.events, arguments)
}
once() {
return this.emitter.once.apply(this.events, arguments)
}
tick() {
this.emitter.emit('tick')
}
}
Add Listener
There are 2 methods to add listener for specific event:
on
and one-time once
.
emitter.on('tick', number => {
console.log('on ' + number)
})
emitter.once('tick', number => {
console.log('once ' + number)
})
emitter.emit('tick', 1)
emitter.emit('tick', 2)
Remove Listener
Methods on
and once
return unbind
function. Call it and this listener
will be removed from event.
const unbind = emitter.on('tick', number => {
console.log('on ' + number)
})
emitter.emit('tick', 1)
unbind()
emitter.emit('tick', 2)
Execute Listeners
Method emit
will execute all listeners. First argument is event name, others
will be passed to listeners.
emitter.on('tick', (a, b) => {
console.log(a, b)
})
emitter.emit('tick', 1, 'one')
Events List
You can get used events list by events
property.
const unbind = emitter.on('tick', () => { })
Object.keys(emitter.events)
unbind()
Object.keys(emitter.events)
Helpers
Remove all listeners
unbindAll
method will remove all listeners to all events.
import unbindAll from 'nanoevents/unbind-all';
emitter.on('event1', () => { })
emitter.on('event2', () => { })
unbindAll(emitter);
Object.keys(emitter.events)