Skip to content

Commit da60d4c

Browse files
committed
Merge pull request #27 from kuzzleio/KUZ-305-update
upgraded to match Kuzzle v0.5.6 specifications
2 parents 12c357a + 9b00b68 commit da60d4c

File tree

9 files changed

+146
-165
lines changed

9 files changed

+146
-165
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ sudo: false
22
language: node_js
33
node_js:
44
- '4.2'
5+
env:
6+
- CXX=g++-4.8
7+
addons:
8+
apt:
9+
sources:
10+
- ubuntu-toolchain-r-test
11+
packages:
12+
- g++-4.8
513
script:
614
- grunt && npm test && cat ./coverage/lcov.info | ./node_modules/.bin/codecov
715
before_install: npm install -g grunt-cli

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
[![Build Status](https://travis-ci.org/kuzzleio/sdk-javascript.svg?branch=master)](https://travis-ci.org/kuzzleio/sdk-javascript) [![codecov.io](http://codecov.io/github/kuzzleio/sdk-javascript/coverage.svg?branch=master)](http://codecov.io/github/kuzzleio/sdk-javascript?branch=master) [![Dependency Status](https://david-dm.org/kuzzleio/sdk-javascript.svg)](https://david-dm.org/kuzzleio/sdk-javascript)
22

33

4-
Kuzzle
4+
Official Kuzzle Javascript SDK
55
======
66

7+
:warning: This SDK version requires Kuzzle v0.6.0 or higher.
8+
79
## About Kuzzle
810

911
For UI and linked objects developers, Kuzzle is an open-source solution that handles all the data management (CRUD, real-time storage, search, high-level features, etc).
@@ -86,7 +88,7 @@ kuzzle
8688

8789
### HTML
8890

89-
#### Vanilla
91+
#### Using automatic builds
9092

9193
Download the file [kuzzle.min.js](https://raw.githubusercontent.com/kuzzleio/sdk-javascript/browser/kuzzle.min.js) available in the [browser](https://github.com/kuzzleio/sdk-javascript/tree/browser) branch of this project.
9294

@@ -120,6 +122,10 @@ kuzzle
120122
});
121123
```
122124

125+
#### Building manually
126+
127+
Clone this github repository and run ``grunt``. A ``browser`` directory will be created, containing a plain browserified version of this SDK, and a minified version.
128+
123129
# License
124130

125131
[Apache 2](LICENSE.md)

package.json

Lines changed: 2 additions & 2 deletions
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": {
@@ -34,7 +34,7 @@
3434
"grunt": "0.4.5",
3535
"grunt-browserify": "^4.0.1",
3636
"grunt-contrib-jshint": "^0.11.3",
37-
"grunt-contrib-uglify": "0.10.1",
37+
"grunt-contrib-uglify": "0.11.0",
3838
"gruntify-eslint": "^1.0.1",
3939
"istanbul": "0.4.0",
4040
"istanbul-middleware": "0.2.1",

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

0 commit comments

Comments
 (0)