Fix l'erreur 'unsafe operation' lors de l'accès au localstorage
parent
d077eb6e83
commit
aac8414a89
|
@ -2,6 +2,7 @@
|
|||
|
||||
import type { Store } from 'redux'
|
||||
import { debounce } from '../utils'
|
||||
import safeLocalStorage from './safeLocalStorage'
|
||||
import type { State } from 'Types/State'
|
||||
import type { Action } from 'Types/ActionsTypes'
|
||||
|
||||
|
@ -12,12 +13,12 @@ const LOCAL_STORAGE_KEY = 'mycompanyinfrance::persisted-everything:v' + VERSION
|
|||
export function persistEverything(store: Store<State, Action>) {
|
||||
const listener = () => {
|
||||
const state = store.getState()
|
||||
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(state))
|
||||
safeLocalStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(state))
|
||||
}
|
||||
store.subscribe(debounce(1000, listener))
|
||||
}
|
||||
|
||||
export function retrievePersistedState(): ?State {
|
||||
const serializedState = window.localStorage.getItem(LOCAL_STORAGE_KEY)
|
||||
const serializedState = safeLocalStorage.getItem(LOCAL_STORAGE_KEY)
|
||||
return serializedState ? JSON.parse(serializedState) : null
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import type { Store } from 'redux'
|
||||
import { debounce } from '../utils'
|
||||
import safeLocalStorage from './safeLocalStorage'
|
||||
import { deserialize, serialize } from './serializeSimulation'
|
||||
import type { State, SavedSimulation } from '../types/State'
|
||||
import type { Action } from 'Types/ActionsTypes'
|
||||
|
@ -16,16 +17,16 @@ export function persistSimulation(store: Store<State, Action>) {
|
|||
if (!state.conversationStarted) {
|
||||
return
|
||||
}
|
||||
window.localStorage.setItem(LOCAL_STORAGE_KEY, serialize(state))
|
||||
safeLocalStorage.setItem(LOCAL_STORAGE_KEY, serialize(state))
|
||||
}
|
||||
store.subscribe(debounce(1000, listener))
|
||||
}
|
||||
|
||||
export function retrievePersistedSimulation(): ?SavedSimulation {
|
||||
const serializedState = window.localStorage.getItem(LOCAL_STORAGE_KEY)
|
||||
const serializedState = safeLocalStorage.getItem(LOCAL_STORAGE_KEY)
|
||||
return serializedState ? deserialize(serializedState) : null
|
||||
}
|
||||
|
||||
export function deletePersistedSimulation(): void {
|
||||
window.localStorage.removeItem(LOCAL_STORAGE_KEY)
|
||||
safeLocalStorage.removeItem(LOCAL_STORAGE_KEY)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
export default {
|
||||
removeItem: function(...args) {
|
||||
try {
|
||||
return window.localStorage.removeItem(...args)
|
||||
} catch (error) {
|
||||
if (error.name === 'SecurityError') {
|
||||
console.warn(
|
||||
'[localStorage] Unable to remove item due to security settings'
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
},
|
||||
getItem: function(...args) {
|
||||
try {
|
||||
return window.localStorage.getItem(...args)
|
||||
} catch (error) {
|
||||
if (error.name === 'SecurityError') {
|
||||
console.warn(
|
||||
'[localStorage] Unable to get item due to security settings'
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
},
|
||||
setItem: function(...args) {
|
||||
try {
|
||||
return window.localStorage.setItem(...args)
|
||||
} catch (error) {
|
||||
if (error.name === 'SecurityError') {
|
||||
console.warn(
|
||||
'[localStorage] Unable to set item due to security settings'
|
||||
)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue