From 9d05653f5be0958a61e6eeda2c3d8b7d1b1e1e2b Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Thu, 14 May 2015 17:19:28 +0200 Subject: [PATCH] Use xo-collection. --- packages/xo-lib/collection.js | 160 ---------------------------------- packages/xo-lib/package.json | 4 +- packages/xo-lib/xo.js | 46 ++++++---- 3 files changed, 30 insertions(+), 180 deletions(-) delete mode 100644 packages/xo-lib/collection.js diff --git a/packages/xo-lib/collection.js b/packages/xo-lib/collection.js deleted file mode 100644 index 7bf279b4f..000000000 --- a/packages/xo-lib/collection.js +++ /dev/null @@ -1,160 +0,0 @@ -'use strict' - -// =================================================================== - -var forEach = require('lodash.foreach') -var indexOf = require('lodash.indexof') - -// =================================================================== - -function deleteProperties (obj) { - /* jshint forin: false */ - var prop - for (prop in obj) { - delete obj[prop] - } -} - -// =================================================================== - -function defaultKey (item) { - return item.id || item._id || item -} - -// =================================================================== - -function getAll () { - /* jshint validthis: true */ - return this._data -} - -function getIndexes () { - /* jshint validthis: true */ - return this._indexes -} - -function Collection (opts) { - if (!opts) { - opts = {} - } - - this._key = opts.key || defaultKey - - this._indexes = Object.create(null) - if (opts.indexes) { - forEach(opts.indexes, function (field) { - this[field] = Object.create(null) - }, this._indexes) - } - - this._data = Object.create(null) - - // Expose public properties. - Object.defineProperties(this, { - all: { - enumerable: true, - get: getAll - }, - indexes: { - enumerable: true, - get: getIndexes - } - }) -} - -Collection.prototype.clear = function () { - deleteProperties(this._data) - forEach(this._indexes, deleteProperties) -} - -function unsetItemFromIndex (index, field) { - /* jshint validthis: true */ - - var prop = this[field] - if (!prop) { - return - } - - var items = index[prop] - - var i = indexOf(items, this) - if (i === -1) { - return - } - - // The index contains only this one item for this prop. - if (items.length === 1) { - delete index[prop] - return - } - - // Remove this item. - items.splice(i, 1) -} - -// Internal unset method. -function unset (item, key) { - /* jshint validthis: true */ - - delete this._data[key] - - forEach(this._indexes, unsetItemFromIndex, item) -} - -function setItemToIndex (index, field) { - /* jshint validthis: true */ - - var prop = this[field] - if (!prop) { - return - } - - var items = index[prop] - if (items) { - // Update the items list. - items.push(this) - } else { - // Create the items list. - index[prop] = [this] - } -} - -Collection.prototype.set = function (item) { - var key = this._key(item) - if (!key) { - // Ignore empty keys. - return - } - - var previous = this._data[key] - if (previous) { - unset.call(this, previous, key) - } - - this._data[key] = item - forEach(this._indexes, setItemToIndex, item) -} - -Collection.prototype.unset = function (item) { - var key = this._key(item) - item = this._data[key] - if (!item) { - return - } - - unset.call(this, item, this._key(item)) -} - -Collection.prototype.setMultiple = function (items) { - forEach(items, this.set, this) -} -Collection.prototype.unsetMultiple = function (items) { - forEach(items, this.unset, this) -} - -// =================================================================== - -function createCollection (opts) { - return new Collection(opts) -} -module.exports = createCollection diff --git a/packages/xo-lib/package.json b/packages/xo-lib/package.json index 9e6dba7c7..0d7d1bb73 100644 --- a/packages/xo-lib/package.json +++ b/packages/xo-lib/package.json @@ -28,12 +28,12 @@ "exec-promise": "^0.5.1", "lodash.assign": "^3.0.0", "lodash.foreach": "^3.0.1", - "lodash.indexof": "^3.0.0", "lodash.isstring": "^3.0.0", "lodash.startswith": "^3.0.0", "make-error": "^0.3.0", "pw": "0.0.4", - "ws": "^0.7.1" + "ws": "^0.7.1", + "xo-collection": "^0.2.1" }, "devDependencies": { "mocha": "^2.1.0", diff --git a/packages/xo-lib/xo.js b/packages/xo-lib/xo.js index 7d149a2c2..8e5cc1556 100644 --- a/packages/xo-lib/xo.js +++ b/packages/xo-lib/xo.js @@ -3,13 +3,15 @@ // =================================================================== var Bluebird = require('bluebird') +var Collection = require('xo-collection').default +var forEach = require('lodash.foreach') +var Index = require('xo-collection/index') var isString = require('lodash.isstring') var startsWith = require('lodash.startswith') var Api = require('./api') var BackOff = require('./back-off') var ConnectionError = require('./connection-error') -var createCollection = require('./collection') var SessionError = require('./session-error') // =================================================================== @@ -29,6 +31,18 @@ function makeStandaloneDeferred () { function noop () {} +function setMultiple (collection, items) { + forEach(items, function (item) { + collection.set(item) + }) +} + +function unsetMultiple (collection, items) { + forEach(items, function (item) { + collection.unset(item) + }) +} + // =================================================================== function Xo (opts) { @@ -61,27 +75,23 @@ function Xo (opts) { return } - var method = ( - notification.params.type === 'exit' ? - 'unset' : - 'set' - ) + 'Multiple' + var method = notification.params.type === 'exit' ? + unsetMultiple : + setMultiple - this.objects[method](notification.params.items) + method(this.objects, notification.params.items) }.bind(this)) // ----------------------------------------------------------------- - this.objects = createCollection({ - indexes: [ - 'ref', - 'type', - 'UUID' - ], - key: function (item) { - return item.UUID || item.ref - } - }) + var objects = this.objects = new Collection() + objects.getKey = function (item) { + return item.UUID || item.ref || 'undefined' + } + objects.createIndex('ref', new Index('ref')) + objects.createIndex('type', new Index('type')) + objects.createIndex('UUID', new Index('UUID')) + this.status = 'disconnected' this.user = null @@ -177,7 +187,7 @@ Xo.prototype._tryToOpenSession = function () { this._api.call('xo.getAllObjects').bind(this).then(function (objects) { this.objects.clear() - this.objects.setMultiple(objects) + setMultiple(this.objects, objects) }) // Validate the sign in.