Skip to content

Commit 622466d

Browse files
scottinetGilles Ballini
authored andcommitted
V5: Option "subscribeToSelf" using volatile data (#268)
* use volatiles instead of request ids to track notifications from our own actions * update dist build
1 parent c337b84 commit 622466d

File tree

7 files changed

+26
-105
lines changed

7 files changed

+26
-105
lines changed

dist/kuzzle.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/kuzzle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "5.0.10",
3+
"version": "5.0.11",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {
@@ -54,4 +54,4 @@
5454
"engines": {
5555
"node": ">= 6.9.1"
5656
}
57-
}
57+
}

src/Kuzzle.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ function Kuzzle (host, options, cb) {
4242

4343
Object.defineProperties(this, {
4444
// 'private' properties
45-
cleanHistoryTimer: {
46-
value: null,
47-
writable: true
45+
id: {
46+
value: uuidv4()
4847
},
4948
collections: {
5049
value: {},
@@ -72,10 +71,6 @@ function Kuzzle (host, options, cb) {
7271
value: false,
7372
writable: true
7473
},
75-
requestHistory: {
76-
value: {},
77-
writable: true
78-
},
7974
state: {
8075
value: 'initializing',
8176
writable: true
@@ -293,8 +288,6 @@ function Kuzzle (host, options, cb) {
293288
this.state = 'ready';
294289
}
295290

296-
this.cleanHistoryTimer = setInterval(function () { cleanHistory(self.requestHistory); }, 1000);
297-
298291
if (this.bluebird) {
299292
return this.bluebird.promisifyAll(this, {
300293
suffix: 'Promise',
@@ -838,21 +831,6 @@ function cleanQueue () {
838831
}
839832
}
840833

841-
842-
/**
843-
* Clean history from requests made more than 10s ago
844-
*/
845-
function cleanHistory (requestHistory) {
846-
var
847-
now = Date.now();
848-
849-
Object.keys(requestHistory).forEach(function (key) {
850-
if (requestHistory[key] < now - 10000) {
851-
delete requestHistory[key];
852-
}
853-
});
854-
}
855-
856834
/**
857835
* Emit a request to Kuzzle
858836
*
@@ -886,9 +864,6 @@ function emitRequest (request, cb) {
886864
}
887865

888866
this.network.send(request);
889-
890-
// Track requests made to allow Room.subscribeToSelf to work
891-
self.requestHistory[request.requestId] = Date.now();
892867
}
893868

894869
/**
@@ -1162,7 +1137,6 @@ Kuzzle.prototype.listIndexes = function (options, cb) {
11621137
Kuzzle.prototype.disconnect = function () {
11631138
var collection;
11641139

1165-
clearInterval(this.cleanHistoryTimer);
11661140
this.state = 'disconnected';
11671141
this.network.close();
11681142
this.network = null;
@@ -1439,6 +1413,7 @@ Kuzzle.prototype.query = function (queryArgs, query, options, cb) {
14391413
}
14401414

14411415
object.volatile.sdkVersion = this.sdkVersion;
1416+
object.volatile.sdkInstanceId = this.id;
14421417

14431418
if (self.state === 'connected' || (options && options.queuable === false)) {
14441419
if (self.state === 'connected') {

src/Room.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,7 @@ function notificationCallback (data) {
321321
delete data.result;
322322
}
323323

324-
if (this.kuzzle.requestHistory[data.requestId]) {
325-
if (this.subscribeToSelf) {
326-
this.callback(null, data);
327-
}
328-
delete this.kuzzle.requestHistory[data.requestId];
329-
} else {
324+
if (this.subscribeToSelf || !data.volatile || data.volatile.sdkInstanceId !== this.kuzzle.id) {
330325
this.callback(null, data);
331326
}
332327
}

test/Room/methods.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -416,23 +416,23 @@ describe('Room methods', function () {
416416
});
417417
});
418418

419-
it('should delete the result from history if emitted by this instance', function () {
420-
room.subscribeToSelf = true;
421-
kuzzle.requestHistory.bar = {};
422-
notifCB.call(room, {type: 'document', result: {}, action: 'foo', requestId: 'bar'});
423-
424-
should(room.callback)
425-
.be.calledOnce();
426-
should(kuzzle.requestHistory).be.empty();
427-
});
428-
429419
it('should not forward the message if subscribeToSelf is false and the response comes from a query emitted by this instance', function () {
430420
room.subscribeToSelf = false;
431-
kuzzle.requestHistory.bar = {};
432-
notifCB.call(room, {type: 'document', result: {}, requestId: 'bar', action: 'foo'});
433421

422+
notifCB.call(room, {type: 'document', result: {}, action: 'foo', volatile: {sdkInstanceId: kuzzle.id}});
434423
should(room.callback).not.be.called();
435-
should(kuzzle.requestHistory).be.empty();
424+
425+
room.callback.resetHistory();
426+
notifCB.call(room, {type: 'document', result: {}, action: 'foo', volatile: {sdkInstanceId: 'foobar'}});
427+
should(room.callback).be.calledOnce();
428+
429+
room.callback.resetHistory();
430+
notifCB.call(room, {type: 'document', result: {}, action: 'foo', volatile: {barfoo: 'foobar'}});
431+
should(room.callback).be.calledOnce();
432+
433+
room.callback.resetHistory();
434+
notifCB.call(room, {type: 'document', result: {}, action: 'foo'});
435+
should(room.callback).be.calledOnce();
436436
});
437437

438438
it('should fire a "tokenExpired" event when receiving a TokenExpired notification', function () {

test/kuzzle/query.test.js

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ describe('Query management', function () {
1818

1919
it('should emit the request when asked to', function () {
2020
var
21-
start = Date.now(),
2221
request = {requestId: 'bar'},
2322
spy = sinon.spy(kuzzle.network, 'send');
2423

2524
emitRequest.call(kuzzle, request);
2625
should(spy).be.calledWithMatch(request);
27-
should(kuzzle.requestHistory.bar).be.within(start, Date.now());
2826
});
2927

3028
it('should trigger a tokenExpired event if the token has expired', function (done) {
@@ -123,7 +121,7 @@ describe('Query management', function () {
123121
collection: 'collection',
124122
controller: 'controller',
125123
index: 'index',
126-
volatile: { sdkVersion: kuzzle.sdkVersion },
124+
volatile: { sdkInstanceId: kuzzle.id, sdkVersion: kuzzle.sdkVersion },
127125
requestId: sinon.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
128126
});
129127
});
@@ -139,7 +137,7 @@ describe('Query management', function () {
139137
collection: 'collection',
140138
controller: 'controller',
141139
index: 'index',
142-
volatile: { sdkVersion: kuzzle.sdkVersion },
140+
volatile: { sdkInstanceId: kuzzle.id, sdkVersion: kuzzle.sdkVersion },
143141
requestId: sinon.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i)
144142
}, sinon.match(function(f) {return f === cb;}));
145143
});
@@ -378,51 +376,4 @@ describe('Query management', function () {
378376
should(kuzzle.offlineQueue[1].query.jwt).be.undefined();
379377
});
380378
});
381-
382-
describe('#cleanHistory', function () {
383-
it('should be started by kuzzle constructor', function () {
384-
var
385-
cleanStub = sinon.stub(),
386-
clock = sinon.useFakeTimers(),
387-
kuzzle;
388-
389-
// we need to re-import so that fake timers can take effect
390-
Kuzzle = rewire('../../src/Kuzzle');
391-
Kuzzle.__set__('cleanHistory', cleanStub);
392-
kuzzle = new Kuzzle('foo', {connect: 'manual'});
393-
394-
clock.tick(1000);
395-
396-
should(kuzzle.cleanHistoryTimer).be.not.null();
397-
should(cleanStub.calledOnce).be.true();
398-
399-
clock.restore();
400-
});
401-
402-
it('should clean oldest entries every 1s', function () {
403-
var
404-
i,
405-
clock = sinon.useFakeTimers(),
406-
kuzzle;
407-
408-
Kuzzle = rewire('../../src/Kuzzle');
409-
kuzzle = new Kuzzle('foo', {connect: 'manual'});
410-
411-
for (i = 100000; i >= 0; i -= 10000) {
412-
kuzzle.requestHistory[i] = -i;
413-
}
414-
415-
clock.tick(1000);
416-
417-
// should only contains i == 0 entry
418-
should(Object.keys(kuzzle.requestHistory)).match(['0']);
419-
420-
kuzzle.requestHistory.foobar = -100000;
421-
422-
clock.tick(1000);
423-
should(Object.keys(kuzzle.requestHistory)).match(['0']);
424-
425-
clock.restore();
426-
});
427-
});
428379
});

0 commit comments

Comments
 (0)