Skip to content

Commit

Permalink
Fix issue where properties were overriding methods
Browse files Browse the repository at this point in the history
Replace `isConnecting` and `mightBeAboutToAutoConnect` with private properties
  • Loading branch information
sgress454 committed Nov 8, 2016
1 parent 8393d87 commit d78d94e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
43 changes: 32 additions & 11 deletions dist/sails.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,24 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
this.headers = responseCtx.headers || {};
this.statusCode = responseCtx.statusCode || 200;
if (this.statusCode < 200 || this.statusCode >= 400) {
this.error = this.body || this.statusCode;

// Determine the appropriate error message.
var msg;
if (this.statusCode === 0) {
msg = 'Server responded with a '+this.statusCode+' status code';
if (this.body !== undefined && this.body !== null) {
msg += ':\n```\n'+this.body+'\n```';
}
else {
msg += '.';
}
}
else {
msg = 'The socket request failed.';
}

// Now build and attach Error instance.
this.error = new Error(msg);
}
}
JWR.prototype.toString = function() {
Expand Down Expand Up @@ -652,6 +669,10 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
var self = this;
opts = opts||{};

// Initialize private properties
self._isConnecting = false;
self._mightBeAboutToAutoConnect = false;

// Set up connection options so that they can only be changed when socket is disconnected.
var _opts = {};
SOCKET_OPTIONS.forEach(function(option) {
Expand Down Expand Up @@ -716,7 +737,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
SailsSocket.prototype._connect = function (){
var self = this;

self.isConnecting = true;
self._isConnecting = true;

// Apply `io.sails` config as defaults
// (now that at least one tick has elapsed)
Expand Down Expand Up @@ -861,7 +882,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
* successfully.
*/
self.on('connect', function socketConnected() {
self.isConnecting = false;
self._isConnecting = false;
consolog.noPrefix(
'\n' +
'\n' +
Expand Down Expand Up @@ -901,7 +922,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
});

self.on('reconnect', function(transport, numAttempts) {
if (!self.isConnecting) {
if (!self._isConnecting) {
self.on('connect', runRequestQueue.bind(self, self));
}
var msSinceConnectionLost = ((new Date()).getTime() - self.connectionLostTimestamp);
Expand All @@ -918,7 +939,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
// (usually because of a failed authorization, which is in turn
// usually due to a missing or invalid cookie)
self.on('error', function failedToConnect(err) {
self.isConnecting = false;
self._isConnecting = false;
////////////////////////////////////////////////////////////////////////////////////
// Note:
// In the future, we could provide a separate event for when a socket cannot connect
Expand All @@ -941,7 +962,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
* @api public
*/
SailsSocket.prototype.reconnect = function (){
if (this.isConnecting) {
if (this._isConnecting) {
throw new Error('Cannot connect- socket is already connecting');
}
if (this.isConnected()) {
Expand All @@ -956,7 +977,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
* @api public
*/
SailsSocket.prototype.disconnect = function (){
this.isConnecting = false;
this._isConnecting = false;
if (!this.isConnected()) {
throw new Error('Cannot disconnect- socket is already disconnected');
}
Expand Down Expand Up @@ -989,7 +1010,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
*/

SailsSocket.prototype.isConnecting = function () {
return this.isConnecting;
return this._isConnecting;
};

/**
Expand All @@ -1001,7 +1022,7 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
* yet, if autoConnect ends up being configured `true`)
*/
SailsSocket.prototype.mightBeAboutToAutoConnect = function() {
return this.mightBeAboutToAutoConnect;
return this._mightBeAboutToAutoConnect;
};

/**
Expand Down Expand Up @@ -1492,12 +1513,12 @@ pong:3,message:4,upgrade:5,noop:6},s=i(r),t={type:"error",data:"parser error"},u
// by specifying properties on `io.sails`)

// Indicate that the autoConnect timer has started.
io.socket.mightBeAboutToAutoConnect = true;
io.socket._mightBeAboutToAutoConnect = true;

setTimeout(function() {

// Indicate that the autoConect timer fired.
io.socket.mightBeAboutToAutoConnect = false;
io.socket._mightBeAboutToAutoConnect = false;

// If autoConnect is disabled, delete the eager socket (io.socket) and bail out.
if (io.sails.autoConnect === false || io.sails.autoconnect === false) {
Expand Down
24 changes: 14 additions & 10 deletions sails.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@
var self = this;
opts = opts||{};

// Initialize private properties
self._isConnecting = false;
self._mightBeAboutToAutoConnect = false;

// Set up connection options so that they can only be changed when socket is disconnected.
var _opts = {};
SOCKET_OPTIONS.forEach(function(option) {
Expand Down Expand Up @@ -716,7 +720,7 @@
SailsSocket.prototype._connect = function (){
var self = this;

self.isConnecting = true;
self._isConnecting = true;

// Apply `io.sails` config as defaults
// (now that at least one tick has elapsed)
Expand Down Expand Up @@ -861,7 +865,7 @@
* successfully.
*/
self.on('connect', function socketConnected() {
self.isConnecting = false;
self._isConnecting = false;
consolog.noPrefix(
'\n' +
'\n' +
Expand Down Expand Up @@ -901,7 +905,7 @@
});

self.on('reconnect', function(transport, numAttempts) {
if (!self.isConnecting) {
if (!self._isConnecting) {
self.on('connect', runRequestQueue.bind(self, self));
}
var msSinceConnectionLost = ((new Date()).getTime() - self.connectionLostTimestamp);
Expand All @@ -918,7 +922,7 @@
// (usually because of a failed authorization, which is in turn
// usually due to a missing or invalid cookie)
self.on('error', function failedToConnect(err) {
self.isConnecting = false;
self._isConnecting = false;
////////////////////////////////////////////////////////////////////////////////////
// Note:
// In the future, we could provide a separate event for when a socket cannot connect
Expand All @@ -941,7 +945,7 @@
* @api public
*/
SailsSocket.prototype.reconnect = function (){
if (this.isConnecting) {
if (this._isConnecting) {
throw new Error('Cannot connect- socket is already connecting');
}
if (this.isConnected()) {
Expand All @@ -956,7 +960,7 @@
* @api public
*/
SailsSocket.prototype.disconnect = function (){
this.isConnecting = false;
this._isConnecting = false;
if (!this.isConnected()) {
throw new Error('Cannot disconnect- socket is already disconnected');
}
Expand Down Expand Up @@ -989,7 +993,7 @@
*/

SailsSocket.prototype.isConnecting = function () {
return this.isConnecting;
return this._isConnecting;
};

/**
Expand All @@ -1001,7 +1005,7 @@
* yet, if autoConnect ends up being configured `true`)
*/
SailsSocket.prototype.mightBeAboutToAutoConnect = function() {
return this.mightBeAboutToAutoConnect;
return this._mightBeAboutToAutoConnect;
};

/**
Expand Down Expand Up @@ -1492,12 +1496,12 @@
// by specifying properties on `io.sails`)

// Indicate that the autoConnect timer has started.
io.socket.mightBeAboutToAutoConnect = true;
io.socket._mightBeAboutToAutoConnect = true;

setTimeout(function() {

// Indicate that the autoConect timer fired.
io.socket.mightBeAboutToAutoConnect = false;
io.socket._mightBeAboutToAutoConnect = false;

// If autoConnect is disabled, delete the eager socket (io.socket) and bail out.
if (io.sails.autoConnect === false || io.sails.autoconnect === false) {
Expand Down

0 comments on commit d78d94e

Please sign in to comment.