Skip to content

Commit bb012e2

Browse files
committed
refactor: migrate Browser class to ES2015
1 parent 6c92019 commit bb012e2

3 files changed

Lines changed: 182 additions & 166 deletions

File tree

lib/browser.js

Lines changed: 134 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,115 @@
1-
var helper = require('./helper')
2-
var logger = require('./logger')
1+
'use strict'
32

4-
var Result = require('./browser_result')
3+
const Result = require('./browser_result')
4+
const helper = require('./helper')
5+
const logger = require('./logger')
56

67
// The browser is ready to execute tests.
7-
var READY = 1
8+
const READY = 1
89

910
// The browser is executing the tests.
10-
var EXECUTING = 2
11+
const EXECUTING = 2
1112

1213
// The browser is not executing, but temporarily disconnected (waiting for reconnecting).
13-
var READY_DISCONNECTED = 3
14+
const READY_DISCONNECTED = 3
1415

1516
// The browser is executing the tests, but temporarily disconnect (waiting for reconnecting).
16-
var EXECUTING_DISCONNECTED = 4
17+
const EXECUTING_DISCONNECTED = 4
1718

1819
// The browser got permanently disconnected (being removed from the collection and destroyed).
19-
var DISCONNECTED = 5
20+
const DISCONNECTED = 5
2021

21-
var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter, socket, timer,
22-
/* config.browserDisconnectTimeout */ disconnectDelay,
23-
/* config.browserNoActivityTimeout */ noActivityTimeout) {
24-
var name = helper.browserFullNameToShort(fullName)
25-
var log = logger.create(name)
26-
var activeSockets = [socket]
27-
var activeSocketsIds = function () {
28-
return activeSockets.map(function (s) {
29-
return s.id
30-
}).join(', ')
31-
}
32-
33-
var self = this
34-
var pendingDisconnect
35-
var disconnect = function (reason) {
36-
self.state = DISCONNECTED
37-
self.disconnectsCount++
38-
log.warn('Disconnected (%d times)' + (reason || ''), self.disconnectsCount)
39-
emitter.emit('browser_error', self, 'Disconnected' + (reason || ''))
40-
collection.remove(self)
22+
class Browser {
23+
constructor (id, fullName, collection, emitter, socket, timer, disconnectDelay, noActivityTimeout) {
24+
this.id = id
25+
this.fullName = fullName
26+
this.name = helper.browserFullNameToShort(fullName)
27+
this.state = READY
28+
this.lastResult = new Result()
29+
this.disconnectsCount = 0
30+
this.activeSockets = [socket]
31+
this.noActivityTimeout = noActivityTimeout
32+
this.collection = collection
33+
this.emitter = emitter
34+
this.socket = socket
35+
this.timer = timer
36+
this.disconnectDelay = disconnectDelay
37+
38+
this.log = logger.create(this.name)
39+
40+
this.noActivityTimeoutId = null
41+
this.pendingDisconnect = null
4142
}
4243

43-
var noActivityTimeoutId
44-
var refreshNoActivityTimeout = noActivityTimeout ? function () {
45-
clearNoActivityTimeout()
46-
noActivityTimeoutId = timer.setTimeout(function () {
47-
self.lastResult.totalTimeEnd()
48-
self.lastResult.disconnected = true
49-
disconnect(', because no message in ' + noActivityTimeout + ' ms.')
50-
emitter.emit('browser_complete', self)
51-
}, noActivityTimeout)
52-
} : function () {}
53-
54-
var clearNoActivityTimeout = noActivityTimeout ? function () {
55-
if (noActivityTimeoutId) {
56-
timer.clearTimeout(noActivityTimeoutId)
57-
noActivityTimeoutId = null
58-
}
59-
} : function () {}
44+
init () {
45+
this.collection.add(this)
6046

61-
this.id = id
62-
this.fullName = fullName
63-
this.name = name
64-
this.state = READY
65-
this.lastResult = new Result()
66-
this.disconnectsCount = 0
47+
this.bindSocketEvents(this.socket)
6748

68-
this.init = function () {
69-
collection.add(this)
70-
71-
this.bindSocketEvents(socket)
72-
73-
log.info('Connected on socket %s with id %s', socket.id, id)
49+
this.log.info('Connected on socket %s with id %s', this.socket.id, this.id)
7450

7551
// TODO(vojta): move to collection
76-
emitter.emit('browsers_change', collection)
52+
this.emitter.emit('browsers_change', this.collection)
7753

78-
emitter.emit('browser_register', this)
54+
this.emitter.emit('browser_register', this)
7955
}
8056

81-
this.isReady = function () {
57+
isReady () {
8258
return this.state === READY
8359
}
8460

85-
this.toString = function () {
61+
toString () {
8662
return this.name
8763
}
8864

89-
this.onKarmaError = function (error) {
65+
onKarmaError (error) {
9066
if (this.isReady()) {
9167
return
9268
}
9369

9470
this.lastResult.error = true
95-
emitter.emit('browser_error', this, error)
71+
this.emitter.emit('browser_error', this, error)
9672

97-
refreshNoActivityTimeout()
73+
this.refreshNoActivityTimeout()
9874
}
9975

100-
this.onInfo = function (info) {
76+
onInfo (info) {
10177
if (this.isReady()) {
10278
return
10379
}
10480

10581
// TODO(vojta): remove
10682
if (helper.isDefined(info.dump)) {
107-
emitter.emit('browser_log', this, info.dump, 'dump')
83+
this.emitter.emit('browser_log', this, info.dump, 'dump')
10884
}
10985

11086
if (helper.isDefined(info.log)) {
111-
emitter.emit('browser_log', this, info.log, info.type)
87+
this.emitter.emit('browser_log', this, info.log, info.type)
11288
}
11389

11490
if (
11591
!helper.isDefined(info.log) &&
11692
!helper.isDefined(info.dump)
11793
) {
118-
emitter.emit('browser_info', this, info)
94+
this.emitter.emit('browser_info', this, info)
11995
}
12096

121-
refreshNoActivityTimeout()
97+
this.refreshNoActivityTimeout()
12298
}
12399

124-
this.onStart = function (info) {
100+
onStart (info) {
125101
this.lastResult = new Result()
126102
this.lastResult.total = info.total
127103

128104
if (info.total === null) {
129-
log.warn('Adapter did not report total number of specs.')
105+
this.log.warn('Adapter did not report total number of specs.')
130106
}
131107

132-
emitter.emit('browser_start', this, info)
133-
refreshNoActivityTimeout()
108+
this.emitter.emit('browser_start', this, info)
109+
this.refreshNoActivityTimeout()
134110
}
135111

136-
this.onComplete = function (result) {
112+
onComplete (result) {
137113
if (this.isReady()) {
138114
return
139115
}
@@ -145,70 +121,68 @@ var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter
145121
this.lastResult.error = true
146122
}
147123

148-
emitter.emit('browsers_change', collection)
149-
emitter.emit('browser_complete', this, result)
124+
this.emitter.emit('browsers_change', this.collection)
125+
this.emitter.emit('browser_complete', this, result)
150126

151-
clearNoActivityTimeout()
127+
this.clearNoActivityTimeout()
152128
}
153129

154-
this.onDisconnect = function (disconnectedSocket) {
155-
activeSockets.splice(activeSockets.indexOf(disconnectedSocket), 1)
130+
onDisconnect (disconnectedSocket) {
131+
this.activeSockets.splice(this.activeSockets.indexOf(disconnectedSocket), 1)
156132

157-
if (activeSockets.length) {
158-
log.debug('Disconnected %s, still have %s', disconnectedSocket.id, activeSocketsIds())
133+
if (this.activeSockets.length) {
134+
this.log.debug('Disconnected %s, still have %s', disconnectedSocket.id, this.getActiveSocketsIds())
159135
return
160136
}
161137

162138
if (this.state === READY) {
163-
disconnect()
139+
this.disconnect()
164140
} else if (this.state === EXECUTING) {
165-
log.debug('Disconnected during run, waiting %sms for reconnecting.', disconnectDelay)
141+
this.log.debug('Disconnected during run, waiting %sms for reconnecting.', this.disconnectDelay)
166142
this.state = EXECUTING_DISCONNECTED
167143

168-
pendingDisconnect = timer.setTimeout(function () {
169-
self.lastResult.totalTimeEnd()
170-
self.lastResult.disconnected = true
171-
disconnect()
172-
emitter.emit('browser_complete', self)
173-
}, disconnectDelay)
144+
this.pendingDisconnect = this.timer.setTimeout(() => {
145+
this.lastResult.totalTimeEnd()
146+
this.lastResult.disconnected = true
147+
this.disconnect()
148+
this.emitter.emit('browser_complete', this)
149+
}, this.disconnectDelay)
174150

175-
clearNoActivityTimeout()
151+
this.clearNoActivityTimeout()
176152
}
177153
}
178154

179-
this.reconnect = function (newSocket) {
155+
reconnect (newSocket) {
180156
if (this.state === EXECUTING_DISCONNECTED) {
181157
this.state = EXECUTING
182-
log.debug('Reconnected on %s.', newSocket.id)
158+
this.log.debug('Reconnected on %s.', newSocket.id)
183159
} else if (this.state === EXECUTING || this.state === READY) {
184-
log.debug('New connection %s (already have %s)', newSocket.id, activeSocketsIds())
160+
this.log.debug('New connection %s (already have %s)', newSocket.id, this.getActiveSocketsIds())
185161
} else if (this.state === DISCONNECTED) {
186162
this.state = READY
187-
log.info('Connected on socket %s with id %s', newSocket.id, this.id)
188-
collection.add(this)
163+
this.log.info('Connected on socket %s with id %s', newSocket.id, this.id)
164+
this.collection.add(this)
189165

190166
// TODO(vojta): move to collection
191-
emitter.emit('browsers_change', collection)
167+
this.emitter.emit('browsers_change', this.collection)
192168

193-
emitter.emit('browser_register', this)
169+
this.emitter.emit('browser_register', this)
194170
}
195171

196-
var exists = activeSockets.some(function (s) {
197-
return s.id === newSocket.id
198-
})
172+
const exists = this.activeSockets.some((s) => s.id === newSocket.id)
199173
if (!exists) {
200-
activeSockets.push(newSocket)
174+
this.activeSockets.push(newSocket)
201175
this.bindSocketEvents(newSocket)
202176
}
203177

204-
if (pendingDisconnect) {
205-
timer.clearTimeout(pendingDisconnect)
178+
if (this.pendingDisconnect) {
179+
this.timer.clearTimeout(this.pendingDisconnect)
206180
}
207181

208-
refreshNoActivityTimeout()
182+
this.refreshNoActivityTimeout()
209183
}
210184

211-
this.onResult = function (result) {
185+
onResult (result) {
212186
if (result.length) {
213187
return result.forEach(this.onResult, this)
214188
}
@@ -220,38 +194,78 @@ var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter
220194

221195
this.lastResult.add(result)
222196

223-
emitter.emit('spec_complete', this, result)
224-
refreshNoActivityTimeout()
197+
this.emitter.emit('spec_complete', this, result)
198+
this.refreshNoActivityTimeout()
225199
}
226200

227-
this.serialize = function () {
201+
serialize () {
228202
return {
229203
id: this.id,
230204
name: this.name,
231205
isReady: this.state === READY
232206
}
233207
}
234208

235-
this.execute = function (config) {
236-
activeSockets.forEach(function (socket) {
237-
socket.emit('execute', config)
238-
})
209+
execute (config) {
210+
this.activeSockets.forEach((socket) => socket.emit('execute', config))
239211

240212
this.state = EXECUTING
241-
refreshNoActivityTimeout()
213+
this.refreshNoActivityTimeout()
214+
}
215+
216+
getActiveSocketsIds () {
217+
return this.activeSockets.map((s) => s.id).join(', ')
218+
}
219+
220+
disconnect (reason) {
221+
this.state = DISCONNECTED
222+
this.disconnectsCount++
223+
this.log.warn('Disconnected (%d times)' + (reason || ''), this.disconnectsCount)
224+
this.emitter.emit('browser_error', this, 'Disconnected' + (reason || ''))
225+
this.collection.remove(this)
226+
}
227+
228+
refreshNoActivityTimeout () {
229+
if (this.noActivityTimeout) {
230+
this.clearNoActivityTimeout()
231+
232+
this.noActivityTimeoutId = this.timer.setTimeout(() => {
233+
this.lastResult.totalTimeEnd()
234+
this.lastResult.disconnected = true
235+
this.disconnect(', because no message in ' + this.noActivityTimeout + ' ms.')
236+
this.emitter.emit('browser_complete', this)
237+
}, this.noActivityTimeout)
238+
}
242239
}
243240

244-
this.bindSocketEvents = function (socket) {
241+
clearNoActivityTimeout () {
242+
if (this.noActivityTimeout) {
243+
if (this.noActivityTimeoutId) {
244+
this.timer.clearTimeout(this.noActivityTimeoutId)
245+
this.noActivityTimeoutId = null
246+
}
247+
}
248+
}
249+
250+
bindSocketEvents (socket) {
245251
// TODO: check which of these events are actually emitted by socket
246-
socket.on('disconnect', function () { self.onDisconnect(socket) })
247-
socket.on('start', function (info) { self.onStart(info) })
248-
socket.on('karma_error', function (error) { self.onKarmaError(error) })
249-
socket.on('complete', function (result) { self.onComplete(result) })
250-
socket.on('info', function (info) { self.onInfo(info) })
251-
socket.on('result', function (result) { self.onResult(result) })
252+
socket.on('disconnect', () => this.onDisconnect(socket))
253+
socket.on('start', (info) => this.onStart(info))
254+
socket.on('karma_error', (error) => this.onKarmaError(error))
255+
socket.on('complete', (result) => this.onComplete(result))
256+
socket.on('info', (info) => this.onInfo(info))
257+
socket.on('result', (result) => this.onResult(result))
252258
}
253259
}
254260

261+
Browser.factory = function (
262+
id, fullName, /* capturedBrowsers */ collection, emitter, socket, timer,
263+
/* config.browserDisconnectTimeout */ disconnectDelay,
264+
/* config.browserNoActivityTimeout */ noActivityTimeout
265+
) {
266+
return new Browser(id, fullName, collection, emitter, socket, timer, disconnectDelay, noActivityTimeout)
267+
}
268+
255269
Browser.STATE_READY = READY
256270
Browser.STATE_EXECUTING = EXECUTING
257271
Browser.STATE_READY_DISCONNECTED = READY_DISCONNECTED

lib/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ Server.prototype._start = function (config, launcher, preprocess, fileList,
307307
id: ['value', info.id || null],
308308
fullName: ['value', (helper.isDefined(info.displayName) ? info.displayName : info.name)],
309309
socket: ['value', socket]
310-
}]).instantiate(Browser)
310+
}]).invoke(Browser.factory)
311311

312312
newBrowser.init()
313313

0 commit comments

Comments
 (0)