@@ -161,7 +161,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
161
161
162
162
this . unsubscribe ( ) ;
163
163
this . subscribing = true ;
164
- this . kuzzle . subscriptions . pending ++ ;
164
+ self . kuzzle . subscriptions . pending [ self . id ] = self ;
165
165
166
166
if ( filters ) {
167
167
this . filters = filters ;
@@ -170,14 +170,15 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
170
170
this . roomId = null ;
171
171
this . callback = cb ;
172
172
173
- subscribeQuery = this . kuzzle . addHeaders ( { body : filters } , this . headers ) ;
173
+ subscribeQuery = this . kuzzle . addHeaders ( { body : self . filters } , this . headers ) ;
174
+
174
175
self . kuzzle . query ( this . collection , 'subscribe' , 'on' , subscribeQuery , { metadata : this . metadata } , function ( error , response ) {
175
176
if ( error ) {
176
177
/*
177
178
If we've already subscribed to this room, Kuzzle returns the actual roomID.
178
179
We'll simply ignore the error and acts as if we successfully subscribed.
179
180
*/
180
- if ( error . details . roomId ) {
181
+ if ( error . details && error . details . roomId ) {
181
182
self . roomId = error . details . roomId ;
182
183
} else {
183
184
throw new Error ( 'Error during Kuzzle subscription: ' + error . message ) ;
@@ -196,7 +197,8 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
196
197
self . kuzzle . socket . on ( self . roomId , self . notifier ) ;
197
198
198
199
self . subscribing = false ;
199
- self . kuzzle . subscriptions . pending -- ;
200
+ delete self . kuzzle . subscriptions . pending [ self . id ] ;
201
+
200
202
self . dequeue ( ) ;
201
203
} ) ;
202
204
@@ -229,11 +231,11 @@ KuzzleRoom.prototype.unsubscribe = function () {
229
231
if ( Object . keys ( self . kuzzle . subscriptions [ room ] ) . length === 1 ) {
230
232
delete self . kuzzle . subscriptions [ room ] ;
231
233
232
- if ( self . kuzzle . subscriptions . pending === 0 ) {
234
+ if ( Object . keys ( self . kuzzle . subscriptions . pending ) . length === 0 ) {
233
235
self . kuzzle . query ( this . collection , 'subscribe' , 'off' , { body : { roomId : room } } ) ;
234
236
} else {
235
237
interval = setInterval ( function ( ) {
236
- if ( self . kuzzle . subscriptions . pending === 0 ) {
238
+ if ( Object . keys ( self . kuzzle . subscriptions . pending ) . length === 0 ) {
237
239
if ( ! self . kuzzle . subscriptions [ room ] ) {
238
240
self . kuzzle . query ( self . collection , 'subscribe' , 'off' , { body : { roomId : room } } ) ;
239
241
}
0 commit comments