diff --git a/index.js b/index.js index d58824f5..e37f5161 100644 --- a/index.js +++ b/index.js @@ -244,12 +244,14 @@ class LRUCache { *indexes ({ allowStale = this.allowStale } = {}) { if (this.size) { for (let i = this.tail, j; true; ) { + if (!this.isValidIndex(i)) { + break + } j = i === this.head if (allowStale || !this.isStale(i)) { yield i } - // either head now, or WAS head and head was deleted - if (i === this.head || j && !this.isValidIndex(i)) { + if (i === this.head) { break } else { i = this.prev[i] @@ -261,12 +263,14 @@ class LRUCache { *rindexes ({ allowStale = this.allowStale } = {}) { if (this.size) { for (let i = this.head, j; true; ) { - j = i === this.tail + if (!this.isValidIndex(i)) { + break + } if (allowStale || !this.isStale(i)) { yield i } // either the tail now, or WAS the tail, and deleted - if (i === this.tail || j && !this.isValidIndex(i)) { + if (i === this.tail) { break } else { i = this.next[i] diff --git a/test/delete-while-iterating.js b/test/delete-while-iterating.js index 67e24f11..c2aa29b3 100644 --- a/test/delete-while-iterating.js +++ b/test/delete-while-iterating.js @@ -66,3 +66,35 @@ t.test('rdelete odds', t => { t.same([...c.keys()], [4, 2, 0]) t.end() }) + +t.test('delete two of them', t => { + const c = t.context + t.same([...c.keys()], [4, 3, 2, 1, 0]) + for (const k of c.keys()) { + if (k === 3) { + c.delete(3) + c.delete(4) + } else if (k === 1) { + c.delete(1) + c.delete(0) + } + } + t.same([...c.keys()], [2]) + t.end() +}) + +t.test('rdelete two of them', t => { + const c = t.context + t.same([...c.keys()], [4, 3, 2, 1, 0]) + for (const k of c.rkeys()) { + if (k === 3) { + c.delete(3) + c.delete(4) + } else if (k === 1) { + c.delete(1) + c.delete(0) + } + } + t.same([...c.keys()], [2]) + t.end() +})