Skip to content

Commit 03b7d75

Browse files
committed
Merge pull request #47 from kuzzleio/KUZ379_loginResubscribe
Successful logins now renew subscriptions
2 parents b7a39f3 + ff48e34 commit 03b7d75

File tree

9 files changed

+241
-219
lines changed

9 files changed

+241
-219
lines changed

dist/kuzzle.js

Lines changed: 77 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -444,21 +444,6 @@ module.exports = Kuzzle = function (url, options, cb) {
444444
enumerable: true,
445445
writable: true
446446
},
447-
loginStrategy: {
448-
value: (options && typeof options.loginStrategy === 'string') ? options.loginStrategy : undefined,
449-
enumerable: true,
450-
writable: false
451-
},
452-
loginCredentials: {
453-
value: (options && typeof options.loginCredentials === 'object') ? options.loginCredentials : undefined,
454-
enumerable: true,
455-
writable: false
456-
},
457-
loginExpiresIn: {
458-
value: (options && ['number', 'string'].indexOf(typeof options.loginExpiresIn) !== -1) ? options.loginExpiresIn : undefined,
459-
enumerable: true,
460-
writable: false
461-
},
462447
jwtToken: {
463448
value: undefined,
464449
enumerable: true,
@@ -564,44 +549,18 @@ Kuzzle.prototype.connect = function () {
564549

565550
self.socket.once('connect', function () {
566551
self.state = 'connected';
567-
568-
Object.keys(self.subscriptions).forEach(function (roomId) {
569-
Object.keys(self.subscriptions[roomId]).forEach(function (subscriptionId) {
570-
var subscription = self.subscriptions[roomId][subscriptionId];
571-
subscription.renew(subscription.callback);
572-
});
573-
});
574-
552+
renewAllSubscriptions.call(self);
575553
dequeue.call(self);
554+
emitEvent.call(self, 'connected');
576555

577-
if (self.loginStrategy) {
578-
self.login(self.loginStrategy, self.loginCredentials, self.loginExpiresIn, function(error) {
579-
self.eventListeners.connected.forEach(function (listener) {
580-
listener.fn(error);
581-
});
582-
583-
if (self.connectCB) {
584-
self.connectCB(error, self);
585-
}
586-
});
587-
}
588-
else {
589-
self.eventListeners.connected.forEach(function (listener) {
590-
listener.fn();
591-
});
592-
593-
if (self.connectCB) {
594-
self.connectCB(null, self);
595-
}
556+
if (self.connectCB) {
557+
self.connectCB(null, self);
596558
}
597559
});
598560

599561
self.socket.on('connect_error', function (error) {
600562
self.state = 'error';
601-
602-
self.eventListeners.error.forEach(function (listener) {
603-
listener.fn();
604-
});
563+
emitEvent.call(self, 'error');
605564

606565
if (self.connectCB) {
607566
self.connectCB(error);
@@ -619,23 +578,15 @@ Kuzzle.prototype.connect = function () {
619578
self.queuing = true;
620579
}
621580

622-
self.eventListeners.disconnected.forEach(function (listener) {
623-
listener.fn();
624-
});
581+
emitEvent.call(self, 'disconnected');
625582
});
626583

627584
self.socket.on('reconnect', function () {
628585
self.state = 'connected';
629586

630587
// renew subscriptions
631588
if (self.autoResubscribe) {
632-
Object.keys(self.subscriptions).forEach(function (roomId) {
633-
Object.keys(self.subscriptions[roomId]).forEach(function (subscriptionId) {
634-
var subscription = self.subscriptions[roomId][subscriptionId];
635-
636-
subscription.renew(subscription.callback);
637-
});
638-
});
589+
renewAllSubscriptions.call(self);
639590
}
640591

641592
// replay queued requests
@@ -645,9 +596,7 @@ Kuzzle.prototype.connect = function () {
645596
}
646597

647598
// alert listeners
648-
self.eventListeners.reconnected.forEach(function (listener) {
649-
listener.fn();
650-
});
599+
emitEvent.call(self, 'reconnected');
651600
});
652601

653602
return this;
@@ -677,6 +626,7 @@ Kuzzle.prototype.login = function (strategy, credentials, expiresIn, cb) {
677626
this.query({controller: 'auth', action: 'login'}, {body: request}, {}, function(error, response) {
678627
if (error === null) {
679628
self.jwtToken = response.jwt;
629+
renewAllSubscriptions.call(self);
680630

681631
if (typeof cb === 'function') {
682632
cb(null, self);
@@ -759,9 +709,7 @@ function emitRequest (request, cb) {
759709
if (self.jwtToken !== undefined || cb) {
760710
self.socket.once(request.requestId, function (response) {
761711
if (response.error && response.error.message === 'Token expired') {
762-
self.eventListeners.jwtTokenExpired.forEach(function (listener) {
763-
listener.fn(request, cb);
764-
});
712+
emitEvent.call(self, 'jwtTokenExpired', request, cb);
765713
}
766714

767715
if (cb) {
@@ -801,6 +749,36 @@ function dequeue () {
801749
}
802750
}
803751

752+
/**
753+
* Renew all registered subscriptions. Triggered either by a successful connection/reconnection or by a
754+
* successful login attempt
755+
*/
756+
function renewAllSubscriptions() {
757+
var self = this;
758+
759+
Object.keys(self.subscriptions).forEach(function (roomId) {
760+
Object.keys(self.subscriptions[roomId]).forEach(function (subscriptionId) {
761+
var subscription = self.subscriptions[roomId][subscriptionId];
762+
subscription.renew(subscription.callback);
763+
});
764+
});
765+
}
766+
767+
/**
768+
* Emits an event to all registered listeners
769+
*
770+
* @param {string} event - name of the target global event
771+
*/
772+
function emitEvent(event) {
773+
var
774+
self = this,
775+
args = Array.prototype.slice.call(arguments, 1);
776+
777+
self.eventListeners[event].forEach(function (listener) {
778+
listener.fn.apply(self, args);
779+
});
780+
}
781+
804782
/**
805783
* Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their
806784
* insertion.
@@ -1327,6 +1305,7 @@ Kuzzle.prototype.stopQueuing = function () {
13271305
return this;
13281306
};
13291307

1308+
13301309
},{"./kuzzleDataCollection":3,"node-uuid":1,"socket.io-client":undefined}],3:[function(require,module,exports){
13311310
var
13321311
KuzzleDocument = require('./kuzzleDocument'),
@@ -2529,6 +2508,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
25292508
id: {
25302509
value: uuid.v4()
25312510
},
2511+
lastRenewal: {
2512+
value: null,
2513+
writable: true
2514+
},
25322515
notifier: {
25332516
value: null,
25342517
writable: true
@@ -2537,6 +2520,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
25372520
value: [],
25382521
writable: true
25392522
},
2523+
// Delay before allowing a subscription renewal
2524+
renewalDelay: {
2525+
value: 500
2526+
},
25402527
scope: {
25412528
value: options && options.scope ? options.scope : 'all'
25422529
},
@@ -2636,6 +2623,7 @@ KuzzleRoom.prototype.count = function (cb) {
26362623
*/
26372624
KuzzleRoom.prototype.renew = function (filters, cb) {
26382625
var
2626+
now = Date.now(),
26392627
subscribeQuery = {
26402628
scope: this.scope,
26412629
state: this.state,
@@ -2648,37 +2636,46 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
26482636
filters = null;
26492637
}
26502638

2639+
/*
2640+
Skip subscription renewal if another one was performed a moment before
2641+
*/
2642+
if (self.lastRenewal && (now - self.lastRenewal) <= self.renewalDelay) {
2643+
return self;
2644+
}
2645+
2646+
self.lastRenewal = now;
2647+
26512648
if (filters) {
2652-
this.filters = filters;
2649+
self.filters = filters;
26532650
}
26542651

26552652
/*
26562653
if not yet connected, register itself to the subscriptions list and wait for the
26572654
main Kuzzle object to renew once online
26582655
*/
2659-
if (this.kuzzle.state !== 'connected') {
2660-
this.callback = cb;
2661-
this.kuzzle.subscriptions.pending[self.id] = self;
2662-
return this;
2656+
if (self.kuzzle.state !== 'connected') {
2657+
self.callback = cb;
2658+
self.kuzzle.subscriptions.pending[self.id] = self;
2659+
return self;
26632660
}
26642661

2665-
if (this.subscribing) {
2666-
this.queue.push({action: 'renew', args: [filters, cb]});
2667-
return this;
2662+
if (self.subscribing) {
2663+
self.queue.push({action: 'renew', args: [filters, cb]});
2664+
return self;
26682665
}
26692666

2670-
this.kuzzle.callbackRequired('KuzzleRoom.renew', cb);
2667+
self.kuzzle.callbackRequired('KuzzleRoom.renew', cb);
26712668

2672-
this.unsubscribe();
2673-
this.roomId = null;
2674-
this.subscribing = true;
2675-
this.callback = cb;
2676-
this.kuzzle.subscriptions.pending[self.id] = self;
2669+
self.unsubscribe();
2670+
self.roomId = null;
2671+
self.subscribing = true;
2672+
self.callback = cb;
2673+
self.kuzzle.subscriptions.pending[self.id] = self;
26772674

2678-
subscribeQuery.body = this.filters;
2679-
subscribeQuery = this.kuzzle.addHeaders(subscribeQuery, this.headers);
2675+
subscribeQuery.body = self.filters;
2676+
subscribeQuery = self.kuzzle.addHeaders(subscribeQuery, this.headers);
26802677

2681-
self.kuzzle.query(this.collection.buildQueryArgs('subscribe', 'on'), subscribeQuery, {metadata: this.metadata}, function (error, response) {
2678+
self.kuzzle.query(self.collection.buildQueryArgs('subscribe', 'on'), subscribeQuery, {metadata: self.metadata}, function (error, response) {
26822679
delete self.kuzzle.subscriptions.pending[self.id];
26832680
self.subscribing = false;
26842681

@@ -2702,7 +2699,7 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
27022699
dequeue.call(self);
27032700
});
27042701

2705-
return this;
2702+
return self;
27062703
};
27072704

27082705
/**

dist/kuzzle.min.js

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

dist/kuzzle.min.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.3.8",
3+
"version": "1.3.9",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

0 commit comments

Comments
 (0)