Skip to content

Commit

Permalink
Initial refactor to remove notify functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Dec 9, 2018
1 parent 12d7d06 commit 9576b00
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 87 deletions.
54 changes: 12 additions & 42 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
* @copyright 2018
* @license BSD-3-Clause
* @link https://github.com/avoidwork/tiny-lru
* @version 4.0.6
* @version 5.0.0
*/
"use strict";

(function (global) {
const next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1),
empty = null;
const empty = null;

function link (item, key, a, b) {
item[a] = key;
Expand All @@ -31,25 +30,20 @@
}

class LRU {
constructor (max, notify, ttl) {
constructor (max, ttl) {
this.max = max;
this.notify = notify;
this.ttl = ttl;
reset.call(this);
}

clear (silent = false) {
clear () {
reset.call(this);

if (silent === false && this.notify === true) {
next(this.onchange("clear", this.dump()));
}

return this;
}

delete (key, silent = false) {
return this.remove(key, silent);
delete (key) {
return this.remove(key);
}

dump () {
Expand All @@ -65,16 +59,12 @@
}

evict () {
this.remove(this.last, true, true);

if (this.notify === true) {
next(this.onchange("evict", this.dump()));
}
this.remove(this.last, true);

return this;
}

get (key, silent = false) {
get (key) {
let result;

if (this.has(key) === true) {
Expand All @@ -83,10 +73,6 @@
if (item.expiry === -1 || item.expiry > Date.now()) {
result = item.value;
this.set(key, result, true, true);

if (silent === false && this.notify === true) {
next(this.onchange("get", this.dump()));
}
} else {
this.remove(key);
}
Expand All @@ -101,7 +87,7 @@

onchange () {}

remove (key, silent = false, bypass = false) {
remove (key, bypass = false) {
let result;

if (bypass === true || this.has(key) === true) {
Expand All @@ -125,16 +111,12 @@
if (this.last === key) {
this.last = result.next;
}

if (silent === false && this.notify === true) {
next(this.onchange("remove", this.dump()));
}
}

return result;
}

set (key, value, silent = false, bypass = false) {
set (key, value, bypass = false) {
if (bypass === true || this.has(key) === true) {
const item = this.cache[key];

Expand Down Expand Up @@ -184,24 +166,12 @@

this.first = key;

if (silent === false && this.notify === true) {
next(this.onchange("set", this.dump()));
}

return this;
}

update (arg) {
const obj = JSON.parse(arg);

Object.keys(obj).forEach(i => {
this[i] = obj[i];
});
}
}

function factory (max = 1000, notify = false, ttl = 0) {
return new LRU(max, notify, ttl);
function factory (max = 1000, ttl = 0) {
return new LRU(max, ttl);
}

// Node, AMD & window supported
Expand Down
4 changes: 2 additions & 2 deletions lib/tiny-lru.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9576b00

Please sign in to comment.