132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 1 | import { get, writable } from 'svelte/store'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 2 | import { getAccountData, setAccountData } from './api'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 3 | import { parseDisabledPackImages, withImageEnabled } from './pack-preferences-model'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 4 | import type { DisabledPackImages } from './pack-preferences-model'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 5 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 6 | const EVENT_TYPE = 'dev.malice.pack_preferences'; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 7 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 8 | export const disabledPackImages = writable<DisabledPackImages>({}); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 9 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 10 | let loadGeneration = 0; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 11 | let accountGeneration = 0; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 12 | let pendingSaves = 0; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 13 | let persistedPreferences: DisabledPackImages = {}; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 14 | let saveQueue: Promise<void> = Promise.resolve(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 15 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 16 | export async function loadPackPreferences(): Promise<void> { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 17 | if (pendingSaves > 0) return; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 18 | const generation = ++loadGeneration; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 19 | const epoch = accountGeneration; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 20 | try { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 21 | const preferences = parseDisabledPackImages(await getAccountData(EVENT_TYPE)); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 22 | if (generation !== loadGeneration || epoch !== accountGeneration || pendingSaves > 0) return; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 23 | persistedPreferences = preferences; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 24 | disabledPackImages.set(preferences); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 25 | } catch {} |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 26 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 27 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 28 | export function setPackImageEnabled(packId: string, shortcode: string, enabled: boolean): Promise<void> { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 29 | const previous = get(disabledPackImages); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 30 | const next = withImageEnabled(previous, packId, shortcode, enabled); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 31 | if (next === previous) return Promise.resolve(); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 32 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 33 | const epoch = accountGeneration; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 34 | loadGeneration += 1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 35 | pendingSaves += 1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 36 | disabledPackImages.set(next); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 37 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 38 | const save = saveQueue.then(async () => { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 39 | if (epoch !== accountGeneration) return; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 40 | await setAccountData(EVENT_TYPE, { disabled: next }); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 41 | if (epoch === accountGeneration) persistedPreferences = next; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 42 | }); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 43 | saveQueue = save.catch(() => undefined); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 44 | return save.catch((error: unknown) => { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 45 | if (epoch === accountGeneration && get(disabledPackImages) === next) { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 46 | loadGeneration += 1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 47 | disabledPackImages.set(persistedPreferences); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 48 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 49 | throw error; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 50 | }).finally(() => { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 51 | if (epoch === accountGeneration) pendingSaves -= 1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 52 | }); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 53 | } |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 54 | |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 55 | export function resetPackPreferences(): void { |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 56 | accountGeneration += 1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 57 | loadGeneration += 1; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 58 | pendingSaves = 0; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 59 | persistedPreferences = {}; |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 60 | disabledPackImages.set({}); |
132eeceeb362 | atagen | 2026-10-25 03:34 UTC | 61 | } |