@@ -292,8 +292,8 @@ module.exports = Kuzzle = function (url, options, cb) {
292292 } ,
293293 eventListeners : {
294294 value : {
295- subscribed : [ ] ,
296- unsubscribed : [ ] ,
295+ connected : [ ] ,
296+ error : [ ] ,
297297 disconnected : [ ] ,
298298 reconnected : [ ]
299299 }
@@ -511,6 +511,10 @@ Kuzzle.prototype.connect = function (cb) {
511511
512512 dequeue . call ( self ) ;
513513
514+ self . eventListeners . connected . forEach ( function ( listener ) {
515+ listener . fn ( ) ;
516+ } ) ;
517+
514518 if ( cb ) {
515519 cb ( null , self ) ;
516520 }
@@ -519,6 +523,10 @@ Kuzzle.prototype.connect = function (cb) {
519523 self . socket . on ( 'connect_error' , function ( error ) {
520524 self . state = 'error' ;
521525
526+ self . eventListeners . error . forEach ( function ( listener ) {
527+ listener . fn ( ) ;
528+ } ) ;
529+
522530 if ( cb ) {
523531 cb ( error ) ;
524532 }
@@ -693,20 +701,11 @@ Kuzzle.prototype.getAllStatistics = function (options, cb) {
693701 this . callbackRequired ( 'Kuzzle.getAllStatistics' , cb ) ;
694702
695703 this . query ( null , 'admin' , 'getAllStats' , { } , options , function ( err , res ) {
696- var result = [ ] ;
697-
698704 if ( err ) {
699705 return cb ( err ) ;
700706 }
701707
702- Object . keys ( res . statistics ) . forEach ( function ( key ) {
703- var frame = res . statistics [ key ] ;
704- frame . timestamp = key ;
705-
706- result . push ( frame ) ;
707- } ) ;
708-
709- cb ( null , result ) ;
708+ cb ( null , res . statistics ) ;
710709 } ) ;
711710
712711 return this ;
@@ -742,18 +741,15 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
742741 }
743742
744743 queryCB = function ( err , res ) {
745- var stats = [ ] ;
746-
747744 if ( err ) {
748745 return cb ( err ) ;
749746 }
750747
751- Object . keys ( res . statistics ) . forEach ( function ( frame ) {
752- res . statistics [ frame ] . timestamp = frame ;
753- stats . push ( res . statistics [ frame ] ) ;
754- } ) ;
755-
756- cb ( null , stats ) ;
748+ if ( timestamp ) {
749+ cb ( null , res . statistics ) ;
750+ } else {
751+ cb ( null , [ res . statistics ] ) ;
752+ }
757753 } ;
758754
759755 this . callbackRequired ( 'Kuzzle.getStatistics' , cb ) ;
@@ -2238,6 +2234,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
22382234 value : null ,
22392235 writable : true
22402236 } ,
2237+ channel : {
2238+ value : null ,
2239+ writable : true
2240+ } ,
22412241 id : {
22422242 value : uuid . v4 ( )
22432243 } ,
@@ -2249,10 +2249,19 @@ function KuzzleRoom(kuzzleDataCollection, options) {
22492249 value : [ ] ,
22502250 writable : true
22512251 } ,
2252+ scope : {
2253+ value : options && options . scope ? options . scope : 'all'
2254+ } ,
2255+ state : {
2256+ value : options && options . state ? options . state : 'done'
2257+ } ,
22522258 subscribing : {
22532259 value : false ,
22542260 writable : true
22552261 } ,
2262+ users : {
2263+ value : options && options . users ? options . users : 'none'
2264+ } ,
22562265 // read-only properties
22572266 collection : {
22582267 value : kuzzleDataCollection . collection ,
@@ -2349,7 +2358,11 @@ KuzzleRoom.prototype.count = function (cb) {
23492358 */
23502359KuzzleRoom . prototype . renew = function ( filters , cb ) {
23512360 var
2352- subscribeQuery ,
2361+ subscribeQuery = {
2362+ scope : this . scope ,
2363+ state : this . state ,
2364+ users : this . users
2365+ } ,
23532366 self = this ;
23542367
23552368 if ( ! cb && filters && typeof filters === 'function' ) {
@@ -2365,17 +2378,17 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
23652378 this . kuzzle . callbackRequired ( 'KuzzleRoom.renew' , cb ) ;
23662379
23672380 this . unsubscribe ( ) ;
2381+ this . roomId = null ;
23682382 this . subscribing = true ;
2369- self . kuzzle . subscriptions . pending [ self . id ] = self ;
2383+ this . callback = cb ;
2384+ this . kuzzle . subscriptions . pending [ self . id ] = self ;
23702385
23712386 if ( filters ) {
23722387 this . filters = filters ;
23732388 }
23742389
2375- this . roomId = null ;
2376- this . callback = cb ;
2377-
2378- subscribeQuery = this . kuzzle . addHeaders ( { body : self . filters } , this . headers ) ;
2390+ subscribeQuery . body = this . filters ;
2391+ subscribeQuery = this . kuzzle . addHeaders ( subscribeQuery , this . headers ) ;
23792392
23802393 self . kuzzle . query ( this . collection , 'subscribe' , 'on' , subscribeQuery , { metadata : this . metadata } , function ( error , response ) {
23812394 delete self . kuzzle . subscriptions . pending [ self . id ] ;
@@ -2387,6 +2400,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
23872400 }
23882401
23892402 self . roomId = response . roomId ;
2403+ self . channel = response . channel ;
23902404
23912405 if ( ! self . kuzzle . subscriptions [ self . roomId ] ) {
23922406 self . kuzzle . subscriptions [ self . roomId ] = { } ;
@@ -2395,7 +2409,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
23952409 self . kuzzle . subscriptions [ self . roomId ] [ self . id ] = self ;
23962410
23972411 self . notifier = notificationCallback . bind ( self ) ;
2398- self . kuzzle . socket . on ( self . roomId , self . notifier ) ;
2412+ self . kuzzle . socket . on ( self . channel , self . notifier ) ;
23992413
24002414 dequeue . call ( self ) ;
24012415 } ) ;
@@ -2424,7 +2438,7 @@ KuzzleRoom.prototype.unsubscribe = function () {
24242438 }
24252439
24262440 if ( room ) {
2427- self . kuzzle . socket . off ( room , this . notifier ) ;
2441+ self . kuzzle . socket . off ( self . channel , this . notifier ) ;
24282442
24292443 if ( Object . keys ( self . kuzzle . subscriptions [ room ] ) . length === 1 ) {
24302444 delete self . kuzzle . subscriptions [ room ] ;
@@ -2473,40 +2487,17 @@ KuzzleRoom.prototype.setHeaders = function (content, replace) {
24732487 * @returns {* }
24742488 */
24752489function notificationCallback ( data ) {
2476- var
2477- self = this ,
2478- globalEvent ,
2479- listening ;
2480-
24812490 if ( data . error ) {
2482- return self . callback ( data . error ) ;
2491+ return this . callback ( data . error ) ;
24832492 }
24842493
2485- if ( data . result . action === 'on' || data . result . action === 'off' ) {
2486- if ( data . result . action === 'on' ) {
2487- globalEvent = 'subscribed' ;
2488- listening = self . listenToConnections ;
2489- } else {
2490- globalEvent = 'unsubscribed' ;
2491- listening = self . listenToDisconnections ;
2492- }
2493-
2494- if ( listening || self . kuzzle . eventListeners [ globalEvent ] . length > 0 ) {
2495- if ( listening ) {
2496- self . callback ( null , data . result ) ;
2497- }
2498-
2499- self . kuzzle . eventListeners [ globalEvent ] . forEach ( function ( listener ) {
2500- listener . fn ( self . subscriptionId , data . result ) ;
2501- } ) ;
2502- }
2503- } else if ( self . kuzzle . requestHistory [ data . result . requestId ] ) {
2504- if ( self . subscribeToSelf ) {
2505- self . callback ( null , data . result ) ;
2494+ if ( this . kuzzle . requestHistory [ data . result . requestId ] ) {
2495+ if ( this . subscribeToSelf ) {
2496+ this . callback ( null , data . result ) ;
25062497 }
2507- delete self . kuzzle . requestHistory [ data . result . requestId ] ;
2498+ delete this . kuzzle . requestHistory [ data . result . requestId ] ;
25082499 } else {
2509- self . callback ( null , data . result ) ;
2500+ this . callback ( null , data . result ) ;
25102501 }
25112502}
25122503
0 commit comments