diff --git a/lib/launcher.js b/lib/launcher.js index fd63c8d3d..aa8b6e253 100644 --- a/lib/launcher.js +++ b/lib/launcher.js @@ -26,13 +26,36 @@ 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 + + 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 @@ -59,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?`) @@ -67,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 } @@ -90,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) => { @@ -115,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) @@ -142,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 @@ -151,7 +159,7 @@ function Launcher (server, emitter, injector) { return false } - this.killAll = (callback) => { + killAll (callback) { callback = callback || function () {} log.debug('Disconnecting all browsers') @@ -165,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 5fd6c08cd..1c9097087 100644 --- a/lib/server.js +++ b/lib/server.js @@ -75,7 +75,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],