Skip to content

Commit 69ec9df

Browse files
committed
refactor createCache
1 parent 46a1e8f commit 69ec9df

File tree

4 files changed

+39
-50
lines changed

4 files changed

+39
-50
lines changed

src/cache.ts

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
11
import { serialize } from './libs/serialize'
2-
import {
3-
Cache as CacheType,
4-
Key,
5-
Provider,
6-
Mutator,
7-
ScopedMutator
8-
} from './types'
2+
import { Cache, Key } from './types'
93

10-
// The factory that binds the `mutate` function to the specific cache object.
11-
export function getCacheFactory(internalMutate: any) {
12-
return function createCache<Data>(
13-
provider: Provider
14-
): {
15-
cache: CacheType
16-
mutate: ScopedMutator<Data>
17-
} {
18-
function cacheSet(key: Key, value: any) {
19-
const _key = typeof key === 'string' ? key : serialize(key)[0]
20-
provider.set(_key, value)
21-
}
22-
23-
function cacheDelete(key: Key) {
24-
const [_key] = serialize(key)
25-
provider.delete(_key)
26-
}
4+
export function wrapCache(provider: Cache): Cache {
5+
function cacheSet(key: Key, value: any) {
6+
const _key = typeof key === 'string' ? key : serialize(key)[0]
7+
provider.set(_key, value)
8+
}
279

28-
function cacheGet(key: Key): any {
29-
return provider.get(key)
30-
}
10+
function cacheDelete(key: Key) {
11+
const [_key] = serialize(key)
12+
provider.delete(_key)
13+
}
3114

32-
const cache = {
33-
set: cacheSet,
34-
get: cacheGet,
35-
delete: cacheDelete
36-
}
15+
function cacheGet(key: Key): any {
16+
return provider.get(key)
17+
}
3718

38-
return {
39-
cache,
40-
mutate: (internalMutate as Mutator<Data>).bind(null, cache)
41-
}
19+
return {
20+
set: cacheSet,
21+
get: cacheGet,
22+
delete: cacheDelete
4223
}
4324
}
44-
45-
export const defaultCache = getCacheFactory(() => {})(new Map()).cache

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { dequal } from 'dequal/lite'
22

3-
import { defaultCache } from './cache'
3+
import { wrapCache } from './cache'
44
import webPreset from './libs/web-preset'
55
import { Configuration, RevalidatorOptions, Revalidator } from './types'
66

@@ -68,7 +68,7 @@ const defaultConfig = {
6868
fetcher,
6969

7070
isPaused: () => false,
71-
cache: defaultCache
71+
cache: wrapCache(new Map())
7272
} as const
7373

7474
export default defaultConfig

src/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,3 @@ export interface Cache {
214214
set(key: Key, value: any): any
215215
delete(key: Key): void
216216
}
217-
218-
export interface Provider {
219-
get(key: Key): any
220-
set(key: Key, value: any): any
221-
delete(key: Key): void
222-
}

src/use-swr.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useCallback, useRef, useDebugValue } from 'react'
33

44
import defaultConfig from './config'
55
import webPreset from './libs/web-preset'
6-
import { getCacheFactory } from './cache'
6+
import { wrapCache } from './cache'
77
import { IS_SERVER, rAF, useIsomorphicLayoutEffect } from './env'
88
import { serialize } from './libs/serialize'
99
import SWRConfigContext from './config-context'
@@ -19,7 +19,8 @@ import {
1919
RevalidatorOptions,
2020
Updater,
2121
SWRConfiguration,
22-
Cache
22+
Cache,
23+
ScopedMutator
2324
} from './types'
2425

2526
type Revalidator = (...args: any[]) => void
@@ -681,6 +682,21 @@ Object.defineProperty(SWRConfigContext.Provider, 'default', {
681682
export const SWRConfig = SWRConfigContext.Provider as typeof SWRConfigContext.Provider & {
682683
default: SWRConfiguration
683684
}
684-
export const mutate = internalMutate.bind(null, defaultConfig.cache)
685-
export const createCache = getCacheFactory(internalMutate)
685+
export const mutate = internalMutate.bind(
686+
null,
687+
defaultConfig.cache
688+
) as ScopedMutator<any>
689+
export function createCache<Data>(
690+
provider: Cache
691+
): {
692+
cache: Cache
693+
mutate: ScopedMutator<Data>
694+
} {
695+
const cache = wrapCache(provider)
696+
return {
697+
cache,
698+
mutate: internalMutate.bind(null, cache) as ScopedMutator<Data>
699+
}
700+
}
701+
686702
export default useSWR

0 commit comments

Comments
 (0)