Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v10.0.0
Choose a base ref
...
head repository: isaacs/node-lru-cache
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v10.0.1
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Jun 15, 2023

  1. changelog v10

    isaacs committed Jun 15, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    0c63b0d View commit details

Commits on Aug 10, 2023

  1. Call dispose() when fetch overwrites a value

    This corrects an oversight in the disposal flow on set overwrites.
    
    We don't call dispose when creating the BackgroundFetch, because of
    course, the thing hasn't been replaced yet.
    
    And we don't call dispose when _replacing_ a BackgroundFetch, because
    that isn't a value, it's just a potential value, so we abort the fetch
    and forget about it (which is a no-op if the set() in question is the
    resolution of that BackgroundFetch).
    
    The missing piece is that we *do* have to dispose() the
    `__staleWhileFetching` value on that BackgroundFetch promise, if there
    is one, when overwriting a BackgroundFetch.
    
    Fix: #309
    isaacs committed Aug 10, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    fcb797e View commit details
  2. 10.0.1

    isaacs committed Aug 10, 2023
    Copy the full SHA
    870a66d View commit details
Showing with 43 additions and 5 deletions.
  1. +7 −0 CHANGELOG.md
  2. +2 −2 package-lock.json
  3. +1 −1 package.json
  4. +9 −0 src/index.ts
  5. +24 −2 test/fetch.ts
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# cringe lorg

## 10.0.0

- `cache.fetch()` return type is now `Promise<V | undefined>`
instead of `Promise<V | void>`. This is an irrelevant change
practically speaking, but can require changes for TypeScript
users.

## 9.1.0

- `cache.set(key, undefined)` is now an alias for
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lru-cache",
"description": "A cache object that deletes the least-recently-used items.",
"version": "10.0.0",
"version": "10.0.1",
"author": "Isaac Z. Schlueter <i@izs.me>",
"keywords": [
"mru",
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1682,6 +1682,15 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
if (v !== oldVal) {
if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
oldVal.__abortController.abort(new Error('replaced'))
const { __staleWhileFetching: s } = oldVal
if (s !== undefined && !noDisposeOnSet) {
if (this.#hasDispose) {
this.#dispose?.(s as V, k, 'set')
}
if (this.#hasDisposeAfter) {
this.#disposed?.push([s as V, k, 'set'])
}
}
} else if (!noDisposeOnSet) {
if (this.#hasDispose) {
this.#dispose?.(oldVal as V, k, 'set')
26 changes: 24 additions & 2 deletions test/fetch.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ if (typeof performance === 'undefined') {
global.performance = require('perf_hooks').performance
}
import t from 'tap'
import { LRUCache, BackgroundFetch } from '../'
import { BackgroundFetch, LRUCache } from '../'
import { expose } from './fixtures/expose'

const fn: LRUCache.Fetcher<any, any> = async (_, v) =>
@@ -720,7 +720,10 @@ t.test('allowStaleOnFetchAbort', async t => {
const p = c.fetch(1, { signal: ac.signal })
ac.abort(new Error('gimme the stale value'))
t.equal(await p, 10)
t.equal(c.get(1, { allowStale: true, noDeleteOnStaleGet: true }), 10)
t.equal(
c.get(1, { allowStale: true, noDeleteOnStaleGet: true }),
10
)
const p2 = c.fetch(1)
c.set(1, 100)
t.equal(await p2, 10)
@@ -844,3 +847,22 @@ t.test('has false for pending fetch without stale val', async t => {
t.equal(c.has(1), true)
}
})

t.test('properly dispose when using fetch', async t => {
const disposes: [number, number, string][] = []
const disposeAfters: [number, number, string][] = []
let i = 0
const c = new LRUCache<number, number>({
max: 3,
ttl: 10,
dispose: (key, val, reason) => disposes.push([key, val, reason]),
disposeAfter: (key, val, reason) =>
disposeAfters.push([key, val, reason]),
fetchMethod: async () => Promise.resolve(i++),
})
t.equal(await c.fetch(1), 0)
clock.advance(20)
t.equal(await c.fetch(1), 1)
t.strictSame(disposes, [[0, 1, 'set']])
t.strictSame(disposeAfters, [[0, 1, 'set']])
})