From c8a23ccb83a28808517915d3b76a8b8159e6ed4d Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sat, 10 Sep 2022 21:47:20 +0200 Subject: new prefs --- client-web/source/preferences/mod.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'client-web/source/preferences/mod.ts') diff --git a/client-web/source/preferences/mod.ts b/client-web/source/preferences/mod.ts index 5b33924..5f238bb 100644 --- a/client-web/source/preferences/mod.ts +++ b/client-web/source/preferences/mod.ts @@ -1,3 +1,4 @@ +import { log } from "../logger.ts"; import { PREF_DECLS } from "./decl.ts"; @@ -15,7 +16,15 @@ type TypeMapper = { "string": string, "number": number, "boolean": boolean } type PrefMap = { [Key in keyof T]: T[Key]["type"] } type Optional = { [Key in keyof T]?: T[Key] } export const { prefs: PREFS, explicit: PREFS_EXPLICIT } = register_prefs(PREF_DECLS) -export const on_pref_change_handlers: ((key: keyof typeof PREFS) => void)[] = [] +const pref_change_handlers: Map unknown>> = new Map() +export const on_pref_changed = (key: keyof typeof PREFS, cb: () => unknown) => + (pref_change_handlers.get(key) + ?? (() => { + const n = new Set<() => unknown>(); + pref_change_handlers.set(key, n); + return n + })() + ).add(cb) export function register_prefs>>(ds: T): { prefs: PrefMap, explicit: Optional> } { const prefs: PrefMap = {} as PrefMap @@ -33,10 +42,12 @@ export function register_prefs>>(ds: } export function change_pref(key: T, value: typeof PREFS[T]) { + log("*", `pref changed: ${key}`) PREFS[key] = value if ((PREF_DECLS as Record>)[key].default != value) PREFS_EXPLICIT[key] = value else delete PREFS_EXPLICIT[key] + pref_change_handlers.get(key)?.forEach(h => h()) window.location.hash = "#" + generate_section() } export function generate_section(): string { -- cgit v1.2.3-70-g09d2