Rebuilt cache changes

This commit is contained in:
Amish Shah
2015-12-12 17:32:49 +00:00
parent 8eb4c47f79
commit 60747afda4

View File

@@ -6,6 +6,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var discrimS = Symbol();
var discrimCacheS = Symbol();
var Cache = (function (_Array) {
_inherits(Cache, _Array);
@@ -13,12 +16,12 @@ var Cache = (function (_Array) {
_classCallCheck(this, Cache);
_Array.call(this);
this.discrim = discrim || "id";
this.discrimCache = {};
this[discrimS] = discrim || "id";
this[discrimCacheS] = {};
}
Cache.prototype.get = function get(key, value) {
if (key === this.discrim) return this.discrimCache[value] || null;
if (key === this[discrimS]) return this[discrimCacheS][value] || null;
var l = this.length;
for (var i = 0; i < l; i++) if (this[i][key] == value) return this[i];
@@ -26,11 +29,11 @@ var Cache = (function (_Array) {
};
Cache.prototype.has = function has(object) {
return !!this.get(this.discrim, object[this.discrim]);
return !!this.get(this[discrimS], object[this[discrimS]]);
};
Cache.prototype.getAll = function getAll(key, value) {
var found = new Cache(this.discrim);
var found = new Cache(this[discrimS]);
this.forEach(function (val, index, array) {
if (val.hasOwnProperty(key) && val[key] == value) {
found.push(val);
@@ -41,24 +44,24 @@ var Cache = (function (_Array) {
};
Cache.prototype.add = function add(data) {
var cacheKey = this.discrim === "id" ? data.id : data[this.discrim];
if (this.discrimCache[cacheKey]) {
return this.discrimCache[cacheKey];
var cacheKey = this[discrimS] === "id" ? data.id : data[this[discrimS]];
if (this[discrimCacheS][cacheKey]) {
return this[discrimCacheS][cacheKey];
}
if (this.limit && this.length >= this.limit) {
this.splice(0, 1);
}
this.push(data);
this.discrimCache[cacheKey] = data;
this[discrimCacheS][cacheKey] = data;
return data;
};
Cache.prototype.update = function update(old, data) {
var item = this.get(this.discrim, old[this.discrim]);
var item = this.get(this[discrimS], old[this[discrimS]]);
if (item) {
var index = this.indexOf(item);
this[index] = data;
this.discrimCache[data[this.discrim]] = data;
this[discrimCacheS][data[this[discrimS]]] = this[index];
return this[index];
} else {
return false;
@@ -70,12 +73,12 @@ var Cache = (function (_Array) {
};
Cache.prototype.remove = function remove(data) {
delete this.discrimCache[data[this.discrim]];
delete this[discrimCacheS][data[this[discrimS]]];
var index = this.indexOf(data);
if (~index) {
this.splice(index, 1);
} else {
var item = this.get(this.discrim, data[this.discrim]);
var item = this.get(this[discrimS], data[this[discrimS]]);
if (item) {
this.splice(this.indexOf(item), 1);
}