overleaf-cep/services/web/frontend/js/infrastructure/session-storage.ts
Alf Eaton 2fbb4615f9 Convert utility functions to TypeScript (#22658)
* Convert event-tracking to TypeScript

* Convert local-storage to TypeScript

* Convert mapSeries to TypeScript

* Convert SessionStorage to TypeScript

* Convert account-upgrade to TypeScript

* Convert isValidTeXFile to TypeScript

* Convert date functions to TypeScript

* Convert EventEmitter to TypeScript

* Convert isNetworkError to TypeScript

* Convert webpack-public-path to TypeScript

* Convert displayNameForUser to TypeScript

GitOrigin-RevId: 79c5a2d1101fcd520f3116f0f4af29d974189d94
2025-01-16 09:05:36 +00:00

52 lines
1.3 KiB
TypeScript

/**
* sessionStorage can throw browser exceptions, for example if it is full.
* We don't use sessionStorage for anything critical, so in that case just fail gracefully.
*/
import { debugConsole } from '@/utils/debugging'
/**
* Catch, log and otherwise ignore errors.
*
* @param {function} fn sessionStorage function to call
* @param {string?} key Key passed to the sessionStorage function (if any)
* @param {any?} value Value passed to the sessionStorage function (if any)
*/
const callSafe = function (
fn: (...args: any) => any,
key?: string,
value?: any
) {
try {
return fn(key, value)
} catch (e) {
debugConsole.error('sessionStorage exception', e)
return null
}
}
const getItem = function (key: string) {
const value = sessionStorage.getItem(key)
return value === null ? null : JSON.parse(value)
}
const setItem = function (key: string, value: any) {
sessionStorage.setItem(key, JSON.stringify(value))
}
const clear = function () {
sessionStorage.clear()
}
const removeItem = function (key: string) {
return sessionStorage.removeItem(key)
}
const customSessionStorage = {
getItem: (key: string) => callSafe(getItem, key),
setItem: (key: string, value: any) => callSafe(setItem, key, value),
clear: () => callSafe(clear),
removeItem: (key: string) => callSafe(removeItem, key),
}
export default customSessionStorage