Skip to content

Commit

Permalink
fetch resolve to undefined rather than void
Browse files Browse the repository at this point in the history
PR-URL: #303
Credit: @Thenkei
Close: #303
Reviewed-by: @isaacs

EDIT(@isaacs): edited to continue to allow `fetchMethod` to return
`Promise<void>`.

BREAKING CHANGE: public method type signature change
  • Loading branch information
Thenkei authored and isaacs committed Jun 15, 2023
1 parent 01075cb commit ca4ea17
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Stack {
/**
* Promise representing an in-progress {@link LRUCache#fetch} call
*/
export type BackgroundFetch<V> = Promise<V | undefined | void> & {
export type BackgroundFetch<V> = Promise<V | undefined> & {
__returned: BackgroundFetch<V> | undefined
__abortController: AbortController
__staleWhileFetching: V | undefined
Expand Down Expand Up @@ -500,7 +500,7 @@ export namespace LRUCache {
key: K,
staleValue: V | undefined,
options: FetcherOptions<K, V, FC>
) => Promise<V | void | undefined> | V | void | undefined
) => Promise<V | undefined | void> | V | undefined | void

/**
* Options which may be passed to the {@link LRUCache} constructor.
Expand Down Expand Up @@ -1868,9 +1868,9 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
}

const cb = (
v: V | void | undefined,
v: V | undefined,
updateCache = false
): V | undefined | void => {
): V | undefined => {
const { aborted } = ac.signal
const ignoreAbort = options.ignoreFetchAbort && v !== undefined
if (options.status) {
Expand Down Expand Up @@ -1943,12 +1943,12 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
}

const pcall = (
res: (v: V | void | undefined) => void,
res: (v: V | undefined) => void,
rej: (e: any) => void
) => {
const fmp = this.#fetchMethod?.(k, v, fetchOpts)
if (fmp && fmp instanceof Promise) {
fmp.then(v => res(v), rej)
fmp.then(v => res(v === undefined ? undefined : v), rej)
}
// ignored, we go until we finish, regardless.
// defer check until we are actually aborting,
Expand All @@ -1958,7 +1958,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
!options.ignoreFetchAbort ||
options.allowStaleOnFetchAbort
) {
res()
res(undefined)
// when it eventually resolves, update the cache.
if (options.allowStaleOnFetchAbort) {
res = v => cb(v, true)
Expand All @@ -1969,7 +1969,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {

if (options.status) options.status.fetchDispatched = true
const p = new Promise(pcall).then(cb, eb)
const bf = Object.assign(p, {
const bf: BackgroundFetch<V> = Object.assign(p, {
__abortController: ac,
__staleWhileFetching: v,
__returned: undefined,
Expand Down Expand Up @@ -2020,7 +2020,7 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
: FC extends undefined | void
? LRUCache.FetchOptionsNoContext<K, V>
: LRUCache.FetchOptionsWithContext<K, V, FC>
): Promise<void | V>
): Promise<undefined | V>
// this overload not allowed if context is required
fetch(
k: unknown extends FC
Expand All @@ -2033,11 +2033,11 @@ export class LRUCache<K extends {}, V extends {}, FC = unknown> {
: FC extends undefined | void
? LRUCache.FetchOptionsNoContext<K, V>
: never
): Promise<void | V>
): Promise<undefined | V>
async fetch(
k: K,
fetchOptions: LRUCache.FetchOptions<K, V, FC> = {}
): Promise<void | V> {
): Promise<undefined | V> {
const {
// get options
allowStale = this.allowStale,
Expand Down

0 comments on commit ca4ea17

Please sign in to comment.