Last active
August 29, 2015 14:06
-
-
Save ysndr/5e92e4d5c790750424a0 to your computer and use it in GitHub Desktop.
ControllerManager
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
/** | |
* File: index (.js) | |
* Description: Manages extern controllers bound together using events | |
* Dependencies: E3 (https://gist.github.com/813449af4a6c05b2cede.git) | |
* Author: y4ng0 @ yangodev | |
* Licence: MIT | |
*/ | |
var E3 = require('E3'), | |
util = require('util'), | |
fs = require('fs'), | |
path = require('path'), | |
out = module.exports = {}; | |
//------------------------------------------------------------------------------// | |
var ControllerManager = { | |
emitter: null, | |
controllers: null, | |
_initialized: false, | |
receive: function () { | |
this.emitter.receive.apply(this.emitter, arguments) | |
return this | |
}, | |
send: function () { | |
this.emitter.send.apply(this.emitter, arguments) | |
return this | |
}, | |
init: function (options) { | |
options = options || {} | |
options.reInit = options.reInit || false | |
options.CONTROLLER_DIR_NAME = options.CONTROLLER_DIR_NAME || 'controller' | |
options.ABSOLUTE_PATH = options.ABSOLUTE_PATH || false | |
// return if already initialized | |
if (this._initialized && !options.reInit) return this | |
var self = this | |
var p = path.join(path.dirname(process.argv[1]), | |
options.CONTROLLER_DIR_NAME) | |
// setup new E3 (ExtendedEventEmitter) | |
this.emitter = Object.create(E3.prototype) | |
this.emitter.init() | |
// set _initialzed to prevent endless recusion through intern methods | |
this._initialized = true | |
//load controllers | |
self.controllers = [] | |
try { | |
self.controllers | |
= fs.readdirSync(p) | |
.map(function(item){ | |
return addController( require(path.join(p, item)), self.emitter) | |
}) | |
} catch (err) { | |
console.error( err ) | |
self._initialized = 'failed' | |
} finally { return self } | |
} | |
} | |
function addController(contr, emitter) { | |
var instance = null | |
if ('function' === typeof contr) { | |
instance = new contr() | |
} else if ('object' === typeof contr) { | |
instance = Object.create(contr) | |
} else { | |
return | |
} | |
return instance.init(emitter) | |
} | |
module.exports = { | |
manager: ControllerManager | |
} |
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
{ | |
"name": "ControllerManager", | |
"version": "0.0.1", | |
"description": "Manages extern controllers bound together using events.", | |
"main": "ControllerManager", | |
"scripts": { | |
"test": "node ./test/test.js" | |
}, | |
"dependencies": { | |
"E3": "git://gist.github.com/813449af4a6c05b2cede.git" | |
}, | |
"keywords": [ | |
"manager", | |
"controller", | |
"events" | |
], | |
"author": "y4ng0 @ yangodev", | |
"license": "MIT", | |
"devDependencies": { | |
"E3": "^0.1.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README comming