-
Notifications
You must be signed in to change notification settings - Fork 864
/
Copy pathgetStorage.js
44 lines (39 loc) · 1.01 KB
/
getStorage.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// @flow
import type { Storage } from '../types'
function noop() {}
let noopStorage = {
getItem: noop,
setItem: noop,
removeItem: noop,
}
function hasStorage(storageType) {
if (typeof self !== 'object' || !(storageType in self)) {
return false
}
try {
let storage = self[storageType]
const testKey = `redux-persist ${storageType} test`
storage.setItem(testKey, 'test')
storage.getItem(testKey)
storage.removeItem(testKey)
} catch (e) {
if (process.env.NODE_ENV !== 'production')
console.warn(
`redux-persist ${storageType} test failed, persistence will be disabled.`
)
return false
}
return true
}
export default function getStorage(type: string): Storage {
const storageType = `${type}Storage`
if (hasStorage(storageType)) return self[storageType]
else {
if (process.env.NODE_ENV !== 'production') {
console.error(
`redux-persist failed to create sync storage. falling back to noop storage.`
)
}
return noopStorage
}
}