@@ -444,21 +444,6 @@ module.exports = Kuzzle = function (url, options, cb) {
444
444
enumerable : true ,
445
445
writable : true
446
446
} ,
447
- loginStrategy : {
448
- value : ( options && typeof options . loginStrategy === 'string' ) ? options . loginStrategy : undefined ,
449
- enumerable : true ,
450
- writable : false
451
- } ,
452
- loginCredentials : {
453
- value : ( options && typeof options . loginCredentials === 'object' ) ? options . loginCredentials : undefined ,
454
- enumerable : true ,
455
- writable : false
456
- } ,
457
- loginExpiresIn : {
458
- value : ( options && [ 'number' , 'string' ] . indexOf ( typeof options . loginExpiresIn ) !== - 1 ) ? options . loginExpiresIn : undefined ,
459
- enumerable : true ,
460
- writable : false
461
- } ,
462
447
jwtToken : {
463
448
value : undefined ,
464
449
enumerable : true ,
@@ -564,44 +549,18 @@ Kuzzle.prototype.connect = function () {
564
549
565
550
self . socket . once ( 'connect' , function ( ) {
566
551
self . state = 'connected' ;
567
-
568
- Object . keys ( self . subscriptions ) . forEach ( function ( roomId ) {
569
- Object . keys ( self . subscriptions [ roomId ] ) . forEach ( function ( subscriptionId ) {
570
- var subscription = self . subscriptions [ roomId ] [ subscriptionId ] ;
571
- subscription . renew ( subscription . callback ) ;
572
- } ) ;
573
- } ) ;
574
-
552
+ renewAllSubscriptions . call ( self ) ;
575
553
dequeue . call ( self ) ;
554
+ emitEvent . call ( self , 'connected' ) ;
576
555
577
- if ( self . loginStrategy ) {
578
- self . login ( self . loginStrategy , self . loginCredentials , self . loginExpiresIn , function ( error ) {
579
- self . eventListeners . connected . forEach ( function ( listener ) {
580
- listener . fn ( error ) ;
581
- } ) ;
582
-
583
- if ( self . connectCB ) {
584
- self . connectCB ( error , self ) ;
585
- }
586
- } ) ;
587
- }
588
- else {
589
- self . eventListeners . connected . forEach ( function ( listener ) {
590
- listener . fn ( ) ;
591
- } ) ;
592
-
593
- if ( self . connectCB ) {
594
- self . connectCB ( null , self ) ;
595
- }
556
+ if ( self . connectCB ) {
557
+ self . connectCB ( null , self ) ;
596
558
}
597
559
} ) ;
598
560
599
561
self . socket . on ( 'connect_error' , function ( error ) {
600
562
self . state = 'error' ;
601
-
602
- self . eventListeners . error . forEach ( function ( listener ) {
603
- listener . fn ( ) ;
604
- } ) ;
563
+ emitEvent . call ( self , 'error' ) ;
605
564
606
565
if ( self . connectCB ) {
607
566
self . connectCB ( error ) ;
@@ -619,23 +578,15 @@ Kuzzle.prototype.connect = function () {
619
578
self . queuing = true ;
620
579
}
621
580
622
- self . eventListeners . disconnected . forEach ( function ( listener ) {
623
- listener . fn ( ) ;
624
- } ) ;
581
+ emitEvent . call ( self , 'disconnected' ) ;
625
582
} ) ;
626
583
627
584
self . socket . on ( 'reconnect' , function ( ) {
628
585
self . state = 'connected' ;
629
586
630
587
// renew subscriptions
631
588
if ( self . autoResubscribe ) {
632
- Object . keys ( self . subscriptions ) . forEach ( function ( roomId ) {
633
- Object . keys ( self . subscriptions [ roomId ] ) . forEach ( function ( subscriptionId ) {
634
- var subscription = self . subscriptions [ roomId ] [ subscriptionId ] ;
635
-
636
- subscription . renew ( subscription . callback ) ;
637
- } ) ;
638
- } ) ;
589
+ renewAllSubscriptions . call ( self ) ;
639
590
}
640
591
641
592
// replay queued requests
@@ -645,9 +596,7 @@ Kuzzle.prototype.connect = function () {
645
596
}
646
597
647
598
// alert listeners
648
- self . eventListeners . reconnected . forEach ( function ( listener ) {
649
- listener . fn ( ) ;
650
- } ) ;
599
+ emitEvent . call ( self , 'reconnected' ) ;
651
600
} ) ;
652
601
653
602
return this ;
@@ -677,6 +626,7 @@ Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
677
626
this . query ( { controller : 'auth' , action : 'login' } , { body : request } , { } , function ( error , response ) {
678
627
if ( error === null ) {
679
628
self . jwtToken = response . jwt ;
629
+ renewAllSubscriptions . call ( self ) ;
680
630
681
631
if ( typeof cb === 'function' ) {
682
632
cb ( null , self ) ;
@@ -759,9 +709,7 @@ function emitRequest (request, cb) {
759
709
if ( self . jwtToken !== undefined || cb ) {
760
710
self . socket . once ( request . requestId , function ( response ) {
761
711
if ( response . error && response . error . message === 'Token expired' ) {
762
- self . eventListeners . jwtTokenExpired . forEach ( function ( listener ) {
763
- listener . fn ( request , cb ) ;
764
- } ) ;
712
+ emitEvent . call ( self , 'jwtTokenExpired' , request , cb ) ;
765
713
}
766
714
767
715
if ( cb ) {
@@ -801,6 +749,36 @@ function dequeue () {
801
749
}
802
750
}
803
751
752
+ /**
753
+ * Renew all registered subscriptions. Triggered either by a successful connection/reconnection or by a
754
+ * successful login attempt
755
+ */
756
+ function renewAllSubscriptions ( ) {
757
+ var self = this ;
758
+
759
+ Object . keys ( self . subscriptions ) . forEach ( function ( roomId ) {
760
+ Object . keys ( self . subscriptions [ roomId ] ) . forEach ( function ( subscriptionId ) {
761
+ var subscription = self . subscriptions [ roomId ] [ subscriptionId ] ;
762
+ subscription . renew ( subscription . callback ) ;
763
+ } ) ;
764
+ } ) ;
765
+ }
766
+
767
+ /**
768
+ * Emits an event to all registered listeners
769
+ *
770
+ * @param {string } event - name of the target global event
771
+ */
772
+ function emitEvent ( event ) {
773
+ var
774
+ self = this ,
775
+ args = Array . prototype . slice . call ( arguments , 1 ) ;
776
+
777
+ self . eventListeners [ event ] . forEach ( function ( listener ) {
778
+ listener . fn . apply ( self , args ) ;
779
+ } ) ;
780
+ }
781
+
804
782
/**
805
783
* Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their
806
784
* insertion.
@@ -1327,6 +1305,7 @@ Kuzzle.prototype.stopQueuing = function () {
1327
1305
return this ;
1328
1306
} ;
1329
1307
1308
+
1330
1309
} , { "./kuzzleDataCollection" :3 , "node-uuid" :1 , "socket.io-client" :undefined } ] , 3 :[ function ( require , module , exports ) {
1331
1310
var
1332
1311
KuzzleDocument = require ( './kuzzleDocument' ) ,
@@ -2529,6 +2508,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
2529
2508
id : {
2530
2509
value : uuid . v4 ( )
2531
2510
} ,
2511
+ lastRenewal : {
2512
+ value : null ,
2513
+ writable : true
2514
+ } ,
2532
2515
notifier : {
2533
2516
value : null ,
2534
2517
writable : true
@@ -2537,6 +2520,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
2537
2520
value : [ ] ,
2538
2521
writable : true
2539
2522
} ,
2523
+ // Delay before allowing a subscription renewal
2524
+ renewalDelay : {
2525
+ value : 500
2526
+ } ,
2540
2527
scope : {
2541
2528
value : options && options . scope ? options . scope : 'all'
2542
2529
} ,
@@ -2636,6 +2623,7 @@ KuzzleRoom.prototype.count = function (cb) {
2636
2623
*/
2637
2624
KuzzleRoom . prototype . renew = function ( filters , cb ) {
2638
2625
var
2626
+ now = Date . now ( ) ,
2639
2627
subscribeQuery = {
2640
2628
scope : this . scope ,
2641
2629
state : this . state ,
@@ -2648,37 +2636,46 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
2648
2636
filters = null ;
2649
2637
}
2650
2638
2639
+ /*
2640
+ Skip subscription renewal if another one was performed a moment before
2641
+ */
2642
+ if ( self . lastRenewal && ( now - self . lastRenewal ) <= self . renewalDelay ) {
2643
+ return self ;
2644
+ }
2645
+
2646
+ self . lastRenewal = now ;
2647
+
2651
2648
if ( filters ) {
2652
- this . filters = filters ;
2649
+ self . filters = filters ;
2653
2650
}
2654
2651
2655
2652
/*
2656
2653
if not yet connected, register itself to the subscriptions list and wait for the
2657
2654
main Kuzzle object to renew once online
2658
2655
*/
2659
- if ( this . kuzzle . state !== 'connected' ) {
2660
- this . callback = cb ;
2661
- this . kuzzle . subscriptions . pending [ self . id ] = self ;
2662
- return this ;
2656
+ if ( self . kuzzle . state !== 'connected' ) {
2657
+ self . callback = cb ;
2658
+ self . kuzzle . subscriptions . pending [ self . id ] = self ;
2659
+ return self ;
2663
2660
}
2664
2661
2665
- if ( this . subscribing ) {
2666
- this . queue . push ( { action : 'renew' , args : [ filters , cb ] } ) ;
2667
- return this ;
2662
+ if ( self . subscribing ) {
2663
+ self . queue . push ( { action : 'renew' , args : [ filters , cb ] } ) ;
2664
+ return self ;
2668
2665
}
2669
2666
2670
- this . kuzzle . callbackRequired ( 'KuzzleRoom.renew' , cb ) ;
2667
+ self . kuzzle . callbackRequired ( 'KuzzleRoom.renew' , cb ) ;
2671
2668
2672
- this . unsubscribe ( ) ;
2673
- this . roomId = null ;
2674
- this . subscribing = true ;
2675
- this . callback = cb ;
2676
- this . kuzzle . subscriptions . pending [ self . id ] = self ;
2669
+ self . unsubscribe ( ) ;
2670
+ self . roomId = null ;
2671
+ self . subscribing = true ;
2672
+ self . callback = cb ;
2673
+ self . kuzzle . subscriptions . pending [ self . id ] = self ;
2677
2674
2678
- subscribeQuery . body = this . filters ;
2679
- subscribeQuery = this . kuzzle . addHeaders ( subscribeQuery , this . headers ) ;
2675
+ subscribeQuery . body = self . filters ;
2676
+ subscribeQuery = self . kuzzle . addHeaders ( subscribeQuery , this . headers ) ;
2680
2677
2681
- self . kuzzle . query ( this . collection . buildQueryArgs ( 'subscribe' , 'on' ) , subscribeQuery , { metadata : this . metadata } , function ( error , response ) {
2678
+ self . kuzzle . query ( self . collection . buildQueryArgs ( 'subscribe' , 'on' ) , subscribeQuery , { metadata : self . metadata } , function ( error , response ) {
2682
2679
delete self . kuzzle . subscriptions . pending [ self . id ] ;
2683
2680
self . subscribing = false ;
2684
2681
@@ -2702,7 +2699,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
2702
2699
dequeue . call ( self ) ;
2703
2700
} ) ;
2704
2701
2705
- return this ;
2702
+ return self ;
2706
2703
} ;
2707
2704
2708
2705
/**
0 commit comments