Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 77 additions & 80 deletions dist/kuzzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,21 +444,6 @@ module.exports = Kuzzle = function (url, options, cb) {
enumerable: true,
writable: true
},
loginStrategy: {
value: (options && typeof options.loginStrategy === 'string') ? options.loginStrategy : undefined,
enumerable: true,
writable: false
},
loginCredentials: {
value: (options && typeof options.loginCredentials === 'object') ? options.loginCredentials : undefined,
enumerable: true,
writable: false
},
loginExpiresIn: {
value: (options && ['number', 'string'].indexOf(typeof options.loginExpiresIn) !== -1) ? options.loginExpiresIn : undefined,
enumerable: true,
writable: false
},
jwtToken: {
value: undefined,
enumerable: true,
Expand Down Expand Up @@ -564,44 +549,18 @@ Kuzzle.prototype.connect = function () {

self.socket.once('connect', function () {
self.state = 'connected';

Object.keys(self.subscriptions).forEach(function (roomId) {
Object.keys(self.subscriptions[roomId]).forEach(function (subscriptionId) {
var subscription = self.subscriptions[roomId][subscriptionId];
subscription.renew(subscription.callback);
});
});

renewAllSubscriptions.call(self);
dequeue.call(self);
emitEvent.call(self, 'connected');

if (self.loginStrategy) {
self.login(self.loginStrategy, self.loginCredentials, self.loginExpiresIn, function(error) {
self.eventListeners.connected.forEach(function (listener) {
listener.fn(error);
});

if (self.connectCB) {
self.connectCB(error, self);
}
});
}
else {
self.eventListeners.connected.forEach(function (listener) {
listener.fn();
});

if (self.connectCB) {
self.connectCB(null, self);
}
if (self.connectCB) {
self.connectCB(null, self);
}
});

self.socket.on('connect_error', function (error) {
self.state = 'error';

self.eventListeners.error.forEach(function (listener) {
listener.fn();
});
emitEvent.call(self, 'error');

if (self.connectCB) {
self.connectCB(error);
Expand All @@ -619,23 +578,15 @@ Kuzzle.prototype.connect = function () {
self.queuing = true;
}

self.eventListeners.disconnected.forEach(function (listener) {
listener.fn();
});
emitEvent.call(self, 'disconnected');
});

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

// renew subscriptions
if (self.autoResubscribe) {
Object.keys(self.subscriptions).forEach(function (roomId) {
Object.keys(self.subscriptions[roomId]).forEach(function (subscriptionId) {
var subscription = self.subscriptions[roomId][subscriptionId];

subscription.renew(subscription.callback);
});
});
renewAllSubscriptions.call(self);
}

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

// alert listeners
self.eventListeners.reconnected.forEach(function (listener) {
listener.fn();
});
emitEvent.call(self, 'reconnected');
});

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

if (typeof cb === 'function') {
cb(null, self);
Expand Down Expand Up @@ -759,9 +709,7 @@ function emitRequest (request, cb) {
if (self.jwtToken !== undefined || cb) {
self.socket.once(request.requestId, function (response) {
if (response.error && response.error.message === 'Token expired') {
self.eventListeners.jwtTokenExpired.forEach(function (listener) {
listener.fn(request, cb);
});
emitEvent.call(self, 'jwtTokenExpired', request, cb);
}

if (cb) {
Expand Down Expand Up @@ -801,6 +749,36 @@ function dequeue () {
}
}

/**
* Renew all registered subscriptions. Triggered either by a successful connection/reconnection or by a
* successful login attempt
*/
function renewAllSubscriptions() {
var self = this;

Object.keys(self.subscriptions).forEach(function (roomId) {
Object.keys(self.subscriptions[roomId]).forEach(function (subscriptionId) {
var subscription = self.subscriptions[roomId][subscriptionId];
subscription.renew(subscription.callback);
});
});
}

/**
* Emits an event to all registered listeners
*
* @param {string} event - name of the target global event
*/
function emitEvent(event) {
var
self = this,
args = Array.prototype.slice.call(arguments, 1);

self.eventListeners[event].forEach(function (listener) {
listener.fn.apply(self, args);
});
}

/**
* Adds a listener to a Kuzzle global event. When an event is fired, listeners are called in the order of their
* insertion.
Expand Down Expand Up @@ -1327,6 +1305,7 @@ Kuzzle.prototype.stopQueuing = function () {
return this;
};


},{"./kuzzleDataCollection":3,"node-uuid":1,"socket.io-client":undefined}],3:[function(require,module,exports){
var
KuzzleDocument = require('./kuzzleDocument'),
Expand Down Expand Up @@ -2529,6 +2508,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
id: {
value: uuid.v4()
},
lastRenewal: {
value: null,
writable: true
},
notifier: {
value: null,
writable: true
Expand All @@ -2537,6 +2520,10 @@ function KuzzleRoom(kuzzleDataCollection, options) {
value: [],
writable: true
},
// Delay before allowing a subscription renewal
renewalDelay: {
value: 500
},
scope: {
value: options && options.scope ? options.scope : 'all'
},
Expand Down Expand Up @@ -2636,6 +2623,7 @@ KuzzleRoom.prototype.count = function (cb) {
*/
KuzzleRoom.prototype.renew = function (filters, cb) {
var
now = Date.now(),
subscribeQuery = {
scope: this.scope,
state: this.state,
Expand All @@ -2648,37 +2636,46 @@ KuzzleRoom.prototype.renew = function (filters, cb) {
filters = null;
}

/*
Skip subscription renewal if another one was performed a moment before
*/
if (self.lastRenewal && (now - self.lastRenewal) <= self.renewalDelay) {
return self;
}

self.lastRenewal = now;

if (filters) {
this.filters = 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 (this.kuzzle.state !== 'connected') {
this.callback = cb;
this.kuzzle.subscriptions.pending[self.id] = self;
return this;
if (self.kuzzle.state !== 'connected') {
self.callback = cb;
self.kuzzle.subscriptions.pending[self.id] = self;
return self;
}

if (this.subscribing) {
this.queue.push({action: 'renew', args: [filters, cb]});
return this;
if (self.subscribing) {
self.queue.push({action: 'renew', args: [filters, cb]});
return self;
}

this.kuzzle.callbackRequired('KuzzleRoom.renew', cb);
self.kuzzle.callbackRequired('KuzzleRoom.renew', cb);

this.unsubscribe();
this.roomId = null;
this.subscribing = true;
this.callback = cb;
this.kuzzle.subscriptions.pending[self.id] = self;
self.unsubscribe();
self.roomId = null;
self.subscribing = true;
self.callback = cb;
self.kuzzle.subscriptions.pending[self.id] = self;

subscribeQuery.body = this.filters;
subscribeQuery = this.kuzzle.addHeaders(subscribeQuery, this.headers);
subscribeQuery.body = self.filters;
subscribeQuery = self.kuzzle.addHeaders(subscribeQuery, this.headers);

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

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

return this;
return self;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions dist/kuzzle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kuzzle.min.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-sdk",
"version": "1.3.8",
"version": "1.3.9",
"description": "Official Javascript SDK for Kuzzle",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
Loading