Remove all Function.bind calls

For https://github.com/feross/webtorrent-desktop/issues/256
This commit is contained in:
Feross Aboukhadijeh 2016-03-28 23:38:23 -07:00
parent 96a8fabe2d
commit c92b1714db
4 changed files with 44 additions and 15 deletions

View File

@ -49,7 +49,9 @@ HTTPTracker.prototype.announce = function (opts) {
})
if (self._trackerId) params.trackerid = self._trackerId
self._request(self.announceUrl, params, self._onAnnounceResponse.bind(self))
self._request(self.announceUrl, params, function (data) {
self._onAnnounceResponse(data)
})
}
HTTPTracker.prototype.scrape = function (opts) {
@ -69,7 +71,9 @@ HTTPTracker.prototype.scrape = function (opts) {
var params = {
info_hash: infoHashes
}
self._request(self.scrapeUrl, params, self._onScrapeResponse.bind(self))
self._request(self.scrapeUrl, params, function (data) {
self._onScrapeResponse(data)
})
}
HTTPTracker.prototype.destroy = function (cb) {

View File

@ -22,8 +22,9 @@ Tracker.prototype.setInterval = function (intervalMs) {
clearInterval(self.interval)
if (intervalMs) {
var update = self.announce.bind(self, self.client._defaultAnnounceOpts())
self.interval = setInterval(update, intervalMs)
self.interval = setInterval(function () {
self.announce(self.client._defaultAnnounceOpts())
}, intervalMs)
if (self.interval.unref) self.interval.unref()
}
}

View File

@ -43,7 +43,10 @@ WebSocketTracker.prototype.announce = function (opts) {
var self = this
if (self.destroyed || self.reconnecting) return
if (!self.socket.connected) {
return self.socket.once('connect', self.announce.bind(self, opts))
self.socket.once('connect', function () {
self.announce(opts)
})
return
}
var params = extend(opts, {
@ -72,7 +75,10 @@ WebSocketTracker.prototype.scrape = function (opts) {
var self = this
if (self.destroyed || self.reconnecting) return
if (!self.socket.connected) {
return self.socket.once('connect', self.scrape.bind(self, opts))
self.socket.once('connect', function () {
self.scrape(opts)
})
return
}
var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
@ -142,10 +148,18 @@ WebSocketTracker.prototype._openSocket = function () {
if (!self.peers) self.peers = {}
self._onSocketConnectBound = self._onSocketConnect.bind(self)
self._onSocketErrorBound = self._onSocketError.bind(self)
self._onSocketDataBound = self._onSocketData.bind(self)
self._onSocketCloseBound = self._onSocketClose.bind(self)
self._onSocketConnectBound = function () {
self._onSocketConnect()
}
self._onSocketErrorBound = function (err) {
self._onSocketError(err)
}
self._onSocketDataBound = function (data) {
self._onSocketData(data)
}
self._onSocketCloseBound = function () {
self._onSocketClose()
}
self.socket = socketPool[self.announceUrl]
if (self.socket) {

View File

@ -113,7 +113,9 @@ function Server (opts) {
})
}
self.ws = new WebSocketServer({ server: self.http })
self.ws.address = self.http.address.bind(self.http)
self.ws.address = function () {
return self.http.address()
}
self.ws.on('error', function (err) { self._onError(err) })
self.ws.on('connection', function (socket) { self.onWebSocketConnection(socket) })
}
@ -301,15 +303,23 @@ Server.prototype.onWebSocketConnection = function (socket, opts) {
socket.peerId = null // as hex
socket.infoHashes = [] // swarms that this socket is participating in
socket.onSend = self._onWebSocketSend.bind(self, socket)
socket.onSend = function (err) {
self._onWebSocketSend(socket, err)
}
socket.onMessageBound = self._onWebSocketRequest.bind(self, socket, opts)
socket.onMessageBound = function (params) {
self._onWebSocketRequest(socket, opts, params)
}
socket.on('message', socket.onMessageBound)
socket.onErrorBound = self._onWebSocketError.bind(self, socket)
socket.onErrorBound = function (err) {
self._onWebSocketError(socket, err)
}
socket.on('error', socket.onErrorBound)
socket.onCloseBound = self._onWebSocketClose.bind(self, socket)
socket.onCloseBound = function () {
self._onWebSocketClose(socket)
}
socket.on('close', socket.onCloseBound)
}