1 var KarmaEventEmitter = require('../events').EventEmitter
2 var EventEmitter = require('events').EventEmitter
4 var log = require('../logger').create('launcher')
11 var BEING_FORCE_KILLED = 6
14 * Base launcher that any custom launcher extends.
16 var BaseLauncher = function (id, emitter) {
21 // TODO(vojta): figure out how to do inheritance with DI
22 Object.keys(EventEmitter.prototype).forEach(function (method) {
23 this[method] = EventEmitter.prototype[method]
25 KarmaEventEmitter.call(this)
35 this.start = function (url) {
39 this.state = BEING_CAPTURED
40 this.emit('start', url + '?id=' + this.id)
43 this.kill = function () {
44 // Already killed, or being killed.
49 killingPromise = this.emitAsync('kill').then(function () {
53 this.state = BEING_KILLED
58 this.forceKill = function () {
60 this.state = BEING_FORCE_KILLED
65 this.restart = function () {
66 if (this.state === BEING_FORCE_KILLED) {
70 if (!killingPromise) {
71 killingPromise = this.emitAsync('kill')
74 killingPromise.then(function () {
75 if (self.state === BEING_FORCE_KILLED) {
79 log.debug('Restarting %s', self.name)
80 self.start(previousUrl)
84 self.state = RESTARTING
87 this.markCaptured = function () {
88 if (this.state === BEING_CAPTURED) {
93 this.isCaptured = function () {
94 return this.state === CAPTURED
97 this.toString = function () {
101 this._done = function (error) {
102 killingPromise = killingPromise || q()
104 this.error = this.error || error
107 if (this.error && this.state !== BEING_FORCE_KILLED && this.state !== RESTARTING) {
108 emitter.emit('browser_process_failure', this)
111 this.state = FINISHED
114 this.STATE_BEING_CAPTURED = BEING_CAPTURED
115 this.STATE_CAPTURED = CAPTURED
116 this.STATE_BEING_KILLED = BEING_KILLED
117 this.STATE_FINISHED = FINISHED
118 this.STATE_RESTARTING = RESTARTING
119 this.STATE_BEING_FORCE_KILLED = BEING_FORCE_KILLED
122 BaseLauncher.decoratorFactory = function (id, emitter) {
123 return function (launcher) {
124 BaseLauncher.call(launcher, id, emitter)
128 module.exports = BaseLauncher