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
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ hooks:
curl -XDELETE kuzzle:7512/profiles/profile${i}
done
template: default
expected: '^ policies: \[ \[Object\] \] } \]$'
expected: '.*Profile.*_id.*rateLimit.*policies.*'
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ hooks:
curl -XDELETE kuzzle:7512/roles/role${i}
done
template: default
expected: '^ _id: ''role3'',$'
expected: '.*Role.*_id.*controllers.*'
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ hooks:
curl -XDELETE kuzzle:7512/users/user${i}
done
template: default
expected: '^ _id: ''user3'',$'
expected: '.*User.*'
5 changes: 4 additions & 1 deletion src/controllers/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class BaseController {
* @param {string} name - Controller full name for API request.
*/
constructor (kuzzle, name) {
this._kuzzle = kuzzle;
Reflect.defineProperty(this, '_kuzzle', {
value: kuzzle
});

this._name = name;
}

Expand Down
5 changes: 0 additions & 5 deletions src/controllers/MemoryStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ for (const action of Object.keys(commands)) {
for (const opt of command.opts) {
if (options[opt] !== null && options[opt] !== undefined) {
assignParameter(request, command.getter, opt, options[opt]);
delete options[opt];
}
}
}
Expand Down Expand Up @@ -296,7 +295,6 @@ function assignGeoRadiusOptions(data, options) {
.forEach(function (opt) {
if (opt === 'withcoord' || opt === 'withdist') {
parsed.push(opt);
delete options[opt];
}
else if (opt === 'count' || opt === 'sort') {
if (opt === 'count') {
Expand All @@ -305,8 +303,6 @@ function assignGeoRadiusOptions(data, options) {

parsed.push(options[opt]);
}

delete options[opt];
});

if (parsed.length > 0) {
Expand All @@ -327,7 +323,6 @@ function assignZrangeOptions (data, options) {

if (options.limit) {
data.limit = options.limit;
delete options.limit;
}
}

Expand Down
74 changes: 39 additions & 35 deletions src/controllers/Realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class RealTimeController extends BaseController {
constructor (kuzzle) {
super(kuzzle, 'realtime');

this.subscriptions = {};
this.subscriptionsOff = {};
this.subscriptions = new Map();
this.subscriptionsOff = new Map();

this.kuzzle.on('tokenExpired', () => this.tokenExpired());
}
Expand All @@ -28,7 +28,8 @@ class RealTimeController extends BaseController {
index,
collection,
body: message,
action: 'publish'
action: 'publish',
_id: options._id
};

return this.query(request, options)
Expand All @@ -40,50 +41,51 @@ class RealTimeController extends BaseController {

return room.subscribe()
.then(() => {
if (!this.subscriptions[room.id]) {
this.subscriptions[room.id] = [];
if (!this.subscriptions.has(room.id)) {
this.subscriptions.set(room.id, []);
}
this.subscriptions[room.id].push(room);
this.subscriptions.get(room.id).push(room);
return room.id;
});
}

unsubscribe (roomId, options = {}) {
const rooms = this.subscriptions[roomId];
const request = {
action: 'unsubscribe',
body: { roomId }
};

if (!rooms) {
return Promise.reject(new Error(`not subscribed to ${roomId}`));
}
return this.query(request, options)
.then(response => {
const rooms = this.subscriptions.get(roomId);

for (const room of rooms) {
room.removeListeners();
}
delete this.subscriptions[roomId];
if (rooms) {
for (const room of rooms) {
room.removeListeners();
}

this.subscriptions.delete(roomId);
}

return this.query({
action: 'unsubscribe',
body: {roomId}
}, options)
.then(response => {
return response.result;
});
}

// called on network error or disconnection
disconnected () {
for (const roomId of Object.keys(this.subscriptions)) {
for (const room of this.subscriptions[roomId]) {
for (const roomId of this.subscriptions.keys()) {
for (const room of this.subscriptions.get(roomId)) {
room.removeListeners();

if (room.autoResubscribe) {
if (!this.subscriptionsOff[roomId]) {
this.subscriptionsOff[roomId] = [];
if (!this.subscriptionsOff.has(roomId)) {
this.subscriptionsOff.set(roomId, []);
}
this.subscriptionsOff[roomId].push(room);
this.subscriptionsOff.get(roomId).push(room);
}
}

delete this.subscriptions[roomId];
this.subscriptions.delete(roomId);
}
}

Expand All @@ -92,31 +94,33 @@ class RealTimeController extends BaseController {
* Resubscribe to eligible disabled rooms.
*/
reconnected () {
for (const roomId of Object.keys(this.subscriptionsOff)) {
for (const room of this.subscriptionsOff[roomId]) {
if (!this.subscriptions[roomId]) {
this.subscriptions[roomId] = [];
for (const roomId of this.subscriptionsOff.keys()) {
for (const room of this.subscriptionsOff.get(roomId)) {
if (!this.subscriptions.has(roomId)) {
this.subscriptions.set(roomId, []);
}
this.subscriptions[roomId].push(room);
this.subscriptions.get(roomId).push(room);

room.subscribe()
.catch(() => this.kuzzle.emit('discarded', {request: room.request}));
}

delete this.subscriptionsOff[roomId];
this.subscriptionsOff.delete(roomId);
}
}

/**
* Removes all subscriptions.
*/
tokenExpired() {
for (const roomId of Object.keys(this.subscriptions)) {
this.subscriptions[roomId].forEach(room => room.removeListeners());
for (const roomId of this.subscriptions.keys()) {
for (const room of this.subscriptions.get(roomId)) {
room.removeListeners();
}
}

this.subscriptions = {};
this.subscriptionsOff = {};
this.subscriptions = new Map();
this.subscriptionsOff = new Map();
}

}
Expand Down
1 change: 0 additions & 1 deletion src/controllers/Security.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class SecurityController extends BaseController {
action: 'createFirstAdmin',
reset: options.reset
};
delete options.reset;

return this.query(request, options)
.then(response => new User(this.kuzzle, response.result._id, response.result._source));
Expand Down
41 changes: 22 additions & 19 deletions src/core/KuzzleEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ class Listener {

class KuzzleEventEmitter {
constructor() {
this._events = {};
this._events = new Map();
}

_exists (listeners, fn) {
return Boolean(listeners.find(listener => listener.fn === fn));
}

listeners (eventName) {
if (this._events[eventName] === undefined) {
if (! this._events.has(eventName)) {
return [];
}

return this._events[eventName].map(listener => listener.fn);
return this._events.get(eventName).map(listener => listener.fn);
}

addListener (eventName, listener, once = false) {
Expand All @@ -33,12 +33,12 @@ class KuzzleEventEmitter {
throw new Error(`Invalid listener type: expected a function, got a ${listenerType}`);
}

if (this._events[eventName] === undefined) {
this._events[eventName] = [];
if (! this._events.has(eventName)) {
this._events.set(eventName, []);
}

if (!this._exists(this._events[eventName], listener)) {
this._events[eventName].push(new Listener(listener, once));
if (! this._exists(this._events.get(eventName), listener)) {
this._events.get(eventName).push(new Listener(listener, once));
}

return this;
Expand All @@ -53,12 +53,14 @@ class KuzzleEventEmitter {
return this;
}

if (this._events[eventName] === undefined) {
this._events[eventName] = [];
if (! this._events.has(eventName)) {
this._events.set(eventName, []);
}

if (!this._exists(this._events[eventName], listener)) {
this._events[eventName] = [new Listener(listener, once)].concat(this._events[eventName]);
if (!this._exists(this._events.get(eventName), listener)) {
const listeners = [new Listener(listener, once)].concat(this._events.get(eventName));

this._events.set(eventName, listeners);
}

return this;
Expand All @@ -77,7 +79,7 @@ class KuzzleEventEmitter {
}

removeListener (eventName, listener) {
const listeners = this._events[eventName];
const listeners = this._events.get(eventName);

if (!listeners || !listeners.length) {
return this;
Expand All @@ -90,24 +92,25 @@ class KuzzleEventEmitter {
}

if (listeners.length === 0) {
delete this._events[eventName];
this._events.delete(eventName);
}

return this;
}

removeAllListeners (eventName) {
if (eventName) {
delete this._events[eventName];
} else {
this._events = {};
this._events.delete(eventName);
}
else {
this._events = new Map();
}

return this;
}

emit (eventName, ...payload) {
const listeners = this._events[eventName];
const listeners = this._events.get(eventName);

if (listeners === undefined) {
return false;
Expand All @@ -131,11 +134,11 @@ class KuzzleEventEmitter {
}

eventNames () {
return Object.keys(this._events);
return Array.from(this._events.keys());
}

listenerCount (eventName) {
return this._events[eventName] && this._events[eventName].length || 0;
return this._events.has(eventName) && this._events.get(eventName).length || 0;
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/core/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class Room {
* @param {object} options
*/
constructor (controller, index, collection, body, callback, options = {}) {
Reflect.defineProperty(this, '_kuzzle', {
value: controller.kuzzle
});
this.controller = controller;
this.kuzzle = controller.kuzzle;
this.index = index;
this.collection = collection;
this.callback = callback;
Expand All @@ -25,12 +27,12 @@ class Room {
collection,
body,
controller: 'realtime',
action: 'subscribe'
action: 'subscribe',
state: this.options.state,
scope: this.options.scope,
users: this.options.users,
volatile: this.options.volatile
};
for (const opt of ['state', 'scope', 'users', 'volatile']) {
this.request[opt] = this.options[opt];
delete this.options[opt];
}

this.autoResubscribe = typeof options.autoResubscribe === 'boolean'
? options.autoResubscribe
Expand All @@ -39,14 +41,14 @@ class Room {
? options.subscribeToSelf
: true;

for (const opt of ['autoResubscribe', 'subscribeToSelf']) {
delete this.options[opt];
}

// force bind for further event listener calls
this._channelListener = this._channelListener.bind(this);
}

get kuzzle () {
return this._kuzzle;
}

subscribe () {
return this.kuzzle.query(this.request, this.options)
.then(response => {
Expand Down
16 changes: 12 additions & 4 deletions src/core/searchResult/SearchResultBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ class SearchResultBase {
* @param {object} response
*/
constructor (kuzzle, request = {}, options = {}, response = {}) {
this._kuzzle = kuzzle;
this._request = request;
this._response = response;
this._options = options;
Reflect.defineProperty(this, '_kuzzle', {
value: kuzzle
});
Reflect.defineProperty(this, '_request', {
value: request
});
Reflect.defineProperty(this, '_options', {
value: options
});
Reflect.defineProperty(this, '_response', {
value: response
});

this._controller = request.controller;
this._searchAction = 'search';
Expand Down
Loading