Created
February 17, 2013 01:38
-
-
Save matthewhadley/4969625 to your computer and use it in GitHub Desktop.
inheriting event emitting
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 events = require('events'); | |
var util = require('util'); | |
var Module = function(name) { | |
events.EventEmitter.call(this); | |
var self = this; | |
self.name = name; | |
self.start = function(){ | |
self.data(function(){ | |
self.ready(); | |
}); | |
} | |
self.data = function(cb) { | |
console.log('request data for module ' + self.name); | |
setTimeout(function(){ | |
self.emit('data'); | |
cb(); | |
}, (100 * Math.floor(Math.random()*30))); | |
} | |
self.ready = function() { | |
console.log('ready to render module ' + self.name); | |
self.emit('ready'); | |
} | |
}; | |
util.inherits(Module, events.EventEmitter); | |
var modules = [ | |
new Module('mod a'), | |
new Module('mod b'), | |
new Module('mod c'), | |
new Module('mod d'), | |
new Module('mod e'), | |
] | |
for (m in modules) { | |
ready(m); | |
} | |
function ready(m){ | |
modules[m].on('ready', function() { | |
console.log(modules[m].name + ' MODULE READY: ' +modules[m].name); | |
}); | |
} | |
for (m in modules) { | |
modules[m].start(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment