2727 * @constructor
2828 */
2929module . exports = Kuzzle = function ( url , options , cb ) {
30+ var self = this ;
31+
3032 if ( ! ( this instanceof Kuzzle ) ) {
3133 return new Kuzzle ( url , options , cb ) ;
3234 }
@@ -86,11 +88,11 @@ module.exports = Kuzzle = function (url, options, cb) {
8688 } ,
8789 // read-only properties
8890 autoReconnect : {
89- value : ( options && options . autoReconnect ) ? options . autoReconnect : true ,
91+ value : ( options && typeof options . autoReconnect === 'boolean' ) ? options . autoReconnect : true ,
9092 enumerable : true
9193 } ,
9294 reconnectionDelay : {
93- value : ( options && options . reconnectionDelay ) ? options . reconnectionDelay : 1000 ,
95+ value : ( options && typeof options . reconnectionDelay === 'number' ) ? options . reconnectionDelay : 1000 ,
9496 enumerable : true
9597 } ,
9698 // writable properties
@@ -158,8 +160,8 @@ module.exports = Kuzzle = function (url, options, cb) {
158160
159161 if ( options ) {
160162 Object . keys ( options ) . forEach ( function ( opt ) {
161- if ( this . hasOwnProperty ( opt ) && Object . getOwnPropertyDescriptor ( this , opt ) . writable ) {
162- this [ opt ] = options [ opt ] ;
163+ if ( self . hasOwnProperty ( opt ) && Object . getOwnPropertyDescriptor ( self , opt ) . writable ) {
164+ self [ opt ] = options [ opt ] ;
163165 }
164166 } ) ;
165167
@@ -169,6 +171,7 @@ module.exports = Kuzzle = function (url, options, cb) {
169171 }
170172
171173 // Helper function ensuring that this Kuzzle object is still valid before performing a query
174+ // istanbul ignore next
172175 Object . defineProperty ( this , 'isValid' , {
173176 value : function ( ) {
174177 if ( this . socket === null ) {
@@ -178,6 +181,7 @@ module.exports = Kuzzle = function (url, options, cb) {
178181 } ) ;
179182
180183 // Helper function copying headers to the query data
184+ // istanbul ignore next
181185 Object . defineProperty ( this , 'addHeaders' , {
182186 value : function ( query , headers ) {
183187 Object . keys ( headers ) . forEach ( function ( header ) {
@@ -194,6 +198,7 @@ module.exports = Kuzzle = function (url, options, cb) {
194198 * Some methods (mainly read queries) require a callback function. This function exists to avoid repetition of code,
195199 * and is called by these methods
196200 */
201+ // istanbul ignore next
197202 Object . defineProperty ( this , 'callbackRequired' , {
198203 value : function ( errorMessagePrefix , callback ) {
199204 if ( ! callback || typeof callback !== 'function' ) {
@@ -232,7 +237,11 @@ function construct(url, cb) {
232237 throw new Error ( 'URL to Kuzzle can\'t be empty' ) ;
233238 }
234239
235- self . socket = io ( url , { reconnection : this . autoReconnect , reconnectionDelay : this . reconnectionDelay } ) ;
240+ self . socket = io ( url , {
241+ reconnection : this . autoReconnect ,
242+ reconnectionDelay : this . reconnectionDelay ,
243+ 'force new connection' : true
244+ } ) ;
236245
237246 self . socket . once ( 'connect' , function ( ) {
238247 self . state = 'connected' ;
@@ -242,7 +251,7 @@ function construct(url, cb) {
242251 }
243252 } ) ;
244253
245- self . socket . once ( 'error ' , function ( error ) {
254+ self . socket . once ( 'connect_error ' , function ( error ) {
246255 self . state = 'error' ;
247256 self . logout ( ) ;
248257
@@ -283,8 +292,8 @@ function construct(url, cb) {
283292
284293 // replay queued requests
285294 if ( self . autoReplay ) {
286- cleanQueue . call ( this ) ;
287- dequeue . call ( this ) ;
295+ cleanQueue . call ( self ) ;
296+ dequeue . call ( self ) ;
288297 }
289298
290299 // alert listeners
@@ -414,8 +423,6 @@ Kuzzle.prototype.addListener = function(event, listener) {
414423 * @returns {object } this
415424 */
416425Kuzzle . prototype . getAllStatistics = function ( options , cb ) {
417- this . isValid ( ) ;
418-
419426 if ( ! cb && typeof options === 'function' ) {
420427 cb = options ;
421428 options = null ;
@@ -470,7 +477,6 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
470477 }
471478 }
472479
473- this . isValid ( ) ;
474480 this . callbackRequired ( 'Kuzzle.getStatistics' , cb ) ;
475481
476482 if ( ! timestamp ) {
@@ -549,8 +555,6 @@ Kuzzle.prototype.flushQueue = function () {
549555 * @returns {object } this
550556 */
551557Kuzzle . prototype . listCollections = function ( options , cb ) {
552- this . isValid ( ) ;
553-
554558 if ( ! cb && typeof options === 'function' ) {
555559 cb = options ;
556560 options = null ;
@@ -593,8 +597,6 @@ Kuzzle.prototype.logout = function () {
593597 * @returns {object } this
594598 */
595599Kuzzle . prototype . now = function ( options , cb ) {
596- this . isValid ( ) ;
597-
598600 if ( ! cb && typeof options === 'function' ) {
599601 cb = options ;
600602 options = null ;
@@ -707,8 +709,6 @@ Kuzzle.prototype.removeAllListeners = function (event) {
707709 knownEvents = Object . keys ( this . eventListeners ) ,
708710 self = this ;
709711
710- this . isValid ( ) ;
711-
712712 if ( event ) {
713713 if ( knownEvents . indexOf ( event ) === - 1 ) {
714714 throw new Error ( '[' + event + '] is not a known event. Known events: ' + knownEvents . toString ( ) ) ;
@@ -733,8 +733,6 @@ Kuzzle.prototype.removeListener = function (event, listenerId) {
733733 knownEvents = Object . keys ( this . eventListeners ) ,
734734 self = this ;
735735
736- this . isValid ( ) ;
737-
738736 if ( knownEvents . indexOf ( event ) === - 1 ) {
739737 throw new Error ( '[' + event + '] is not a known event. Known events: ' + knownEvents . toString ( ) ) ;
740738 }
0 commit comments