diff options
author | metamuffin <metamuffin@disroot.org> | 2022-09-10 21:47:20 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2022-09-10 21:47:20 +0200 |
commit | c8a23ccb83a28808517915d3b76a8b8159e6ed4d (patch) | |
tree | 3fe486d6a88ea37c264f35f20175a5e2103f78d6 /client-web/source/preferences/mod.ts | |
parent | abf0ae46f9dd5324e691f939c63b6e97dea7c6b7 (diff) | |
download | keks-meet-c8a23ccb83a28808517915d3b76a8b8159e6ed4d.tar keks-meet-c8a23ccb83a28808517915d3b76a8b8159e6ed4d.tar.bz2 keks-meet-c8a23ccb83a28808517915d3b76a8b8159e6ed4d.tar.zst |
new prefs
Diffstat (limited to 'client-web/source/preferences/mod.ts')
-rw-r--r-- | client-web/source/preferences/mod.ts | 13 |
1 files changed, 12 insertions, 1 deletions
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<T extends { [key: string]: { type: unknown } }> = { [Key in keyof T]: T[Key]["type"] } type Optional<T extends { [key: string]: unknown }> = { [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<keyof typeof PREFS, Set<() => 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<T extends Record<string, PrefDecl<unknown>>>(ds: T): { prefs: PrefMap<T>, explicit: Optional<PrefMap<T>> } { const prefs: PrefMap<T> = {} as PrefMap<T> @@ -33,10 +42,12 @@ export function register_prefs<T extends Record<string, PrefDecl<unknown>>>(ds: } export function change_pref<T extends keyof typeof PREFS>(key: T, value: typeof PREFS[T]) { + log("*", `pref changed: ${key}`) PREFS[key] = value if ((PREF_DECLS as Record<string, PrefDecl<unknown>>)[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 { |