1
Fork 0
tildes-shepherd/source/storage/common.ts

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-06-15 12:56:59 +00:00
import browser from "webextension-polyfill";
import {createValue} from "@holllo/webextension-storage";
2023-07-01 10:41:54 +00:00
import {type TourId} from "../tours/exports.js";
2023-06-15 12:56:59 +00:00
/** All available storage keys. */
2023-06-15 12:56:59 +00:00
export enum StorageKey {
IntroductionUnderstood = "introduction-understood",
ToursCompleted = "tours-completed",
}
/** All values we want to save in storage. */
const storageValues = {
[StorageKey.IntroductionUnderstood]: createValue<boolean>({
2023-06-15 12:56:59 +00:00
deserialize: (input) => input === "true",
serialize: (input) => JSON.stringify(input),
key: StorageKey.IntroductionUnderstood,
storage: browser.storage.local,
value: false,
}),
[StorageKey.ToursCompleted]: createValue<Set<TourId>>({
2023-06-15 12:56:59 +00:00
deserialize: (input) => new Set(JSON.parse(input) as TourId[]),
serialize: (input) => JSON.stringify(Array.from(input)),
key: StorageKey.ToursCompleted,
storage: browser.storage.local,
value: new Set([]),
}),
};
/** Alias to get the inferred type shape of {@link storageValues}. */
export type StorageValues = typeof storageValues;
2023-06-15 12:56:59 +00:00
/**
* Get the stored value for a given key.
* @param key The key to get from storage.
*/
export async function fromStorage<K extends StorageKey>(
key: K,
): Promise<StorageValues[K]> {
return storageValues[key];
2023-06-15 12:56:59 +00:00
}