@@ -22,10 +22,6 @@ var uuid = require('node-uuid');
22
22
* @constructor
23
23
*/
24
24
function KuzzleRoom ( kuzzleDataCollection , options ) {
25
- if ( ! kuzzleDataCollection ) {
26
- throw new Error ( 'KuzzleRoom: missing parameters' ) ;
27
- }
28
-
29
25
// Define properties
30
26
Object . defineProperties ( this , {
31
27
// private properties
@@ -173,20 +169,16 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
173
169
subscribeQuery = this . kuzzle . addHeaders ( { body : self . filters } , this . headers ) ;
174
170
175
171
self . kuzzle . query ( this . collection , 'subscribe' , 'on' , subscribeQuery , { metadata : this . metadata } , function ( error , response ) {
172
+ delete self . kuzzle . subscriptions . pending [ self . id ] ;
173
+ self . subscribing = false ;
174
+
176
175
if ( error ) {
177
- /*
178
- If we've already subscribed to this room, Kuzzle returns the actual roomID.
179
- We'll simply ignore the error and acts as if we successfully subscribed.
180
- */
181
- if ( error . details && error . details . roomId ) {
182
- self . roomId = error . details . roomId ;
183
- } else {
184
- throw new Error ( 'Error during Kuzzle subscription: ' + error . message ) ;
185
- }
186
- } else {
187
- self . roomId = response . roomId ;
176
+ self . queue = [ ] ;
177
+ throw new Error ( 'Error during Kuzzle subscription: ' + error . message ) ;
188
178
}
189
179
180
+ self . roomId = response . roomId ;
181
+
190
182
if ( ! self . kuzzle . subscriptions [ self . roomId ] ) {
191
183
self . kuzzle . subscriptions [ self . roomId ] = { } ;
192
184
}
@@ -196,10 +188,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
196
188
self . notifier = notificationCallback . bind ( self ) ;
197
189
self . kuzzle . socket . on ( self . roomId , self . notifier ) ;
198
190
199
- self . subscribing = false ;
200
- delete self . kuzzle . subscriptions . pending [ self . id ] ;
201
-
202
- self . dequeue ( ) ;
191
+ dequeue . call ( self ) ;
203
192
} ) ;
204
193
205
194
return this ;
@@ -241,7 +230,7 @@ KuzzleRoom.prototype.unsubscribe = function () {
241
230
}
242
231
clearInterval ( interval ) ;
243
232
}
244
- } , 500 ) ;
233
+ } , 100 ) ;
245
234
}
246
235
} else {
247
236
delete self . kuzzle . subscriptions [ room ] [ self . id ] ;
@@ -267,19 +256,6 @@ KuzzleRoom.prototype.setHeaders = function (content, replace) {
267
256
return this ;
268
257
} ;
269
258
270
- /**
271
- * Dequeue actions performed while subscription was being renewed
272
- */
273
- KuzzleRoom . prototype . dequeue = function ( ) {
274
- var element ;
275
-
276
- while ( this . queue . length > 0 ) {
277
- element = this . queue . shift ( ) ;
278
-
279
- this [ element . action ] . apply ( this , element . args ) ;
280
- }
281
- } ;
282
-
283
259
/**
284
260
* Callback called by socket.io when a message is sent to the subscribed room ID
285
261
* Calls the registered callback if the notification passes the subscription filters
@@ -312,7 +288,7 @@ function notificationCallback (data) {
312
288
}
313
289
314
290
self . kuzzle . eventListeners [ globalEvent ] . forEach ( function ( listener ) {
315
- listener ( self . subscriptionId , data . result ) ;
291
+ listener . fn ( self . subscriptionId , data . result ) ;
316
292
} ) ;
317
293
}
318
294
} else if ( self . kuzzle . requestHistory [ data . result . requestId ] ) {
@@ -325,4 +301,18 @@ function notificationCallback (data) {
325
301
}
326
302
}
327
303
304
+
305
+ /**
306
+ * Dequeue actions performed while subscription was being renewed
307
+ */
308
+ function dequeue ( ) {
309
+ var element ;
310
+
311
+ while ( this . queue . length > 0 ) {
312
+ element = this . queue . shift ( ) ;
313
+
314
+ this [ element . action ] . apply ( this , element . args ) ;
315
+ }
316
+ }
317
+
328
318
module . exports = KuzzleRoom ;
0 commit comments