Skip to content

Commit

Permalink
types: Update Cache Interface types (#2554)
Browse files Browse the repository at this point in the history
* update Cache.keys iterator type from string to Key

* revert lockfile

* use string as key for cache interface

* revert String coercion
  • Loading branch information
dmmulroy authored Apr 8, 2023
1 parent c09d200 commit bb55887
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions _internal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,9 +429,9 @@ export type RevalidateCallback = <K extends RevalidateEvent>(

export interface Cache<Data = any> {
keys(): IterableIterator<string>
get(key: Key): State<Data> | undefined
set(key: Key, value: State<Data>): void
delete(key: Key): void
get(key: string): State<Data> | undefined
set(key: string, value: State<Data>): void
delete(key: string): void
}

export interface StateDependencies {
Expand Down
4 changes: 2 additions & 2 deletions _internal/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const createCacheHelper = <Data = any, T = State<Data, any>>(
const state = SWRGlobalState.get(cache) as GlobalState
return [
// Getter
() => (cache.get(key) || EMPTY_CACHE) as T,
() => ((!isUndefined(key) && cache.get(key)) || EMPTY_CACHE) as T,
// Setter
(info: T) => {
if (!isUndefined(key)) {
Expand All @@ -61,7 +61,7 @@ export const createCacheHelper = <Data = any, T = State<Data, any>>(
}

// If we haven't done any client-side updates, we return the current value.
return (cache.get(key) || EMPTY_CACHE) as T
return ((!isUndefined(key) && cache.get(key)) || EMPTY_CACHE) as T
}
] as const
}

0 comments on commit bb55887

Please sign in to comment.