Skip to content

Commit 60786d0

Browse files
committed
upgraded to match Kuzzle v0.5.6 specifications
1 parent cd45a8c commit 60786d0

File tree

7 files changed

+129
-162
lines changed

7 files changed

+129
-162
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/kuzzle.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ module.exports = Kuzzle = function (url, options, cb) {
4242
},
4343
eventListeners: {
4444
value: {
45-
subscribed: [],
46-
unsubscribed: [],
45+
connected: [],
46+
error: [],
4747
disconnected: [],
4848
reconnected: []
4949
}
@@ -261,6 +261,10 @@ Kuzzle.prototype.connect = function (cb) {
261261

262262
dequeue.call(self);
263263

264+
self.eventListeners.connected.forEach(function (listener) {
265+
listener.fn();
266+
});
267+
264268
if (cb) {
265269
cb(null, self);
266270
}
@@ -269,6 +273,10 @@ Kuzzle.prototype.connect = function (cb) {
269273
self.socket.on('connect_error', function (error) {
270274
self.state = 'error';
271275

276+
self.eventListeners.error.forEach(function (listener) {
277+
listener.fn();
278+
});
279+
272280
if (cb) {
273281
cb(error);
274282
}
@@ -443,20 +451,11 @@ Kuzzle.prototype.getAllStatistics = function (options, cb) {
443451
this.callbackRequired('Kuzzle.getAllStatistics', cb);
444452

445453
this.query(null, 'admin', 'getAllStats', {}, options, function (err, res) {
446-
var result = [];
447-
448454
if (err) {
449455
return cb(err);
450456
}
451457

452-
Object.keys(res.statistics).forEach(function (key) {
453-
var frame = res.statistics[key];
454-
frame.timestamp = key;
455-
456-
result.push(frame);
457-
});
458-
459-
cb(null, result);
458+
cb(null, res.statistics);
460459
});
461460

462461
return this;
@@ -492,18 +491,15 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
492491
}
493492

494493
queryCB = function (err, res) {
495-
var stats = [];
496-
497494
if (err) {
498495
return cb(err);
499496
}
500497

501-
Object.keys(res.statistics).forEach(function (frame) {
502-
res.statistics[frame].timestamp = frame;
503-
stats.push(res.statistics[frame]);
504-
});
505-
506-
cb(null, stats);
498+
if (timestamp) {
499+
cb(null, res.statistics);
500+
} else {
501+
cb(null, [res.statistics]);
502+
}
507503
};
508504

509505
this.callbackRequired('Kuzzle.getStatistics', cb);

src/kuzzleRoom.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
2929
value: null,
3030
writable: true
3131
},
32+
channel: {
33+
value: null,
34+
writable: true
35+
},
3236
id: {
3337
value: uuid.v4()
3438
},
@@ -40,10 +44,19 @@ function KuzzleRoom(kuzzleDataCollection, options) {
4044
value: [],
4145
writable: true
4246
},
47+
scope: {
48+
value: options && options.scope ? options.scope : 'all'
49+
},
50+
state: {
51+
value: options && options.state ? options.state : 'done'
52+
},
4353
subscribing: {
4454
value: false,
4555
writable: true
4656
},
57+
users: {
58+
value: options && options.users ? options.users : 'none'
59+
},
4760
// read-only properties
4861
collection: {
4962
value: kuzzleDataCollection.collection,
@@ -140,7 +153,11 @@ KuzzleRoom.prototype.count = function (cb) {
140153
*/
141154
KuzzleRoom.prototype.renew = function (filters, cb) {
142155
var
143-
subscribeQuery,
156+
subscribeQuery = {
157+
scope: this.scope,
158+
state: this.state,
159+
users: this.users
160+
},
144161
self = this;
145162

146163
if (!cb && filters && typeof filters === 'function') {
@@ -156,17 +173,17 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
156173
this.kuzzle.callbackRequired('KuzzleRoom.renew', cb);
157174

158175
this.unsubscribe();
176+
this.roomId = null;
159177
this.subscribing = true;
160-
self.kuzzle.subscriptions.pending[self.id] = self;
178+
this.callback = cb;
179+
this.kuzzle.subscriptions.pending[self.id] = self;
161180

162181
if (filters) {
163182
this.filters = filters;
164183
}
165184

166-
this.roomId = null;
167-
this.callback = cb;
168-
169-
subscribeQuery = this.kuzzle.addHeaders({body: self.filters}, this.headers);
185+
subscribeQuery.body = this.filters;
186+
subscribeQuery = this.kuzzle.addHeaders(subscribeQuery, this.headers);
170187

171188
self.kuzzle.query(this.collection, 'subscribe', 'on', subscribeQuery, {metadata: this.metadata}, function (error, response) {
172189
delete self.kuzzle.subscriptions.pending[self.id];
@@ -178,6 +195,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
178195
}
179196

180197
self.roomId = response.roomId;
198+
self.channel = response.channel;
181199

182200
if (!self.kuzzle.subscriptions[self.roomId]) {
183201
self.kuzzle.subscriptions[self.roomId] = {};
@@ -186,7 +204,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
186204
self.kuzzle.subscriptions[self.roomId][self.id] = self;
187205

188206
self.notifier = notificationCallback.bind(self);
189-
self.kuzzle.socket.on(self.roomId, self.notifier);
207+
self.kuzzle.socket.on(self.channel, self.notifier);
190208

191209
dequeue.call(self);
192210
});
@@ -215,7 +233,7 @@ KuzzleRoom.prototype.unsubscribe = function () {
215233
}
216234

217235
if (room) {
218-
self.kuzzle.socket.off(room, this.notifier);
236+
self.kuzzle.socket.off(self.channel, this.notifier);
219237

220238
if (Object.keys(self.kuzzle.subscriptions[room]).length === 1) {
221239
delete self.kuzzle.subscriptions[room];
@@ -264,40 +282,17 @@ KuzzleRoom.prototype.setHeaders = function (content, replace) {
264282
* @returns {*}
265283
*/
266284
function notificationCallback (data) {
267-
var
268-
self = this,
269-
globalEvent,
270-
listening;
271-
272285
if (data.error) {
273-
return self.callback(data.error);
286+
return this.callback(data.error);
274287
}
275288

276-
if (data.result.action === 'on' || data.result.action === 'off') {
277-
if (data.result.action === 'on') {
278-
globalEvent = 'subscribed';
279-
listening = self.listenToConnections;
280-
} else {
281-
globalEvent = 'unsubscribed';
282-
listening = self.listenToDisconnections;
283-
}
284-
285-
if (listening || self.kuzzle.eventListeners[globalEvent].length > 0) {
286-
if (listening) {
287-
self.callback(null, data.result);
288-
}
289-
290-
self.kuzzle.eventListeners[globalEvent].forEach(function (listener) {
291-
listener.fn(self.subscriptionId, data.result);
292-
});
293-
}
294-
} else if (self.kuzzle.requestHistory[data.result.requestId]) {
295-
if (self.subscribeToSelf) {
296-
self.callback(null, data.result);
289+
if (this.kuzzle.requestHistory[data.result.requestId]) {
290+
if (this.subscribeToSelf) {
291+
this.callback(null, data.result);
297292
}
298-
delete self.kuzzle.requestHistory[data.result.requestId];
293+
delete this.kuzzle.requestHistory[data.result.requestId];
299294
} else {
300-
self.callback(null, data.result);
295+
this.callback(null, data.result);
301296
}
302297
}
303298

test/kuzzle/constructor.test.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,27 @@ describe('Kuzzle constructor', () => {
207207
kuzzle.state = state;
208208
should(kuzzle.connect()).be.exactly(kuzzle);
209209
should(kuzzle.state).be.exactly('connecting');
210+
kuzzle.socket.removeAllListeners();
210211
});
211212
});
212213
});
213214

215+
it('should registered listeners upon receiving a connect event', function (done) {
216+
var
217+
kuzzle = new Kuzzle('nowhere', {connect: 'manual'}),
218+
listenerCalled = false;
219+
220+
kuzzle.state = 'initializing';
221+
kuzzle.addListener('connected', function () { listenerCalled = true; });
222+
kuzzle.connect();
223+
224+
setTimeout(() => {
225+
should(listenerCalled).be.true();
226+
kuzzle.socket.removeAllListeners();
227+
done();
228+
}, 10);
229+
});
230+
214231
describe('=> on connection error', () => {
215232
var
216233
iostub = function () {
@@ -230,6 +247,7 @@ describe('Kuzzle constructor', () => {
230247
should(err).be.exactly('error');
231248
should(res).be.undefined();
232249
should(kuzzle.state).be.exactly('error');
250+
kuzzle.socket.removeAllListeners();
233251
done();
234252
}
235253
catch (e) {
@@ -238,6 +256,27 @@ describe('Kuzzle constructor', () => {
238256
});
239257
});
240258
});
259+
260+
it('should registered listeners upon receiving a error event', function (done) {
261+
Kuzzle.__with__({
262+
io: iostub
263+
})(function () {
264+
var kuzzle = new Kuzzle('nowhere');
265+
266+
kuzzle.addListener('error', function () { listenerCalled = true; });
267+
268+
setTimeout(() => {
269+
try {
270+
should(listenerCalled).be.true();
271+
kuzzle.socket.removeAllListeners();
272+
done();
273+
}
274+
catch (e) {
275+
done(e);
276+
}
277+
}, 10);
278+
});
279+
});
241280
});
242281

243282
describe('=> on connection success', () => {
@@ -254,11 +293,12 @@ describe('Kuzzle constructor', () => {
254293
Kuzzle.__with__({
255294
io: iostub
256295
})(function () {
257-
new Kuzzle('nowhere', function (err, res) {
296+
var kuzzle = new Kuzzle('nowhere', function (err, res) {
258297
try {
259298
should(err).be.null();
260299
should(res).be.instanceof(Kuzzle);
261300
should(res.state).be.exactly('connected');
301+
kuzzle.socket.removeAllListeners();
262302
done();
263303
}
264304
catch (e) {
@@ -269,12 +309,14 @@ describe('Kuzzle constructor', () => {
269309
});
270310

271311
it('should renew subscriptions automatically on a connection success', function (done) {
272-
var renewed = false;
312+
var
313+
kuzzle,
314+
renewed = false;
273315

274316
this.timeout(50);
275317

276318
Kuzzle.__with__({io: iostub})(function () {
277-
var kuzzle = new Kuzzle('nowhere', {connect: 'manual', autoResubscribe: false});
319+
kuzzle = new Kuzzle('nowhere', {connect: 'manual', autoResubscribe: false});
278320

279321
kuzzle.subscriptions['foo'] = {
280322
bar: {
@@ -288,6 +330,7 @@ describe('Kuzzle constructor', () => {
288330

289331
setTimeout(() => {
290332
should(renewed).be.true();
333+
kuzzle.socket.removeAllListeners();
291334
done();
292335
}, 20);
293336
});
@@ -308,6 +351,7 @@ describe('Kuzzle constructor', () => {
308351
kuzzle.connect(() => {
309352
should(kuzzle.state).be.exactly('connected');
310353
should(dequeued).be.true();
354+
kuzzle.socket.removeAllListeners();
311355
revert();
312356
done();
313357
});
@@ -349,6 +393,7 @@ describe('Kuzzle constructor', () => {
349393
should(kuzzle.queuing).be.false();
350394
should(listenerCalled).be.true();
351395
kuzzle.isValid();
396+
kuzzle.socket.removeAllListeners();
352397
done();
353398
}
354399
catch (e) {
@@ -366,6 +411,7 @@ describe('Kuzzle constructor', () => {
366411
should(kuzzle.state).be.exactly('offline');
367412
should(kuzzle.queuing).be.true();
368413
kuzzle.isValid();
414+
kuzzle.socket.removeAllListeners();
369415
done();
370416
}
371417
catch (e) {
@@ -384,6 +430,7 @@ describe('Kuzzle constructor', () => {
384430
should(kuzzle.state).be.exactly('offline');
385431
should(kuzzle.queuing).be.false();
386432
kuzzle.isValid();
433+
kuzzle.socket.removeAllListeners();
387434
done('the kuzzle instance should have been invalidated');
388435
}
389436
catch (e) {
@@ -422,6 +469,7 @@ describe('Kuzzle constructor', () => {
422469
// should not switch queuing to 'false' automatically by default
423470
should(kuzzle.queuing).be.true();
424471
kuzzle.isValid();
472+
kuzzle.socket.removeAllListeners();
425473
done();
426474
}
427475
catch (e) {
@@ -448,6 +496,7 @@ describe('Kuzzle constructor', () => {
448496
should(kuzzle.state).be.exactly('connected');
449497
should(renewCalled).be.true();
450498
kuzzle.isValid();
499+
kuzzle.socket.removeAllListeners();
451500
done();
452501
}
453502
catch (e) {
@@ -476,6 +525,7 @@ describe('Kuzzle constructor', () => {
476525
should(kuzzle.state).be.exactly('connected');
477526
should(renewCalled).be.false();
478527
kuzzle.isValid();
528+
kuzzle.socket.removeAllListeners();
479529
done();
480530
}
481531
catch (e) {
@@ -497,6 +547,7 @@ describe('Kuzzle constructor', () => {
497547
should(kuzzle.state).be.exactly('connected');
498548
should(kuzzle.queuing).be.false();
499549
kuzzle.isValid();
550+
kuzzle.socket.removeAllListeners();
500551
done();
501552
}
502553
catch (e) {

0 commit comments

Comments
 (0)