Created
June 16, 2013 15:02
-
-
Save hoonto/5792317 to your computer and use it in GitHub Desktop.
A simple module template, use with sublime text 2 for example
This file contains 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 inherits = require('util').inherits; | |
var EventEmitter = require('events').EventEmitter; | |
module.exports = TEMPLATE; | |
function TEMPLATE (opts) { | |
EventEmitter.call(this); | |
var self = this; | |
if (!(self instanceof TEMPLATE)) return new TEMPLATE(opts); //If you don't use the 'new', I will for you. | |
if (!opts) opts = {}; | |
//private stuff: | |
self._privthing = {}; | |
//public stuff: | |
self.pubthing = {}; | |
function somefunc () { | |
} | |
//if you have something that emits stuff: | |
self.emitthing.on('create', function (thing) { | |
// performs complicated calculations | |
// but don't block: | |
process.nextTick(function () { | |
//and when you're done you can: | |
self.emit('done', 'some results'); | |
}); | |
}); | |
} | |
//Now you can emit: | |
inherits(TEMPLATE, EventEmitter); | |
function dutyOfSubclass() { | |
throw new Error('method must be implemented by subclass'); | |
} | |
TEMPLATE.prototype.youdostuff = dutyOfSubclass; | |
TEMPLATE.prototype.idostuff = function (stuff) { | |
var self = this; | |
//emit stuff | |
this.emit('ididstuff'); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment