Skip to content

Commit

Permalink
refactor: use dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Feb 2, 2013
1 parent 557922d commit 172bf8d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ var Collection = function(emitter, browsers) {
return browsers.forEach(callback, context);
};
};

Collection.$inject = ['emitter'];

exports.Result = Result;
exports.Browser = Browser;
Expand Down
4 changes: 3 additions & 1 deletion lib/file-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ var byPath = function(a, b) {


// TODO(vojta): ignore changes (add/change/remove) when in the middle of refresh
var List = function(patterns, excludes, emitter, preprocess, batchInterval) {
var List = function(patterns, excludes, emitter, preprocess) {
var batchInterval = 250;
var self = this;
var pendingDeferred;
var pendingTimeout;
Expand Down Expand Up @@ -364,6 +365,7 @@ var List = function(patterns, excludes, emitter, preprocess, batchInterval) {
return done();
};
};
List.$inject = ['config.files', 'config.exclude', 'emitter', 'preprocess'];

// PUBLIC
exports.List = List;
Expand Down
6 changes: 5 additions & 1 deletion lib/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ var Launcher = function(emitter) {
var browsers = [];


this.launch = function(names, hostname, port, urlRoot, timeout, retryLimit) {
this.launch = function(names, hostname, port, urlRoot, timeout) {
var url = 'http://' + hostname + ':' + port + urlRoot;
var retryLimit = 3;
var Cls, browser;

names.forEach(function(name) {
Expand All @@ -31,6 +32,7 @@ var Launcher = function(emitter) {
browsers.push(browser);
});
};
this.launch.$inject = ['config.browsers', 'config.hostname', 'config.port', 'config.urlRoot', 'config.captureTimeout'];


this.kill = function(callback) {
Expand Down Expand Up @@ -75,6 +77,8 @@ var Launcher = function(emitter) {
emitter.on('exit', this.kill);
};

Launcher.$inject = ['emitter'];

Launcher.generateId = function() {
return Math.floor(Math.random() * 100000000);
};
Expand Down
1 change: 1 addition & 0 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var createPreprocessor = function(config, basePath) {
return process.nextTick(done);
};
};
createPreprocessor.$inject = ['config.preprocessors', 'config.basePath'];

// Publish
exports.Live = require('./preprocessors/Live');
Expand Down
2 changes: 2 additions & 0 deletions lib/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ var createReporters = function(names, config, emitter) {
return multiReporter;
};

createReporters.$inject = ['config.reporters', 'config', 'emitter'];


// PUBLISH
exports.Dots = require('./reporters/Dots');
Expand Down
53 changes: 33 additions & 20 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,41 @@
var io = require('socket.io');
var net = require('net');
var di = require('di');

var cfg = require('./config');
var ws = require('./web-server');
var logger = require('./logger');
var browser = require('./browser');
var reporter = require('./reporter');
var events = require('./events');
var helper = require('./helper');
var constant = require('./constants');
var watcher = require('./watcher');

var ws = require('./web-server');
var preprocessor = require('./preprocessor');
var Launcher = require('./launcher').Launcher;
var FileList = require('./file-list').List;
var helper = require('./helper');
var reporter = require('./reporter');
var EventEmitter = require('./events').EventEmitter;



// TODO(vojta): get this whole mess under test
exports.start = function(cliOptions, done) {
var config = cfg.parseConfig(cliOptions.configFile, cliOptions);
var start = function(injector, config, launcher, globalEmitter, preprocess, fileList, webServer,
resultReporter, capturedBrowsers, done) {

// Make done callback optional so it's backwards compatible
if (! helper.isFunction(done)) {
done = process.exit;
}

logger.setup(config.logLevel, config.colors, config.loggers);

var log = logger.create();
var globalEmitter = new events.EventEmitter();
var launcher = new Launcher(globalEmitter);

var preprocess = preprocessor.createPreprocessor(config.preprocessors, config.basePath);
var fileList = new FileList(config.files, config.exclude, globalEmitter, preprocess, 250);
// TODO(vojta): wait for fileList resolving before serving first request...
var filesPromise = fileList.refresh();

if (config.autoWatch) {
filesPromise.then(function() {
watcher.watch(config.files, config.exclude, fileList);
injector.invoke(watcher.watch);
});
}

var webServer = ws.createWebServer(config.basePath, config.proxies, config.urlRoot);
// TODO(vojta): instantiate by DI
var socketServer = io.listen(webServer, {
logger: logger.create('socket.io', constant.LOG_ERROR),
resource: config.urlRoot + 'socket.io',
Expand All @@ -63,16 +58,15 @@ exports.start = function(cliOptions, done) {
log.info('Testacular server started at http://' + config.hostname + ':' + config.port + config.urlRoot);

if (config.browsers && config.browsers.length) {
launcher.launch(config.browsers, config.hostname, config.port, config.urlRoot, config.captureTimeout, 3);
injector.invoke(launcher.launch, launcher);
}
});

var resultReporter = reporter.createReporters(config.reporters, config, globalEmitter);
// TODO(vojta): move this to reporter.createReporters
resultReporter.reporters.forEach(function(reporter) {
globalEmitter.bind(reporter);
});

var capturedBrowsers = new browser.Collection(globalEmitter);
var executionScheduled = false;
var pendingCount = 0;
var runningBrowsers;
Expand Down Expand Up @@ -230,3 +224,22 @@ exports.start = function(cliOptions, done) {
disconnectBrowsers(1);
});
};
start.$inject = ['injector', 'config', 'launcher', 'emitter', 'preprocess', 'fileList',
'webServer', 'reporter', 'capturedBrowsers', 'done'];


exports.start = function(cliOptions, done) {
var injector = new di.Injector([
['done', 'value', done || process.exit],
['emitter', 'type', EventEmitter],
['launcher', 'type', Launcher],
['config', 'value', cfg.parseConfig(cliOptions.configFile, cliOptions)],
['preprocess', 'factory', preprocessor.createPreprocessor],
['fileList', 'type', FileList],
['webServer', 'factory', ws.createWebServer],
['reporter', 'factory', reporter.createReporters],
['capturedBrowsers', 'type', browser.Collection]
]);

injector.invoke(start);
};
2 changes: 2 additions & 0 deletions lib/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ exports.watch = function(patterns, excludes, fileList) {

return chokidarWatcher;
};

exports.watch.$inject = ['config.files', 'config.exclude', 'fileList'];
2 changes: 2 additions & 0 deletions lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,5 @@ exports.createWebServer = function (baseFolder, proxies, urlRoot) {

return server;
};

exports.createWebServer.$inject = ['config.basePath', 'config.proxies', 'config.urlRoot'];
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"Kevin Ortman <[email protected]>"
],
"dependencies": {
"di": "0.0.1",
"socket.io": ">= 0.9.0",
"chokidar": ">=0.5.1",
"glob": ">=3.1.14",
Expand Down

0 comments on commit 172bf8d

Please sign in to comment.