Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | 2x 2x 37x 37x 5x 35x 35x 32x 2x 6x 5x 5x 1x 1x 4x 1x 3x 2x 2x 15x 15x 15x 15x 1x 1x 1x 15x 8x 15x 14x 2x 12x 11x 12x 1x 1x 1x 1x 11x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 6x 6x 6x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 15x 15x 15x 1x 1x 14x 4x 4x 3x 3x 1x 2x 2x 2x 1x 2x 1x 4x 14x 2x 1x 1x 9x 9x 1x 1x 8x 7x 7x 8x 7x 2x 3x 3x 20x 2x | var uuidv4 = require('uuid/v4'), Document = require('./Document'); /** * This is a global callback pattern, called by all asynchronous functions of the Kuzzle object. * * @callback responseCallback * @param {Object} err - Error object, NULL if the query is successful * @param {Object} [data] - The content of the query response */ /** * This object is the result of a subscription request, allowing to manipulate the subscription itself. * * In Kuzzle, you don’t exactly subscribe to a room or a topic but, instead, you subscribe to documents. * * What it means is that, to subscribe, you provide to Kuzzle a set of matching filters. * Once you have subscribed, if a pub/sub message is published matching your filters, or if a matching stored * document change (because it is created, updated or deleted), then you’ll receive a notification about it. * * @param {object} collection - an instantiated and valid kuzzle object * @param {object} [options] - subscription optional configuration * @constructor */ function Room(collection, options) { // Define properties Object.defineProperties(this, { // private properties callback: { value: null, writable: true }, channel: { value: null, writable: true }, id: { value: uuidv4() }, lastRenewal: { value: null, writable: true }, notifier: { value: null, writable: true }, onDoneCB: { value: null, writable: true }, queue: { value: [], writable: true }, // Delay before allowing a subscription renewal renewalDelay: { value: 500 }, scope: { value: options && options.scope ? options.scope : 'all' }, state: { value: options && options.state ? options.state : 'done' }, subscribing: { value: false, writable: true }, users: { value: options && options.users ? options.users : 'none' }, // read-only properties collection: { value: collection, enumerable: true }, kuzzle: { value: collection.kuzzle, enumerable: true }, // writable properties filters: { value: null, enumerable: true, writable: true }, headers: { value: JSON.parse(JSON.stringify(collection.headers)), enumerable: true, writable: true }, volatile: { value: (options && options.volatile) ? options.volatile : {}, enumerable: true, writable: true }, roomId: { value: null, enumerable: true, writable: true }, subscribeToSelf: { value: options && typeof options.subscribeToSelf === 'boolean' ? options.subscribeToSelf : true, enumerable: true, writable: true } }); if (this.kuzzle.bluebird) { return this.kuzzle.bluebird.promisifyAll(this, { suffix: 'Promise', filter: function (name, func, target, passes) { var whitelist = ['count']; return passes && whitelist.indexOf(name) !== -1; } }); } return this; } /** * Returns the number of other subscriptions on that room. * * @param {responseCallback} cb - Handles the query response */ Room.prototype.count = function (cb) { var data; this.kuzzle.callbackRequired('Room.count', cb); data = this.kuzzle.addHeaders({body: {roomId: this.roomId}}, this.headers); if (!isReady.call(this)) { this.queue.push({action: 'count', args: [cb]}); return; } if (!this.roomId) { throw new Error('Room.count: cannot count subscriptions on an inactive room'); } this.kuzzle.query(this.collection.buildQueryArgs('realtime', 'count'), data, function (err, res) { cb(err, res && res.result.count); }); }; /** * Renew the subscription using new filters * * @param {object} [filters] - Filters in Kuzzle DSL format * @param {responseCallback} notificationCB - called for each new notification * @param {responseCallback} [cb] - handles the query response */ Room.prototype.renew = function (filters, notificationCB, cb) { var self = this, now = Date.now(), subscribeQuery = { scope: self.scope, state: self.state, users: self.users }; if (typeof filters === 'function') { cb = notificationCB; notificationCB = filters; filters = null; } if (!cb) { cb = self.onDoneCB; } self.kuzzle.callbackRequired('Room.renew', notificationCB); /* Skip subscription renewal if another one was performed a moment before */ if (self.lastRenewal && (now - self.lastRenewal) <= self.renewalDelay) { return cb && cb(new Error('Subscription already renewed less than ' + self.renewalDelay + 'ms ago')); } if (filters) { self.filters = filters; } /* if not yet connected, register itself to the subscriptions list and wait for the main Kuzzle object to renew once online */ if (self.kuzzle.state !== 'connected') { self.callback = notificationCB; self.onDoneCB = cb; self.kuzzle.subscriptions.pending[self.id] = self; return; } if (self.subscribing) { self.queue.push({action: 'renew', args: [filters, notificationCB, cb]}); return; } self.unsubscribe(); self.roomId = null; self.subscribing = true; self.callback = notificationCB; self.onDoneCB = cb; self.kuzzle.subscriptions.pending[self.id] = self; subscribeQuery.body = self.filters; subscribeQuery = self.kuzzle.addHeaders(subscribeQuery, self.headers); self.kuzzle.query(self.collection.buildQueryArgs('realtime', 'subscribe'), subscribeQuery, {volatile: self.volatile}, function (error, response) { delete self.kuzzle.subscriptions.pending[self.id]; self.subscribing = false; if (error) { self.queue = []; return cb && cb(new Error('Error during Kuzzle subscription: ' + error.message)); } self.lastRenewal = now; self.roomId = response.result.roomId; self.channel = response.result.channel; Eif (!self.kuzzle.subscriptions[self.roomId]) { self.kuzzle.subscriptions[self.roomId] = {}; } self.kuzzle.subscriptions[self.roomId][self.id] = self; self.notifier = notificationCallback.bind(self); self.kuzzle.network.on(self.channel, self.notifier); dequeue.call(self); cb && cb(null, self); }); }; /** * Unsubscribes from Kuzzle. * * Stop listening immediately. If there is no listener left on that room, sends an unsubscribe request to Kuzzle, once * pending subscriptions reaches 0, and only if there is still no listener on that room. * We wait for pending subscriptions to finish to avoid unsubscribing while another subscription on that room is * * @return {*} this */ Room.prototype.unsubscribe = function () { var self = this, room = self.roomId, interval; if (!isReady.call(this)) { self.queue.push({action: 'unsubscribe', args: []}); return self; } if (room) { self.kuzzle.network.off(self.channel, this.notifier); if (Object.keys(self.kuzzle.subscriptions[room]).length === 1) { delete self.kuzzle.subscriptions[room]; if (Object.keys(self.kuzzle.subscriptions.pending).length === 0) { self.kuzzle.query(self.collection.buildQueryArgs('realtime', 'unsubscribe'), {body: {roomId: room}}); } else { interval = setInterval(function () { Eif (Object.keys(self.kuzzle.subscriptions.pending).length === 0) { if (!self.kuzzle.subscriptions[room]) { self.kuzzle.query(self.collection.buildQueryArgs('realtime', 'unsubscribe'), {body: {roomId: room}}); } clearInterval(interval); } }, 100); } } else { delete self.kuzzle.subscriptions[room][self.id]; } self.roomId = null; } return self; }; /** * Helper function allowing to set headers while chaining calls. * * If the replace argument is set to true, replace the current headers with the provided content. * Otherwise, it appends the content to the current headers, only replacing already existing values * * @param content - new headers content * @param [replace] - default: false = append the content. If true: replace the current headers with tj */ Room.prototype.setHeaders = function (content, replace) { this.kuzzle.setHeaders.call(this, content, replace); return this; }; /** * Callback called by the network handler when a message is sent to the subscribed room ID * Calls the registered callback if the notification passes the subscription filters * * @param {object} data - data * @returns {*} */ function notificationCallback (data) { var notificationData = Object.assign({}, data); if (data.type === 'TokenExpired') { this.kuzzle.jwtToken = undefined; return this.kuzzle.emitEvent('tokenExpired'); } if (notificationData.type === 'document') { notificationData.document = new Document(this.collection, notificationData.result._id, notificationData.result._source, notificationData.result._meta); delete notificationData.result; } if (this.subscribeToSelf || !notificationData.volatile || notificationData.volatile.sdkInstanceId !== this.kuzzle.id) { this.callback(null, notificationData); } } /** * Dequeue actions performed while subscription was being renewed */ function dequeue () { var element; while (this.queue.length > 0) { element = this.queue.shift(); this[element.action].apply(this, element.args); } } function isReady() { return this.kuzzle.state === 'connected' && !this.subscribing; } module.exports = Room; |