Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
nanoevents
Advanced tools
Simple and tiny event emitter library for JavaScript.
on
method returns unbind
function. You don’t need to save
callback to variable for removeListener
.emit
and on
methods.import NanoEvents from 'nanoevents'
const emitter = new NanoEvents()
const unbind = emitter.on('tick', volume => {
summary += volume
})
function disable () {
unbind()
}
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.emitter, arguments)
}
tick() {
this.emitter.emit('tick')
}
}
Use on
method to add listener for specific event:
emitter.on('tick', number => {
console.log(number)
})
emitter.emit('tick', 1)
// Prints "1"
emitter.emit('tick', 2)
// Prints "2"
Methods on
returns 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)
// Prints "on 1"
unbind()
emitter.emit('tick', 2)
// Prints nothing
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')
// Prints 1, 'one'
You can get used events list by events
property.
const unbind = emitter.on('tick', () => { })
Object.keys(emitter.events) //=> ["tick"]
unbind()
Object.keys(emitter.events) //=> []
If you need add event listener only for first event dispatch, you can use this snippet:
class Ticker {
constructor() {
this.emitter = new NanoEvents()
}
…
once (event, callback) {
const unbind = this.emitter.on(event, function () {
unbind()
callback.apply(this, arguments)
})
return unbind
}
}
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) //=> { }
1.0.8
FAQs
Simple and tiny (107 bytes) event emitter library
The npm package nanoevents receives a total of 93,111 weekly downloads. As such, nanoevents popularity was classified as popular.
We found that nanoevents demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.