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

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
describe('driver', () => {
silence() // suppress log during tests (disable this while developing tests)
before(() => utils.setup()) // add user accounts for bot and mock user
/*
describe('.connect', () => {
context('with localhost connection', () => {
it('without args, returns a promise', () => {
......@@ -91,7 +91,7 @@ describe('driver', () => {
})
})
})
*/
// describe('disconnect', () => {
// Disabled for now, as only Asteroid v2 has a disconnect method
// it('disconnects from asteroid', async () => {
......
......@@ -132,20 +132,26 @@ export function connect (options: IConnectOptions = {}, callback?: ICallback): P
setupMethodCache(asteroid) // init instance for later caching method calls
asteroid.on('connected', () => events.emit('connected'))
asteroid.on('reconnected', () => events.emit('reconnected'))
// let cancelled = false
let cancelled = false
const rejectionTimeout = setTimeout(function () {
logger.info(`[connect] Timeout (${config.timeout})`)
// cancelled = true
const err = new Error('Asteroid connection timeout')
cancelled = true
events.removeAllListeners('connected')
callback ? callback(err, asteroid) : reject(err)
}, config.timeout)
events.once('connected', () => {
logger.info('[connect] Connected')
// if (cancelled) return asteroid.ddp.disconnect() // cancel if already rejected
clearTimeout(rejectionTimeout)
if (callback) callback(null, asteroid)
resolve(asteroid)
})
// 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', () => {
logger.info('[connect] Connected')
// if (cancelled) return asteroid.ddp.disconnect() // cancel if already rejected
clearTimeout(rejectionTimeout)
if (callback) callback(null, asteroid)
resolve(asteroid)
})
}
})
}
......
Markdown is supported
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