Created
December 2, 2014 18:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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