nakedobjects.spa
Version:
Single Page Application client for a Naked Objects application.
105 lines • 3.1 kB
JavaScript
var LruNode = (function () {
function LruNode(key, value) {
this.key = key;
this.value = value;
this.next = null;
this.previous = null;
}
return LruNode;
}());
var SimpleLruCache = (function () {
function SimpleLruCache(depth) {
this.depth = depth;
this.cache = {};
this.count = 0;
this.head = null;
this.tail = null;
}
SimpleLruCache.prototype.unlinkNode = function (node) {
var nodePrevious = node.previous;
var nodeNext = node.next;
if (nodePrevious) {
nodePrevious.next = nodeNext;
}
else {
// was head
this.head = nodeNext;
}
if (nodeNext) {
nodeNext.previous = nodePrevious;
}
else {
//was tail
this.tail = nodePrevious;
}
this.count--;
};
SimpleLruCache.prototype.moveNodeToHead = function (node) {
var existingHead = this.head;
node.previous = null;
node.next = existingHead;
if (existingHead) {
existingHead.previous = node;
}
else {
// no existing head so this is also tail
this.tail = node;
}
this.head = node;
this.count++;
};
SimpleLruCache.prototype.add = function (key, value) {
if (this.cache[key]) {
this.updateExistingEntry(key, value);
}
else {
this.addNewEntry(key, value);
}
};
SimpleLruCache.prototype.remove = function (key) {
var node = this.cache[key];
if (node) {
this.unlinkNode(node);
delete this.cache[key];
}
};
SimpleLruCache.prototype.removeAll = function () {
this.head = this.tail = null;
this.cache = {};
this.count = 0;
};
SimpleLruCache.prototype.getNode = function (key) {
var node = this.cache[key];
if (node) {
this.unlinkNode(node);
this.moveNodeToHead(node);
}
return node;
};
SimpleLruCache.prototype.get = function (key) {
var node = this.getNode(key);
return node ? node.value : null;
};
SimpleLruCache.prototype.updateExistingEntry = function (key, value) {
var node = this.getNode(key);
node.value = value;
};
SimpleLruCache.prototype.addNewEntry = function (key, value) {
var newNode = new LruNode(key, value);
this.cache[key] = newNode;
this.moveNodeToHead(newNode);
this.trimCache();
};
SimpleLruCache.prototype.trimCache = function () {
while (this.count > this.depth) {
var tail = this.tail;
if (tail) {
this.unlinkNode(tail);
delete this.cache[tail.key];
}
}
};
return SimpleLruCache;
}());
export { SimpleLruCache };
//# sourceMappingURL=simple-lru-cache.js.map