Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created December 2, 2014 18:20
Show Gist options
  • Save adamloving/d08eb3f78e70e3675501 to your computer and use it in GitHub Desktop.
Save adamloving/d08eb3f78e70e3675501 to your computer and use it in GitHub Desktop.
Example of missing events. Inspired by http://howtonode.org/understanding-process-next-tick
var EventEmitter = require('events').EventEmitter;
// Constructor emits before client subscribed
function StreamLibrary(resourceName) {
// should use process.nextTick here to delay the firing of the event
this.emit('start');
// read from the file, and for every chunk read, do:
this.emit('data', chunkRead);
}
StreamLibrary.prototype.__proto__ = EventEmitter.prototype; // inherit from EventEmitter
// Somewhere else, someone is listening to these events:
var stream = new StreamLibrary('whatever');
stream.on('start', function() {
console.log('Reading has started');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment