import type pb from "anki/backend_proto"; import { getGraphPreferences, setGraphPreferences } from "./graph-helpers"; import { writable, get } from "svelte/store"; export interface CustomStore { subscribe: (getter: (value: T) => void) => () => void; set: (value: T) => void; } export type PreferenceStore = { [K in keyof pb.BackendProto.GraphsPreferencesOut]: CustomStore< pb.BackendProto.GraphsPreferencesOut[K] >; }; function createPreference( initialValue: T, savePreferences: () => void ): CustomStore { const { subscribe, set } = writable(initialValue); return { subscribe, set: (v: T): void => { set(v); savePreferences(); }, }; } function preparePreferences( graphsPreferences: pb.BackendProto.GraphsPreferencesOut ): PreferenceStore { const preferences: Partial = {}; function constructPreferences(): pb.BackendProto.GraphsPreferencesOut { const payload: Partial = {}; for (const [key, pref] of Object.entries(preferences as PreferenceStore)) { payload[key] = get(pref as any); } return payload as pb.BackendProto.GraphsPreferencesOut; } function savePreferences(): void { setGraphPreferences(constructPreferences()); } for (const [key, value] of Object.entries(graphsPreferences)) { preferences[key] = createPreference(value, savePreferences); } return preferences as PreferenceStore; } export async function getPreferences() { const initialPreferences = await getGraphPreferences(); return preparePreferences(initialPreferences); }