From d78d94e6488778221d73f56823b1b2f3d64eb0ce Mon Sep 17 00:00:00 2001 From: Scott Gress Date: Tue, 8 Nov 2016 00:20:03 -0500 Subject: [PATCH] Fix issue where properties were overriding methods Replace `isConnecting` and `mightBeAboutToAutoConnect` with private properties --- dist/sails.io.js | 43 ++++++++++++++++++++++++++++++++----------- sails.io.js | 24 ++++++++++++++---------- 2 files changed, 46 insertions(+), 21 deletions(-) diff --git a/dist/sails.io.js b/dist/sails.io.js index 2239808..7d02c77 100644 --- a/dist/sails.io.js +++ b/dist/sails.io.js @@ -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() { @@ -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) { @@ -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) @@ -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' + @@ -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); @@ -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 @@ -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()) { @@ -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'); } @@ -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; }; /** @@ -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; }; /** @@ -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) { diff --git a/sails.io.js b/sails.io.js index 3f54c9a..30e4a6a 100644 --- a/sails.io.js +++ b/sails.io.js @@ -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) { @@ -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) @@ -861,7 +865,7 @@ * successfully. */ self.on('connect', function socketConnected() { - self.isConnecting = false; + self._isConnecting = false; consolog.noPrefix( '\n' + '\n' + @@ -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); @@ -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 @@ -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()) { @@ -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'); } @@ -989,7 +993,7 @@ */ SailsSocket.prototype.isConnecting = function () { - return this.isConnecting; + return this._isConnecting; }; /** @@ -1001,7 +1005,7 @@ * yet, if autoConnect ends up being configured `true`) */ SailsSocket.prototype.mightBeAboutToAutoConnect = function() { - return this.mightBeAboutToAutoConnect; + return this._mightBeAboutToAutoConnect; }; /** @@ -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) {