Try to fix WS race condition connection (#4976)

* Try to fix wrong token logic

* Linting
This commit is contained in:
Nicolas Gotchac 2017-03-21 17:01:33 +01:00 committed by Jaco Greeff
parent cb881108c3
commit c7e6992239

View File

@ -241,7 +241,7 @@ export default class SecureApi extends Api {
this.transport.updateToken(token, false); this.transport.updateToken(token, false);
log.debug('connecting with token', token); log.debug('connecting with token', token);
return this.transport.connect() const connectPromise = this.transport.connect()
.then(() => { .then(() => {
log.debug('connected with', token); log.debug('connected with', token);
@ -258,26 +258,35 @@ export default class SecureApi extends Api {
log.debug('did not connect ; error', error); log.debug('did not connect ; error', error);
} }
// Check if the Node is up return false;
return this.isNodeUp() });
.then((isNodeUp) => {
// If it's not up, try again in a few...
if (!isNodeUp) {
const timeout = this.transport.retryTimeout;
log.debug('node is not up ; will try again in', timeout, 'ms'); return Promise
.all([
connectPromise,
this.isNodeUp()
])
.then(([ connected, isNodeUp ]) => {
if (connected) {
return true;
}
return new Promise((resolve, reject) => { // If it's not up, try again in a few...
window.setTimeout(() => { if (!isNodeUp) {
this._connectWithToken(token).then(resolve).catch(reject); const timeout = this.transport.retryTimeout;
}, timeout);
});
}
// The token is invalid log.debug('node is not up ; will try again in', timeout, 'ms');
log.debug('tried with a wrong token', token);
return false; return new Promise((resolve, reject) => {
window.setTimeout(() => {
this._connectWithToken(token).then(resolve).catch(reject);
}, timeout);
}); });
}
// The token is invalid
log.debug('tried with a wrong token', token);
return false;
}); });
} }