Socket
Book a DemoInstallSign in
Socket

overshadow-listeners

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

overshadow-listeners

Add an event listener before existing listeners.

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

#overshadow-listeners Build Status

Add an event listener before existing listeners.

Why

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.

Example

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)

detach/reattach

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

detach/reattach is chainable

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

Also See

  • hughsk/on-first

Licence

MIT

Keywords

event

FAQs

Package last updated on 24 Nov 2013

Did you know?

Socket

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.

Install

Related posts