Wrap local/session storage calls in try/catch (take 2) (#3066)
Steve tried this in #3043, but we reverted it in #3063. Steve's version added `JSON.parse`/`JSON.stringify` to the helpers without checking for where we were already `JSON.parse`ing (or not). In some places we just store strings directly rather than wanting them jsonified, so in this version we leave the jsonification to the callers - the helpers just do the reading/writing and return the string values. ### Change Type - [x] `patch` — Bug fix
This commit is contained in:
parent
8adaaf8e22
commit
ce782dc70b
13 changed files with 204 additions and 51 deletions
|
@ -1,4 +1,5 @@
|
|||
import { Atom, atom, react } from '@tldraw/state'
|
||||
import { deleteFromSessionStorage, getFromSessionStorage, setInSessionStorage } from '@tldraw/utils'
|
||||
|
||||
// --- 1. DEFINE ---
|
||||
//
|
||||
|
@ -126,14 +127,10 @@ function createDebugValueBase<T>(def: DebugFlagDef<T>): DebugFlag<T> {
|
|||
if (def.shouldStoreForSession) {
|
||||
react(`debug:${def.name}`, () => {
|
||||
const currentValue = valueAtom.get()
|
||||
try {
|
||||
if (currentValue === defaultValue) {
|
||||
window.sessionStorage.removeItem(`tldraw_debug:${def.name}`)
|
||||
} else {
|
||||
window.sessionStorage.setItem(`tldraw_debug:${def.name}`, JSON.stringify(currentValue))
|
||||
}
|
||||
} catch {
|
||||
// not a big deal
|
||||
if (currentValue === defaultValue) {
|
||||
deleteFromSessionStorage(`tldraw_debug:${def.name}`)
|
||||
} else {
|
||||
setInSessionStorage(`tldraw_debug:${def.name}`, JSON.stringify(currentValue))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -154,7 +151,7 @@ function createDebugValueBase<T>(def: DebugFlagDef<T>): DebugFlag<T> {
|
|||
|
||||
function getStoredInitialValue(name: string) {
|
||||
try {
|
||||
return JSON.parse(window?.sessionStorage.getItem(`tldraw_debug:${name}`) ?? 'null')
|
||||
return JSON.parse(getFromSessionStorage(`tldraw_debug:${name}`) ?? 'null')
|
||||
} catch (err) {
|
||||
return null
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue