Skip to content

Instantly share code, notes, and snippets.

@hoonto
Created June 16, 2013 15:02
Show Gist options
  • Save hoonto/5792317 to your computer and use it in GitHub Desktop.
Save hoonto/5792317 to your computer and use it in GitHub Desktop.
A simple module template, use with sublime text 2 for example
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