forked from ava/use-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseCache.ts
26 lines (22 loc) · 1.21 KB
/
useCache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import useSSR from 'use-ssr'
import { invariant } from './utils'
import { Cache, CachePolicies } from './types'
import getLocalStorage from './storage/localStorage'
import getMemoryStorage from './storage/memoryStorage'
const { NETWORK_ONLY, NO_CACHE } = CachePolicies
/**
* Eventually, this will be replaced by use-react-storage, so
* having this as a hook allows us to have minimal changes in
* the future when switching over.
*/
type UseCacheArgs = { persist: boolean, cacheLife: number, cachePolicy: CachePolicies }
const useCache = ({ persist, cacheLife, cachePolicy }: UseCacheArgs): Cache => {
const { isNative, isServer } = useSSR()
invariant(!(isServer && persist), 'There is no persistent storage on the Server currently! 🙅♂️')
invariant(!(isNative && persist), 'React Native support for persistent cache is not yet implemented. 🙅♂️')
invariant(!(persist && [NO_CACHE, NETWORK_ONLY].includes(cachePolicy)), `You cannot use option 'persist' with cachePolicy: ${cachePolicy} 🙅♂️`)
// right now we're not worrying about react-native
if (persist) return getLocalStorage({ cacheLife: cacheLife || (24 * 3600000) })
return getMemoryStorage({ cacheLife })
}
export default useCache