Fix: see #2212: actually save custom themes as visited

This commit is contained in:
Pieter Vander Vennet 2024-10-17 02:10:25 +02:00
parent 91f5c8f166
commit 9427083939
19 changed files with 129 additions and 75 deletions

View file

@ -4,8 +4,11 @@ import { UIEventSource } from "../UIEventSource"
* UIEventsource-wrapper around localStorage
*/
export class LocalStorageSource {
static GetParsed<T>(key: string, defaultValue: T): UIEventSource<T> {
return LocalStorageSource.Get(key).sync(
private static readonly _cache: Record<string, UIEventSource<string>> = {}
static getParsed<T>(key: string, defaultValue: T): UIEventSource<T> {
return LocalStorageSource.get(key).sync(
(str) => {
if (str === undefined) {
return defaultValue
@ -17,34 +20,40 @@ export class LocalStorageSource {
}
},
[],
(value) => JSON.stringify(value)
(value) => JSON.stringify(value),
)
}
static Get(key: string, defaultValue: string = undefined): UIEventSource<string> {
static get(key: string, defaultValue: string = undefined): UIEventSource<string> {
const cached = LocalStorageSource._cache[key]
if (cached) {
return cached
}
let saved = defaultValue
try {
let saved = localStorage.getItem(key)
saved = localStorage.getItem(key)
if (saved === "undefined") {
saved = undefined
}
const source = new UIEventSource<string>(saved ?? defaultValue, "localstorage:" + key)
source.addCallback((data) => {
if(data === undefined || data === "" || data === null){
localStorage.removeItem(key)
return
}
try {
localStorage.setItem(key, data)
} catch (e) {
// Probably exceeded the quota with this item!
// Lets nuke everything
localStorage.clear()
}
})
return source
} catch (e) {
return new UIEventSource<string>(defaultValue)
console.error("Could not get value", key, "from local storage")
}
const source = new UIEventSource<string>(saved ?? defaultValue, "localstorage:" + key)
source.addCallback((data) => {
if (data === undefined || data === "" || data === null) {
localStorage.removeItem(key)
return
}
try {
localStorage.setItem(key, data)
} catch (e) {
// Probably exceeded the quota with this item!
// Let's nuke everything
localStorage.clear()
}
})
LocalStorageSource._cache[key] = source
return source
}
}