Skip to content

Commit

Permalink
Moving reset() into lexical scope & calling from constructor() & …
Browse files Browse the repository at this point in the history
…`clear()`, fixing / simplifying `remove()`, fixes #7
  • Loading branch information
avoidwork committed Dec 5, 2018
1 parent cd1d926 commit d0cdcfe
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 52 deletions.
44 changes: 19 additions & 25 deletions lib/tiny-lru.js
Expand Up @@ -13,17 +13,24 @@
const next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1),
empty = null;

function reset () {
this.cache = {};
this.first = empty;
this.last = empty;
this.length = 0;

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

return this.reset();
reset.call(this);
}

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

if (silent === false && this.notify === true) {
next(this.onchange("clear", this.dump()));
Expand Down Expand Up @@ -95,23 +102,19 @@
this.length--;

if (result.previous !== empty) {
this.cache[result.previous].next = result.next !== key ? result.next : empty;

if (this.first === key) {
this.first = result.previous;
}
} else if (this.first === key) {
this.first = empty;
this.cache[result.previous].next = result.next;
}

if (result.next !== empty) {
this.cache[result.next].previous = result.previous !== key ? result.previous : empty;
this.cache[result.next].previous = result.previous;
}

if (this.last === key) {
this.last = result.next;
}
} else if (this.last === key) {
this.last = empty;
if (this.first === key) {
this.first = result.previous;
}

if (this.last === key) {
this.last = this.first;
}

if (silent === false && this.notify === true) {
Expand All @@ -122,15 +125,6 @@
return result;
}

reset () {
this.cache = {};
this.first = empty;
this.last = empty;
this.length = 0;

return this;
}

set (key, value, silent = false, bypass = false) {
if (bypass === true || this.has(key) === true) {
const item = this.cache[key];
Expand Down
2 changes: 1 addition & 1 deletion lib/tiny-lru.min.js

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

7 comments on commit d0cdcfe

@cemremengu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avoidwork
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cemremengu ugh, didn't expect anyone to call that directly; will revert.

@avoidwork
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cemremengu reset() shouldn't be called, it should be clear() if you follow the API back to 1.0; it will disappear again with 4.0 which will be equal to this commit.

@cemremengu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, will update the code on my side accordingly.

@avoidwork
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's my mistake. Sorry for the hassle, found the original commit and added a comment. The method shouldn't have been made public.

@cemremengu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, thanks for your time and keep up the great work!

@avoidwork
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cemremengu 3.0.7 reverts the breaking change; it's been published but npm is not showing it yet. 4.0.0 is also published.

Please sign in to comment.