Unverified Commit 2ba4dec1 authored by Francis Gulotta's avatar Francis Gulotta Committed by GitHub

Add a generic type for unstable_cache (#49166)

## For Contributors

I know it's an unstable api but this just makes it easier to test with.
This uses the type of the callback to `unstable_cache` as the return
type of `unstable_cache`.
parent bc5164fe
...@@ -5,15 +5,14 @@ import { ...@@ -5,15 +5,14 @@ import {
type Callback = (...args: any[]) => Promise<any> type Callback = (...args: any[]) => Promise<any>
// TODO: generic callback type? export function unstable_cache<T extends Callback>(
export function unstable_cache( cb: T,
cb: Callback,
keyParts: string[], keyParts: string[],
options: { options: {
revalidate: number | false revalidate: number | false
tags?: string[] tags?: string[]
} }
): Callback { ): T {
const joinedKey = cb.toString() + '-' + keyParts.join(', ') const joinedKey = cb.toString() + '-' + keyParts.join(', ')
const staticGenerationAsyncStorage = ( const staticGenerationAsyncStorage = (
fetch as any fetch as any
...@@ -34,7 +33,7 @@ export function unstable_cache( ...@@ -34,7 +33,7 @@ export function unstable_cache(
) )
} }
return async (...args: any[]) => { const cachedCb = async (...args: any[]) => {
// We override the default fetch cache handling inside of the // We override the default fetch cache handling inside of the
// cache callback so that we only cache the specific values returned // cache callback so that we only cache the specific values returned
// from the callback instead of also caching any fetches done inside // from the callback instead of also caching any fetches done inside
...@@ -131,4 +130,6 @@ export function unstable_cache( ...@@ -131,4 +130,6 @@ export function unstable_cache(
} }
) )
} }
// TODO: once AsyncLocalStorage.run() returns the correct types this override will no longer be necessary
return cachedCb as unknown as T
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment