Commit 43bfbbe5 authored by Mikael Mello's avatar Mikael Mello
Browse files

fix(connect): Remove 'connected' listener after connection times out

Uncomment `driver .conenct` tests on driver.spec.ts
parent c8c48425
...@@ -16,7 +16,7 @@ let clock ...@@ -16,7 +16,7 @@ let clock
describe('driver', () => { describe('driver', () => {
silence() // suppress log during tests (disable this while developing tests) silence() // suppress log during tests (disable this while developing tests)
before(() => utils.setup()) // add user accounts for bot and mock user before(() => utils.setup()) // add user accounts for bot and mock user
/*
describe('.connect', () => { describe('.connect', () => {
context('with localhost connection', () => { context('with localhost connection', () => {
it('without args, returns a promise', () => { it('without args, returns a promise', () => {
...@@ -91,7 +91,7 @@ describe('driver', () => { ...@@ -91,7 +91,7 @@ describe('driver', () => {
}) })
}) })
}) })
*/
// describe('disconnect', () => { // describe('disconnect', () => {
// Disabled for now, as only Asteroid v2 has a disconnect method // Disabled for now, as only Asteroid v2 has a disconnect method
// it('disconnects from asteroid', async () => { // it('disconnects from asteroid', async () => {
......
...@@ -132,13 +132,18 @@ export function connect (options: IConnectOptions = {}, callback?: ICallback): P ...@@ -132,13 +132,18 @@ export function connect (options: IConnectOptions = {}, callback?: ICallback): P
setupMethodCache(asteroid) // init instance for later caching method calls setupMethodCache(asteroid) // init instance for later caching method calls
asteroid.on('connected', () => events.emit('connected')) asteroid.on('connected', () => events.emit('connected'))
asteroid.on('reconnected', () => events.emit('reconnected')) asteroid.on('reconnected', () => events.emit('reconnected'))
// let cancelled = false let cancelled = false
const rejectionTimeout = setTimeout(function () { const rejectionTimeout = setTimeout(function () {
logger.info(`[connect] Timeout (${config.timeout})`) logger.info(`[connect] Timeout (${config.timeout})`)
// cancelled = true
const err = new Error('Asteroid connection timeout') const err = new Error('Asteroid connection timeout')
cancelled = true
events.removeAllListeners('connected')
callback ? callback(err, asteroid) : reject(err) callback ? callback(err, asteroid) : reject(err)
}, config.timeout) }, config.timeout)
// if to avoid condition where timeout happens before listener to 'connected' is added
// and this listener is not removed (because it was added after the removal)
if (!cancelled) {
events.once('connected', () => { events.once('connected', () => {
logger.info('[connect] Connected') logger.info('[connect] Connected')
// if (cancelled) return asteroid.ddp.disconnect() // cancel if already rejected // if (cancelled) return asteroid.ddp.disconnect() // cancel if already rejected
...@@ -146,6 +151,7 @@ export function connect (options: IConnectOptions = {}, callback?: ICallback): P ...@@ -146,6 +151,7 @@ export function connect (options: IConnectOptions = {}, callback?: ICallback): P
if (callback) callback(null, asteroid) if (callback) callback(null, asteroid)
resolve(asteroid) resolve(asteroid)
}) })
}
}) })
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment