« get me outta code hell

client: initInfo: session storage - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-04-23 15:04:15 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-05 12:25:58 -0300
commitcaab765ef3e4eb10e020adb53e3af4036c61e78d (patch)
treeecca138e7ae5a38c9dab899ec34e226a6a16cd97 /src
parentc80bdf0b5ba39d3cac6f8635c3df749752b38760 (diff)
client: initInfo: session storage
Diffstat (limited to 'src')
-rw-r--r--src/static/client3.js44
1 files 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;
 }