diff --git a/index.js b/index.js index 58f107d..98456ec 100644 --- a/index.js +++ b/index.js @@ -171,6 +171,25 @@ module.exports = class Hyperswarm extends EventEmitter { this._clientConnections++ let opened = false + conn.on('open', () => { + opened = true + this._connectDone() + this.connections.add(conn) + conn.removeListener('error', noop) + peerInfo._connected() + peerInfo.client = true + this.emit('connection', conn, peerInfo) + this._flushMaybe(peerInfo) + + this.emit('update') + }) + conn.on('error', err => { + if (this.relayThrough && shouldForceRelaying(err.code)) { + peerInfo.forceRelaying = true + // Reset the attempts in order to fast connect to relay + peerInfo.attempts = 0 + } + }) conn.on('close', () => { if (!opened) this._connectDone() this.connections.delete(conn) @@ -187,25 +206,6 @@ module.exports = class Hyperswarm extends EventEmitter { this.emit('update') }) - conn.on('error', err => { - if (this.relayThrough && shouldForceRelaying(err.code)) { - peerInfo.forceRelaying = true - // Reset the attempts in order to fast connect to relay - peerInfo.attempts = 0 - } - }) - conn.on('open', () => { - opened = true - this._connectDone() - this.connections.add(conn) - conn.removeListener('error', noop) - peerInfo._connected() - peerInfo.client = true - this.emit('connection', conn, peerInfo) - this._flushMaybe(peerInfo) - - this.emit('update') - }) this.emit('update') }