Skip to content

Commit

Permalink
fix: cache map object type
Browse files Browse the repository at this point in the history
  • Loading branch information
shhonarmandi committed Apr 26, 2024
1 parent 6da84a3 commit 9502485
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/_internal/types.ts
Expand Up @@ -509,7 +509,10 @@ export type RevalidateCallback = <K extends RevalidateEvent>(
opts?: any
) => RevalidateCallbackReturnType[K]

export interface Cache<Data = any> {
export type Cache<Data = any> = Omit<
Map<any, any>,
'keys' | 'get' | 'set' | 'delete'
> & {
keys(): IterableIterator<string>
get(key: string): State<Data> | undefined
set(key: string, value: State<Data>): void
Expand Down
14 changes: 11 additions & 3 deletions test/use-swr-cache.test.tsx
Expand Up @@ -252,16 +252,24 @@ describe('useSWR - cache provider', () => {
parentCache = parentCache_
return {
keys: () => parentCache.keys(),
set: (k, v) => parentCache_.set(k, v),
set: (k, v) => parentCache.set(k, v),
get: k => {
// We append `-extended` to the value returned by the parent cache.
const v = parentCache_.get(k)
const v = parentCache.get(k)
if (v && typeof v.data !== 'undefined') {
return { ...v, data: v.data + '-extended' }
}
return v
},
delete: k => parentCache_.delete(k)
delete: k => parentCache.delete(k),
clear: () => parentCache.clear(),
size: parentCache.size,
values: parentCache.values(),
has: k => parentCache.has(k),
entries: () => parentCache.entries(),
forEach: k => parentCache.forEach(k),
[Symbol.toStringTag]: parentCache[Symbol.toStringTag],
[Symbol.iterator]: () => parentCache[Symbol.iterator]()
}
}
})
Expand Down

0 comments on commit 9502485

Please sign in to comment.