Skip to content

Commit

Permalink
fix reconnect error - in case of connection was unexpectedly closed b…
Browse files Browse the repository at this point in the history
…efore it was open,

resolve first promise that was returned from call to connect
  • Loading branch information
darkdarkdragon committed Jan 21, 2016
1 parent 0fefb2b commit ab9d193
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 57 deletions.
64 changes: 10 additions & 54 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/common/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ class Connection extends EventEmitter {
return this._state === WebSocket.OPEN && this._isReady;
}

_onUnexpectedClose() {
_onUnexpectedClose(resolve = function() {}, reject = function() {}) {
this._ws = null;
this._isReady = false;
this.connect().then();
this.connect().then(resolve, reject);
}

_onOpen() {
this._ws.removeListener('close', this._onUnexpectedCloseBound);
this._onUnexpectedCloseBound = this._onUnexpectedClose.bind(this);
this._ws.once('close', this._onUnexpectedCloseBound);

const request = {
command: 'subscribe',
streams: ['ledger']
Expand Down Expand Up @@ -176,7 +180,12 @@ class Connection extends EventEmitter {
this._ws.on('error', error =>
this.emit('error', 'websocket', error.messsage, error));
this._ws.on('message', this._onMessage.bind(this));
this._onUnexpectedCloseBound = this._onUnexpectedClose.bind(this);
// in browser close event can came before open event, so we must
// resolve connect's promise after reconnect in that case.
// after open event we will rebound _onUnexpectedCloseBound
// without resolve and reject functions
this._onUnexpectedCloseBound = this._onUnexpectedClose.bind(this,
resolve, reject);
this._ws.once('close', this._onUnexpectedCloseBound);
this._ws.once('open', () => this._onOpen().then(resolve, reject));
}
Expand Down

0 comments on commit ab9d193

Please sign in to comment.