Skip to content

Commit

Permalink
use exports instead of singletons in server lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Van Camp committed Jan 3, 2015
1 parent 3d3e1b2 commit 23b6809
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 62 deletions.
99 changes: 48 additions & 51 deletions lib/server/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,68 +11,65 @@ var EventEmitter = require('events').EventEmitter,
var extensions = {};
var expressExtensions = [];

function ExtensionManager() {
var self = this;
bundles.on('allLoaded', function(allBundles) {
log.trace("Starting extension mounting");

while(allBundles.length > 0) {
var startLen = allBundles.length;
for (var i = 0; i < startLen; i++) {
if (!allBundles[i].extension) {
allBundles.splice(i, 1);
break;
}

if (!allBundles[i].bundleDependencies) {
log.debug("Bundle %s has extension with no dependencies", allBundles[i].name);
self._loadExtension(allBundles[i]);
allBundles.splice(i, 1);
break;
}

if (self._bundleDepsSatisfied(allBundles[i])) {
log.debug("Bundle %s has extension with satisfied dependencies", allBundles[i].name);
self._loadExtension(allBundles[i]);
allBundles.splice(i, 1);
break;
}
exports = new EventEmitter();

bundles.on('allLoaded', function(allBundles) {
log.trace("Starting extension mounting");

while(allBundles.length > 0) {
var startLen = allBundles.length;
for (var i = 0; i < startLen; i++) {
if (!allBundles[i].extension) {
allBundles.splice(i, 1);
break;
}

var endLen = allBundles.length;
if (startLen === endLen) {
allBundles.forEach(function(bundle) {
var unsatisfiedDeps = [];
bundle.bundleDependencies.forEach(function(dep) {
if (extensions.hasOwnProperty(dep)) return;
unsatisfiedDeps.push(dep);
});
log.warn("Extension for bundle %s could not be mounted, as it had unsatisfied dependencies:",
bundle.name, unsatisfiedDeps.join(', '));
bundles.remove(bundle.name);
});
log.warn("%d bundle(s) could not be loaded, as their dependencies were not satisfied", endLen);
if (!allBundles[i].bundleDependencies) {
log.debug("Bundle %s has extension with no dependencies", allBundles[i].name);
_loadExtension(allBundles[i]);
allBundles.splice(i, 1);
break;
}

if (_bundleDepsSatisfied(allBundles[i])) {
log.debug("Bundle %s has extension with satisfied dependencies", allBundles[i].name);
_loadExtension(allBundles[i]);
allBundles.splice(i, 1);
break;
}
}

self.emit('extensionsLoaded');
log.trace("Completed extension mounting");
});
}
var endLen = allBundles.length;
if (startLen === endLen) {
allBundles.forEach(function(bundle) {
var unsatisfiedDeps = [];
bundle.bundleDependencies.forEach(function(dep) {
if (extensions.hasOwnProperty(dep)) return;
unsatisfiedDeps.push(dep);
});
log.warn("Extension for bundle %s could not be mounted, as it had unsatisfied dependencies:",
bundle.name, unsatisfiedDeps.join(', '));
bundles.remove(bundle.name);
});
log.warn("%d bundle(s) could not be loaded, as their dependencies were not satisfied", endLen);
break;
}
}

util.inherits(ExtensionManager, EventEmitter);
exports.emit('extensionsLoaded');
log.trace("Completed extension mounting");
});

ExtensionManager.prototype.getExtensions = function() {
exports.getExtensions = function() {
// TODO: return copy?
return extensions;
};

ExtensionManager.prototype.getExpressExtensions = function() {
exports.getExpressExtensions = function() {
return expressExtensions;
};

ExtensionManager.prototype._loadExtension = function(bundle) {
function _loadExtension(bundle) {
var extPath = path.join(bundle.dir, bundle.extension.path);
if (fs.existsSync(extPath)) {
try {
Expand All @@ -91,9 +88,9 @@ ExtensionManager.prototype._loadExtension = function(bundle) {
} else {
log.error("Specified entry point %s for %s does not exist. Skipped.", bundle.extension.path, bundle.name);
}
};
}

ExtensionManager.prototype._bundleDepsSatisfied = function(bundle) {
function _bundleDepsSatisfied(bundle) {
var deps = bundle.bundleDependencies;

for (var extName in extensions) {
Expand All @@ -104,6 +101,6 @@ ExtensionManager.prototype._bundleDepsSatisfied = function(bundle) {
}

return deps.length === 0;
};
}

module.exports = new ExtensionManager();
module.exports = exports;
19 changes: 8 additions & 11 deletions lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ var express = require('express'),
bodyParser = require('body-parser'),
fs = require('fs'),
path = require('path'),
EventEmitter = require('events').EventEmitter,
util = require('util');
EventEmitter = require('events').EventEmitter;

var configHelper = require('../config'),
log,
Expand All @@ -17,11 +16,9 @@ var configHelper = require('../config'),
self,
_baseDir = null;

function Server(baseDirectory) {}
exports = new EventEmitter();

util.inherits(Server, EventEmitter);

Server.prototype.init = function(baseDirectory) {
exports.init = function(baseDirectory) {
if (_baseDir !== null) {
throw new Error('Server already initialised!');
}
Expand All @@ -42,7 +39,7 @@ Server.prototype.init = function(baseDirectory) {
extensionManager.on('extensionNeedsMount', function() {})
};

Server.prototype.start = function() {
exports.start = function() {
log.info('Starting NodeCG');
// Get a fresh config before starting
this.config = configHelper.getConfig();
Expand Down Expand Up @@ -119,7 +116,7 @@ Server.prototype.start = function() {
log.info("NodeCG running on http://%s:%s", this.config.host, this.config.port);
};

Server.prototype.stop = function() {
exports.stop = function() {
server.close();

io = null;
Expand All @@ -129,16 +126,16 @@ Server.prototype.stop = function() {
this.emit('stopped');
};

Server.prototype.getExtensions = function() {
exports.getExtensions = function() {
if (extensionManager) {
return extensionManager.getExtensions();
}

return {};
};

Server.prototype.getIO = function() {
exports.getIO = function() {
return io;
};

module.exports = new Server();
module.exports = exports;

0 comments on commit 23b6809

Please sign in to comment.