npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fixed-event

v0.2.0

Published

addListener() after an event was emitted and still get called back

Downloads

1

Readme

Fixed Event: Once and forever event emitter

Fixed Event is a Javascript and Node.js package for a common EventEmitter problem:

  1. Run a start-up or initialization function, e.g. connect to the DB, load the configuration, etc.
  2. Fire an event when it is done
  3. Later, you have code that depends on the start-up routing finishing. Should you subscribe to the event? What you want is this:
  • If it has not finished yet, just subscribe to it normally
  • If it has finished, execute your callback immediately with the original result

Fixed Event is available as an NPM module.

$ npm install fixed-event

Example

var fixed = require('fixed-event')
var status = new fixed.EventEmitter

// Listening before the event fires
status.on('ready', function(result) {
  console.log('Ready first listener: ' + result)
})

// Like .emit() but fixes the result in place
status.fixed('ready', "Awesome!")

// Listening after the event fires
setTimeout(late_listener, 1000)

function late_listener() {
  status.on('ready', function(result) {
    console.log('Ready second listener: ' + result)
  })
}

Output

Ready first listener: Awesome!
Ready second listener: Awesome!

Details

A Fixed EventEmitter is a normal EventEmitter, with a few extra methods:

  • fix("event_name") | Fix event_name. The next emit for "event_name" works normally; but all subsequently-added listeners will fire immediately
  • fixed("event_name", [val1], [val2]) | Fix event_name and also emit the event, with val1, val2, etc. as parameters

Defaults

Fixed Event is defaultable. The default option fixed allows pre-setting event names.

var fixed = require('fixed-event')
  , done_is_fixed    = fixed.defaults({ events:"done" })
  , ducks_are_fixed  = fixed.defaults({ events:["Huey", "Dewey", "Louie"] })
  , values_are_fixed = fixed.defaults({ events:{ enter: "You entered!"
                                               , exit : ["You", "exited"]
                                               }})

The events default supports different data types

  • string | Automatically runs .fix() for that name (no events fired yet)
  • array | Automatically run .fix() for all those names (no events fired yet)
  • object | Every key is both fixed, and emitted with the value indicated

Thus, the above code could be used like so

var state = new done_is_fixed.EventEmitter
state.emit('done', 'Hi, listener below me!')
state.on('done', function(hi) { console.log('Loud and clear.') }) // Fires

var bros = new ducks_are_fixed.EventEmitter
bros.emit('Dewey', 'Duck')
bros.on('Dewey', function() { 'Dewey event fired' }) // Fires
bros.on('Dewey', function() { 'Dewey fired again' }) // Also fires

// This will fire immediately
var room = new values_are_fixed.EventEmitter
room.on('enter', function(msg) { console.log(msg) }) // output: "You entered!"

Tests

Follow uses node-tap. If you clone this Git repository, tap is included.

$ ./node_modules/.bin/tap test
ok test/event.js ...................................... 19/19
ok test/once.js ................................... 5011/5011
total ............................................. 5032/5032

ok

License

Apache 2.0