2021-04-14 23:46:13 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import { Writable, writable, get } from "svelte/store";
|
|
|
|
|
|
|
|
export interface CustomStore<T> extends Writable<T> {
|
|
|
|
subscribe: (getter: (value: T) => void) => () => void;
|
|
|
|
set: (value: T) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PreferenceStore<T> = {
|
|
|
|
[K in keyof Omit<T, "toJSON">]: CustomStore<T[K]>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type PreferencePayload<T> = {
|
|
|
|
[K in keyof Omit<T, "toJSON">]: T[K];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type PreferenceRaw<T> = {
|
|
|
|
[K in keyof T]: T[K];
|
|
|
|
};
|
|
|
|
|
|
|
|
function createPreference<T>(
|
|
|
|
initialValue: T,
|
2021-10-19 01:06:00 +02:00
|
|
|
savePreferences: () => void,
|
2021-04-14 23:46:13 +02:00
|
|
|
): CustomStore<T> {
|
|
|
|
const { subscribe, set, update } = writable(initialValue);
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscribe,
|
|
|
|
set: (value: T): void => {
|
|
|
|
set(value);
|
|
|
|
savePreferences();
|
|
|
|
},
|
|
|
|
update: (updater: (value: T) => T): void => {
|
|
|
|
update(updater);
|
|
|
|
savePreferences();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function preparePreferences<T>(
|
|
|
|
Preferences: T,
|
|
|
|
setter: (payload: PreferencePayload<T>) => Promise<void>,
|
2021-10-19 01:06:00 +02:00
|
|
|
toObject: (preferences: T, options: { defaults: boolean }) => PreferenceRaw<T>,
|
2021-04-14 23:46:13 +02:00
|
|
|
): PreferenceStore<T> {
|
|
|
|
const preferences: Partial<PreferenceStore<T>> = {};
|
|
|
|
|
|
|
|
function constructPreferences(): PreferencePayload<T> {
|
|
|
|
const payload: Partial<PreferencePayload<T>> = {};
|
|
|
|
|
|
|
|
for (const key in preferences as PreferenceStore<T>) {
|
|
|
|
payload[key] = get(preferences[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload as PreferencePayload<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
function savePreferences(): void {
|
|
|
|
setter(constructPreferences());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(
|
2021-10-19 01:06:00 +02:00
|
|
|
toObject(Preferences, { defaults: true }),
|
2021-04-14 23:46:13 +02:00
|
|
|
)) {
|
|
|
|
preferences[key] = createPreference(value, savePreferences);
|
|
|
|
}
|
|
|
|
|
|
|
|
return preferences as PreferenceStore<T>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPreferences<T>(
|
|
|
|
getter: () => Promise<T>,
|
|
|
|
setter: (payload: PreferencePayload<T>) => Promise<void>,
|
2021-10-19 01:06:00 +02:00
|
|
|
toObject: (preferences: T, options: { defaults: boolean }) => PreferenceRaw<T>,
|
2021-04-14 23:46:13 +02:00
|
|
|
): Promise<PreferenceStore<T>> {
|
|
|
|
const initialPreferences = await getter();
|
|
|
|
return preparePreferences(initialPreferences, setter, toObject);
|
|
|
|
}
|