From caab765ef3e4eb10e020adb53e3af4036c61e78d Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 23 Apr 2024 15:04:15 -0300 Subject: client: initInfo: session storage --- src/static/client3.js | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/static/client3.js b/src/static/client3.js index 7d6544a0..64f5b377 100644 --- a/src/static/client3.js +++ b/src/static/client3.js @@ -18,7 +18,7 @@ const clientSteps = { addPageListeners: [], }; -function initInfo(key, description) { +function initInfo(infoKey, description) { const object = {...description}; for (const obj of [ @@ -31,7 +31,47 @@ function initInfo(key, description) { Object.preventExtensions(obj); } - clientInfo[key] = object; + if (object.session) { + const sessionDefaults = object.session; + + object.session = {}; + + for (const [key, defaultValue] of Object.entries(sessionDefaults)) { + const storageKey = `hsmusic.${infoKey}.${key}`; + + let fallbackValue = defaultValue; + + Object.defineProperty(object.session, key, { + get: () => { + try { + return sessionStorage.getItem(storageKey) ?? defaultValue; + } catch (error) { + if (error instanceof DOMException) { + return fallbackValue; + } else { + throw error; + } + } + }, + + set: (value) => { + try { + sessionStorage.setItem(storageKey, value); + } catch (error) { + if (error instanceof DOMException) { + fallbackValue = value; + } else { + throw error; + } + } + }, + }); + } + + Object.preventExtensions(object.session); + } + + clientInfo[infoKey] = object; return object; } -- cgit 1.3.0-6-gf8a5