-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
base.js
141 lines (111 loc) · 3.23 KB
/
base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const KarmaEventEmitter = require('../events').EventEmitter
const EventEmitter = require('events').EventEmitter
const Promise = require('bluebird')
const log = require('../logger').create('launcher')
const helper = require('../helper')
const BEING_CAPTURED = 'BEING_CAPTURED'
const CAPTURED = 'CAPTURED'
const BEING_KILLED = 'BEING_KILLED'
const FINISHED = 'FINISHED'
const RESTARTING = 'RESTARTING'
const BEING_FORCE_KILLED = 'BEING_FORCE_KILLED'
/**
* Base launcher that any custom launcher extends.
*/
function BaseLauncher (id, emitter) {
if (this.start) {
return
}
// TODO(vojta): figure out how to do inheritance with DI
Object.keys(EventEmitter.prototype).forEach(function (method) {
this[method] = EventEmitter.prototype[method]
}, this)
this.bind = KarmaEventEmitter.prototype.bind.bind(this)
this.emitAsync = KarmaEventEmitter.prototype.emitAsync.bind(this)
this.id = id
this._state = null
Object.defineProperty(this, 'state', {
get: () => {
return this._state
},
set: (toState) => {
log.debug(`${this._state} -> ${toState}`)
this._state = toState
}
})
this.error = null
let killingPromise
let previousUrl
this.start = function (url) {
previousUrl = url
this.error = null
this.state = BEING_CAPTURED
this.emit('start', url + '?id=' + this.id + (helper.isDefined(this.displayName) ? '&displayName=' + encodeURIComponent(this.displayName) : ''))
}
this.kill = function () {
// Already killed, or being killed.
if (killingPromise) {
return killingPromise
}
killingPromise = this.emitAsync('kill').then(() => {
this.state = FINISHED
})
this.state = BEING_KILLED
return killingPromise
}
this.forceKill = function () {
this.kill()
this.state = BEING_FORCE_KILLED
return killingPromise
}
this.restart = function () {
if (this.state === BEING_FORCE_KILLED) {
return
}
if (!killingPromise) {
killingPromise = this.emitAsync('kill')
}
killingPromise.then(() => {
if (this.state === BEING_FORCE_KILLED) {
this.state = FINISHED
} else {
killingPromise = null
log.debug(`Restarting ${this.name}`)
this.start(previousUrl)
}
})
this.state = RESTARTING
}
this.markCaptured = function () {
if (this.state === BEING_CAPTURED) {
this.state = CAPTURED
}
}
this.isCaptured = function () {
return this.state === CAPTURED
}
this.toString = function () {
return this.name
}
this._done = function (error) {
killingPromise = killingPromise || Promise.resolve()
this.error = this.error || error
this.emit('done')
if (this.error && this.state !== BEING_FORCE_KILLED && this.state !== RESTARTING) {
emitter.emit('browser_process_failure', this)
}
this.state = FINISHED
}
this.STATE_BEING_CAPTURED = BEING_CAPTURED
this.STATE_CAPTURED = CAPTURED
this.STATE_BEING_KILLED = BEING_KILLED
this.STATE_FINISHED = FINISHED
this.STATE_RESTARTING = RESTARTING
this.STATE_BEING_FORCE_KILLED = BEING_FORCE_KILLED
}
BaseLauncher.decoratorFactory = function (id, emitter) {
return function (launcher) {
BaseLauncher.call(launcher, id, emitter)
}
}
module.exports = BaseLauncher