Fixed Event: Once and forever event emitter
Fixed Event is a Javascript and Node.js package for a common EventEmitter
problem:
- Run a start-up or initialization function, e.g. connect to the DB, load the configuration, etc.
- Fire an event when it is done
- 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 = var status = // Listening before the event firesstatus // Like .emit() but fixes the result in placestatus // Listening after the event fires { status}
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 = done_is_fixed = fixed ducks_are_fixed = fixed values_are_fixed = fixed
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 = statestate // Fires var bros = brosbros // Firesbros // Also fires // This will fire immediatelyvar room = room // 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