diff --git a/lib/launcher.js b/lib/launcher.js index cc383c3a1..723390c09 100644 --- a/lib/launcher.js +++ b/lib/launcher.js @@ -25,13 +25,37 @@ const baseBrowserDecoratorFactory = function ( } } -function Launcher (server, emitter, injector) { - this._browsers = [] - let lastStartTime +class Launcher { + constructor (server, emitter, injector) { + this._server = server + this._emitter = emitter + this._injector = injector + this._browsers = [] + this._lastStartTime = null + + // Attach list of dependency injection parameters to methods. + this.launch.$inject = [ + 'config.browsers', + 'config.concurrency' + ] + + this.launchSingle.$inject = [ + 'config.protocol', + 'config.hostname', + 'config.port', + 'config.urlRoot', + 'config.upstreamProxy', + 'config.processKillTimeout' + ] + + this._emitter.on('exit', (callback) => this.killAll(callback)) + } - const getBrowserById = (id) => this._browsers.find((browser) => browser.id === id) + getBrowserById (id) { + return this._browsers.find((browser) => browser.id === id) + } - this.launchSingle = (protocol, hostname, port, urlRoot, upstreamProxy, processKillTimeout) => { + launchSingle (protocol, hostname, port, urlRoot, upstreamProxy, processKillTimeout) { if (upstreamProxy) { protocol = upstreamProxy.protocol hostname = upstreamProxy.hostname @@ -58,7 +82,7 @@ function Launcher (server, emitter, injector) { } try { - browser = injector.createChild([locals], ['launcher:' + name]).get('launcher:' + name) + browser = this._injector.createChild([locals], ['launcher:' + name]).get('launcher:' + name) } catch (e) { if (e.message.includes(`No provider for "launcher:${name}"`)) { log.error(`Cannot load browser "${name}": it is not registered! Perhaps you are missing some plugin?`) @@ -66,7 +90,7 @@ function Launcher (server, emitter, injector) { log.error(`Cannot load browser "${name}"!\n ` + e.stack) } - emitter.emit('load_error', 'launcher', name) + this._emitter.emit('load_error', 'launcher', name) return } @@ -89,16 +113,16 @@ function Launcher (server, emitter, injector) { } } - this.launch = (names, concurrency) => { + launch (names, concurrency) { log.info(`Launching browsers ${names.join(', ')} with concurrency ${concurrency === Infinity ? 'unlimited' : concurrency}`) this.jobs = new Jobs({ maxConcurrency: concurrency }) - lastStartTime = Date.now() + this._lastStartTime = Date.now() - if (server.loadErrors.length) { + if (this._server.loadErrors.length) { this.jobs.add((args, done) => done(), []) } else { - names.forEach((name) => injector.invoke(this.launchSingle, this)(name)) + names.forEach((name) => this._injector.invoke(this.launchSingle, this)(name)) } this.jobs.on('end', (err) => { @@ -114,24 +138,9 @@ function Launcher (server, emitter, injector) { return this._browsers } - this.launch.$inject = [ - 'config.browsers', - 'config.concurrency', - 'config.processKillTimeout' - ] - - this.launchSingle.$inject = [ - 'config.protocol', - 'config.hostname', - 'config.port', - 'config.urlRoot', - 'config.upstreamProxy', - 'config.processKillTimeout' - ] - - this.kill = (id, callback) => { + kill (id, callback) { callback = callback || function () {} - const browser = getBrowserById(id) + const browser = this.getBrowserById(id) if (browser) { browser.forceKill().then(callback) @@ -141,8 +150,8 @@ function Launcher (server, emitter, injector) { return false } - this.restart = (id) => { - const browser = getBrowserById(id) + restart (id) { + const browser = this.getBrowserById(id) if (browser) { browser.restart() return true @@ -150,7 +159,7 @@ function Launcher (server, emitter, injector) { return false } - this.killAll = (callback) => { + killAll (callback) { callback = callback || function () {} log.debug('Disconnecting all browsers') @@ -164,20 +173,27 @@ function Launcher (server, emitter, injector) { ).then(callback) } - this.areAllCaptured = () => this._browsers.every((browser) => browser.isCaptured()) + areAllCaptured () { + return this._browsers.every((browser) => browser.isCaptured()) + } - this.markCaptured = (id) => { - const browser = getBrowserById(id) + markCaptured (id) { + const browser = this.getBrowserById(id) if (browser) { browser.markCaptured() - log.debug(`${browser.name} (id ${browser.id}) captured in ${(Date.now() - lastStartTime) / 1000} secs`) + log.debug(`${browser.name} (id ${browser.id}) captured in ${(Date.now() - this._lastStartTime) / 1000} secs`) } } - emitter.on('exit', this.killAll) + static generateId () { + return Math.floor(Math.random() * 100000000).toString() + } +} + +Launcher.factory = function (server, emitter, injector) { + return new Launcher(server, emitter, injector) } -Launcher.$inject = ['server', 'emitter', 'injector'] -Launcher.generateId = () => Math.floor(Math.random() * 100000000).toString() +Launcher.factory.$inject = ['server', 'emitter', 'injector'] exports.Launcher = Launcher diff --git a/lib/server.js b/lib/server.js index f69ae1353..e79a2aa22 100644 --- a/lib/server.js +++ b/lib/server.js @@ -73,7 +73,7 @@ class Server extends KarmaEventEmitter { emitter: ['value', this], server: ['value', this], watcher: ['value', watcher], - launcher: ['type', Launcher], + launcher: ['factory', Launcher.factory], config: ['value', config], preprocess: ['factory', preprocessor.createPriorityPreprocessor], fileList: ['factory', FileList.factory],