Skip to content

Commit

Permalink
refactor: subscribe to socket events explicitly
Browse files Browse the repository at this point in the history
This makes code easier to understand, because all listeners are added
explicitly. Now it is clear what methods are called in response to which
events and what parameters are passed to them.

Also it is a preparation for migration to ES2015 classes, where original
approach won't work, because methods in ES2015 classes are added as not
enumerable properties, so it is harder to loop through them.
  • Loading branch information
devoto13 committed Mar 15, 2018
1 parent 2694d54 commit 6c92019
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/browser.js
@@ -1,5 +1,4 @@
var helper = require('./helper')
var events = require('./events')
var logger = require('./logger')

var Result = require('./browser_result')
Expand Down Expand Up @@ -69,7 +68,7 @@ var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter
this.init = function () {
collection.add(this)

events.bindAll(this, socket)
this.bindSocketEvents(socket)

log.info('Connected on socket %s with id %s', socket.id, id)

Expand Down Expand Up @@ -152,7 +151,7 @@ var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter
clearNoActivityTimeout()
}

this.onDisconnect = function (_, disconnectedSocket) {
this.onDisconnect = function (disconnectedSocket) {
activeSockets.splice(activeSockets.indexOf(disconnectedSocket), 1)

if (activeSockets.length) {
Expand Down Expand Up @@ -199,7 +198,7 @@ var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter
})
if (!exists) {
activeSockets.push(newSocket)
events.bindAll(this, newSocket)
this.bindSocketEvents(newSocket)
}

if (pendingDisconnect) {
Expand Down Expand Up @@ -241,6 +240,16 @@ var Browser = function (id, fullName, /* capturedBrowsers */ collection, emitter
this.state = EXECUTING
refreshNoActivityTimeout()
}

this.bindSocketEvents = function (socket) {
// TODO: check which of these events are actually emitted by socket
socket.on('disconnect', function () { self.onDisconnect(socket) })
socket.on('start', function (info) { self.onStart(info) })
socket.on('karma_error', function (error) { self.onKarmaError(error) })
socket.on('complete', function (result) { self.onComplete(result) })
socket.on('info', function (info) { self.onInfo(info) })
socket.on('result', function (result) { self.onResult(result) })
}
}

Browser.STATE_READY = READY
Expand Down

0 comments on commit 6c92019

Please sign in to comment.