@@ -161,7 +161,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
161161
162162  this . unsubscribe ( ) ; 
163163  this . subscribing  =  true ; 
164-   this . kuzzle . subscriptions . pending ++ ; 
164+   self . kuzzle . subscriptions . pending [ self . id ]   =   self ; 
165165
166166  if  ( filters )  { 
167167    this . filters  =  filters ; 
@@ -170,14 +170,15 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
170170  this . roomId  =  null ; 
171171  this . callback  =  cb ; 
172172
173-   subscribeQuery  =  this . kuzzle . addHeaders ( { body : filters } ,  this . headers ) ; 
173+   subscribeQuery  =  this . kuzzle . addHeaders ( { body : self . filters } ,  this . headers ) ; 
174+ 
174175  self . kuzzle . query ( this . collection ,  'subscribe' ,  'on' ,  subscribeQuery ,  { metadata : this . metadata } ,  function  ( error ,  response )  { 
175176    if  ( error )  { 
176177      /* 
177178       If we've already subscribed to this room, Kuzzle returns the actual roomID. 
178179       We'll simply ignore the error and acts as if we successfully subscribed. 
179180        */ 
180-       if  ( error . details . roomId )  { 
181+       if  ( error . details   &&   error . details . roomId )  { 
181182        self . roomId  =  error . details . roomId ; 
182183      }  else  { 
183184        throw  new  Error ( 'Error during Kuzzle subscription: '  +  error . message ) ; 
@@ -196,7 +197,8 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
196197    self . kuzzle . socket . on ( self . roomId ,  self . notifier ) ; 
197198
198199    self . subscribing  =  false ; 
199-     self . kuzzle . subscriptions . pending -- ; 
200+     delete  self . kuzzle . subscriptions . pending [ self . id ] ; 
201+ 
200202    self . dequeue ( ) ; 
201203  } ) ; 
202204
@@ -229,11 +231,11 @@ KuzzleRoom.prototype.unsubscribe = function () {
229231    if  ( Object . keys ( self . kuzzle . subscriptions [ room ] ) . length  ===  1 )  { 
230232      delete  self . kuzzle . subscriptions [ room ] ; 
231233
232-       if  ( self . kuzzle . subscriptions . pending  ===  0 )  { 
234+       if  ( Object . keys ( self . kuzzle . subscriptions . pending ) . length  ===  0 )  { 
233235        self . kuzzle . query ( this . collection ,  'subscribe' ,  'off' ,  { body : { roomId : room } } ) ; 
234236      }  else  { 
235237        interval  =  setInterval ( function  ( )  { 
236-           if  ( self . kuzzle . subscriptions . pending  ===  0 )  { 
238+           if  ( Object . keys ( self . kuzzle . subscriptions . pending ) . length  ===  0 )  { 
237239            if  ( ! self . kuzzle . subscriptions [ room ] )  { 
238240              self . kuzzle . query ( self . collection ,  'subscribe' ,  'off' ,  { body : { roomId : room } } ) ; 
239241            } 
0 commit comments