Add an event listener before any existing listeners.
Overshadow(emitter).on('event', function() {
// this handler will run before any other handlers for 'event'
console.log('overshadowed!')
})
Event listeners always fire in the order they are added, yet sometimes we have no control over this ordering and require certain listeners to definitely run before any others.
var emitter = new EventEmitter()
emitter.on('event', function() {
console.log('added first')
})
emitter.on('event', function() {
console.log('added second')
})
emitter.emit('event')
// console output:
// added first
// added second
// Note event handlers fire in order they were added.
Overshadow(emitter).on('event', function() {
// this handler will run before any other handlers for 'event'
console.log('overshadowed!')
})
emitter.emit('event')
// console output:
// overshadowed!
// added first
// added second
// Note overshadow handler fires first, even though it was added last!
var http = require('http')
var Overshadow = require('overshadow-listeners')
var server = http.createServer(function(req, res) {
res.end('ok!')
})
Overshadow(server).on('request', function(req, res) {
// This handler runs before any other request handlers
// (including the handler supplied to http.createServer!)
if (req.url !== '/') return res.end('no.') // reject any requests other than those for '/'
})
Overshadow(server).once('request', function(req, res) {
// happens only once, before any other request handlers
})
server.listen(9000)
Alternatively, manually detach and reattach listeners:
var overshadow = Overshadow(server)
overshadow.detach('request')
// attach whatever listeners you need
server.on('request', function(req, res) {
if (req.url !== '/') return res.end('no.')
})
overshadow.reattach('request')
// remember to reattach old listeners
We've included a simple .then(fn)
method you can call to make chainable the process of detaching, doing something then reattaching. .then(fn)
does nothing but execute the supplied function
Overshadow(server)
.detach('request')
.then(function() { // '.then' executes supplied fn & returns chain
server.once('request', function(req, res) {
if (req.url !== '/') return res.end('no.')
})
})
.reattach('request') // remember to reattach old listeners
MIT