Skip to content

Commit

Permalink
minor refactoring in extensions lib, making it easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Van Camp committed Feb 19, 2015
1 parent ddc8f14 commit 0326cb3
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions lib/server/extensions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

var EventEmitter = require('events').EventEmitter,
fs = require('fs'),
path = require('path'),
bundles = require('../bundles'),
ExtensionApi = require('../extension_api'),
log = require('../logger')('nodecg/lib/server/extensions');
var EventEmitter = require('events').EventEmitter;
var fs = require('fs');
var path = require('path');
var bundles = require('../bundles');
var ExtensionApi = require('../extension_api');
var log = require('../logger')('nodecg/lib/server/extensions');

var extensions = {};

Expand All @@ -14,33 +14,24 @@ exports = new EventEmitter();
bundles.on('allLoaded', function(allBundles) {
log.trace('Starting extension mounting');

var unsatisfiedDeps = [];
function checkBundles(bundle) {
bundle.bundleDependencies.forEach(checkBundleDep);
log.warn('Extension for bundle %s could not be mounted, as it had unsatisfied dependencies:',
bundle.name, unsatisfiedDeps.join(', '));
bundles.remove(bundle.name);
}
function checkBundleDep(dep) {
if (extensions.hasOwnProperty(dep)) return;
unsatisfiedDeps.push(dep);
}

while(allBundles.length > 0) {
var startLen = allBundles.length;
for (var i = 0; i < startLen; i++) {
// If this bundle does not have an extension, remove it from the list
if (!allBundles[i].extension) {
allBundles.splice(i, 1);
break;
}

// If this bundle has no dependencies, load it and remove it from the list
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 this bundle has dependencies, and all of them are satisfied, load it and remove it from the list
if (_bundleDepsSatisfied(allBundles[i])) {
log.debug('Bundle %s has extension with satisfied dependencies', allBundles[i].name);
_loadExtension(allBundles[i]);
Expand All @@ -51,7 +42,24 @@ bundles.on('allLoaded', function(allBundles) {

var endLen = allBundles.length;
if (startLen === endLen) {
allBundles.forEach(checkBundles);
// This block can only ever be entered once, so its safe to define a function here
// even though we're inside of a `while` loop.
/* jshint -W083 */

// Any bundles left over must have had unsatisfied dependencies.
// Print a warning about each bundle, and what its unsatisfied deps were.
// Then, unload the bundle.
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);
});
/*jshint +W083 */
log.warn('%d bundle(s) could not be loaded, as their dependencies were not satisfied', endLen);
break;
}
Expand Down

0 comments on commit 0326cb3

Please sign in to comment.