Skip to content

Commit 80ab1cd

Browse files
committed
update @types/node, docs on allowStaleOnFetchAbort
1 parent df2451d commit 80ab1cd

File tree

6 files changed

+70
-15
lines changed

6 files changed

+70
-15
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,32 @@ Set to true to return a stale value from the cache when the
264264
event, whether user-triggered, or due to internal cache behavior.
265265

266266
Unless `ignoreFetchAbort` is also set, the underlying
267-
`fetchMethod` will still be considered canceled, and its return
268-
value will be ignored and not cached.
267+
`fetchMethod` will still be considered canceled, and any value
268+
it returns will be ignored and not cached.
269+
270+
Caveat: since fetches are aborted when a new value is explicitly
271+
set in the cache, this can lead to fetch returning a stale value,
272+
since that was the fallback value _at the moment the `fetch()` was
273+
initiated_, even though the new updated value is now present in
274+
the cache.
275+
276+
For example:
277+
278+
```ts
279+
const cache = new LRUCache<string, any>({
280+
ttl: 100,
281+
fetchMethod: async (url, oldValue, { signal }) => {
282+
const res = await fetch(url, { signal })
283+
return await res.json()
284+
}
285+
})
286+
cache.set('https://example.com/', { some: 'data' })
287+
// 100ms go by...
288+
const result = cache.fetch('https://example.com/')
289+
cache.set('https://example.com/', { other: 'thing' })
290+
console.log(await result) // { some: 'data' }
291+
console.log(cache.get('https://example.com/')) // { other: 'thing' }
292+
```
269293

270294
### `ignoreFetchAbort`
271295

@@ -569,7 +593,7 @@ For the usage of the `status` option, see **Status Tracking**
569593
below.
570594

571595
If the value is `undefined`, then this is an alias for
572-
`cache.delete(key)`. `undefined` is never stored in the cache.
596+
`cache.delete(key)`. `undefined` is never stored in the cache.
573597
See **Storing Undefined Values** below.
574598

575599
### `get(key, { updateAgeOnGet, allowStale, status } = {}) => value`
@@ -1028,7 +1052,7 @@ internally in a few places to indicate that a key is not in the
10281052
cache.
10291053

10301054
You may call `cache.set(key, undefined)`, but this is just an
1031-
an alias for `cache.delete(key)`. Note that this has the effect
1055+
an alias for `cache.delete(key)`. Note that this has the effect
10321056
that `cache.has(key)` will return _false_ after setting it to
10331057
undefined.
10341058

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"repository": "git://github.com/isaacs/node-lru-cache.git",
5757
"devDependencies": {
5858
"@size-limit/preset-small-lib": "^7.0.8",
59-
"@types/node": "^17.0.31",
59+
"@types/node": "^20.2.5",
6060
"@types/tap": "^15.0.6",
6161
"benchmark": "^2.1.4",
6262
"c8": "^7.11.2",

src/index.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,32 @@ export namespace LRUCache {
712712
* event, whether user-triggered, or due to internal cache behavior.
713713
*
714714
* Unless {@link OptionsBase.ignoreFetchAbort} is also set, the underlying
715-
* {@link OptionsBase.fetchMethod} will still be considered canceled, and its return
716-
* value will be ignored and not cached.
715+
* {@link OptionsBase.fetchMethod} will still be considered canceled, and
716+
* any value it returns will be ignored and not cached.
717+
*
718+
* Caveat: since fetches are aborted when a new value is explicitly
719+
* set in the cache, this can lead to fetch returning a stale value,
720+
* since that was the fallback value _at the moment the `fetch()` was
721+
* initiated_, even though the new updated value is now present in
722+
* the cache.
723+
*
724+
* For example:
725+
*
726+
* ```ts
727+
* const cache = new LRUCache<string, any>({
728+
* ttl: 100,
729+
* fetchMethod: async (url, oldValue, { signal }) => {
730+
* const res = await fetch(url, { signal })
731+
* return await res.json()
732+
* }
733+
* })
734+
* cache.set('https://example.com/', { some: 'data' })
735+
* // 100ms go by...
736+
* const result = cache.fetch('https://example.com/')
737+
* cache.set('https://example.com/', { other: 'thing' })
738+
* console.log(await result) // { some: 'data' }
739+
* console.log(cache.get('https://example.com/')) // { other: 'thing' }
740+
* ```
717741
*/
718742
allowStaleOnFetchAbort?: boolean
719743

test/fetch.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,8 @@ t.test('abort, but then keep on fetching anyway', async t => {
647647
return new Promise(res =>
648648
setTimeout(() => {
649649
resolved = true
650-
res(returnUndefined ? undefined : k)
650+
if (returnUndefined) res()
651+
else res(k)
651652
}, 100)
652653
)
653654
},
@@ -706,7 +707,10 @@ t.test('allowStaleOnFetchAbort', async t => {
706707
fetchMethod: async (k, _, { signal }) => {
707708
return new Promise(res => {
708709
const t = setTimeout(() => res(k), 100)
709-
signal.addEventListener('abort', () => clearTimeout(t))
710+
signal.addEventListener('abort', () => {
711+
clearTimeout(t)
712+
res()
713+
})
710714
})
711715
},
712716
})
@@ -716,7 +720,11 @@ t.test('allowStaleOnFetchAbort', async t => {
716720
const p = c.fetch(1, { signal: ac.signal })
717721
ac.abort(new Error('gimme the stale value'))
718722
t.equal(await p, 10)
719-
t.equal(c.get(1, { allowStale: true }), 10)
723+
t.equal(c.get(1, { allowStale: true, noDeleteOnStaleGet: true }), 10)
724+
const p2 = c.fetch(1)
725+
c.set(1, 100)
726+
t.equal(await p2, 10)
727+
t.equal(c.get(1), 100)
720728
})
721729

722730
t.test('background update on timeout, return stale', async t => {

tsconfig-base.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"isolatedModules": true,
1010
"moduleResolution": "node",
1111
"resolveJsonModule": true,
12-
"skipLibCheck": true,
1312
"sourceMap": true,
1413
"inlineSources": true,
1514
"strict": true,

0 commit comments

Comments
 (0)