diff --git a/ext/src/lib/options.ts b/ext/src/lib/options.ts index 6c7da21..e5d81f8 100644 --- a/ext/src/lib/options.ts +++ b/ext/src/lib/options.ts @@ -4,8 +4,13 @@ import defaultOptions from "../defaultOptions"; import { ReceiverSelectorType } from "../background/receiverSelector"; import { TypedEventTarget } from "./typedEvents"; +import { TypedStorageArea } from "./typedStorage"; +const storageArea = new TypedStorageArea<{ + options: Options +}>(browser.storage.sync); + export interface Options { bridgeApplicationName: string; mediaEnabled: boolean; @@ -89,9 +94,7 @@ export default new class extends TypedEventTarget { * Options interface type. */ public async getAll (): Promise { - const { options } = await browser.storage.sync.get( - "options") as { options: Options }; - + const { options } = await storageArea.get("options"); return options; } @@ -100,7 +103,7 @@ export default new class extends TypedEventTarget { * Returns storage promise. */ public async setAll (options: Options): Promise { - return browser.storage.sync.set({ options }); + return storageArea.set({ options }) } /** diff --git a/ext/src/lib/typedStorage.ts b/ext/src/lib/typedStorage.ts new file mode 100644 index 0000000..f01d1de --- /dev/null +++ b/ext/src/lib/typedStorage.ts @@ -0,0 +1,80 @@ +"use strict"; + +/** + * Allows typed access to a StorageArea. + * + * Provide a string-keyed schema as a type parameter with + * the specified storage area. + */ +export class TypedStorageArea { + private storageArea: any; + + constructor (storageArea: browser.storage.StorageArea) { + this.storageArea = storageArea; + } + + /** + * Retrieves one or more items from the storage area. + * + * @param keys - + * A string, array of strings or partial schema object + * (with default values) indicating which keys to retrieve + * from storage. + */ + public async get> ( + keys?: SchemaKey + | Array + | SchemaPartial + | null | undefined) + : Promise>> { + + return await this.storageArea.get(keys); + } + + /** + * Gets the amount of storage space — in bytes — used by one + * or more items in the storage area. + * + * @param keys - + * A string or array of strings indicating the keys of + * which to get the storage space. + */ + public async getBytesInUse ( + keys?: Schema | Array): Promise { + + return await this.storageArea.getBytesInUse(keys); + } + + /** + * Stores one or more items in the storage area. + * + * @param keys - + * A partial schema object containing the items to be + * stored or updated. + */ + public async set (keys: Partial): Promise { + await this.storageArea.set(keys); + } + + /** + * Removes one or more items from the storage area. + * + * @param keys - + * A string or array of strings indicating which keys to + * remove from storage. + */ + public async remove ( + keys: SchemaKey | Array): Promise { + + await this.storageArea.remove(keys); + } + + /** + * Removes all items from the storage area. + */ + public async clear (): Promise { + await this.storageArea.clear(); + } +}