Add TypedStorageArea

This commit is contained in:
hensm
2019-09-30 10:40:53 +01:00
parent e1b15a12a2
commit d1dd2b188c
2 changed files with 87 additions and 4 deletions

View File

@@ -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<EventMap> {
* Options interface type.
*/
public async getAll (): Promise<Options> {
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<EventMap> {
* Returns storage promise.
*/
public async setAll (options: Options): Promise<void> {
return browser.storage.sync.set({ options });
return storageArea.set({ options })
}
/**

View File

@@ -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<Schema extends { [key: string]: any }> {
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<SchemaKey extends keyof Schema
, SchemaPartial extends Partial<Schema>> (
keys?: SchemaKey
| Array<SchemaKey>
| SchemaPartial
| null | undefined)
: Promise<Pick<Schema, Extract<
keyof SchemaPartial, SchemaKey>>> {
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<SchemaKey extends keyof Schema> (
keys?: Schema | Array<SchemaKey>): Promise<number> {
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<Schema>): Promise<void> {
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<SchemaKey extends keyof Schema> (
keys: SchemaKey | Array<SchemaKey>): Promise<void> {
await this.storageArea.remove(keys);
}
/**
* Removes all items from the storage area.
*/
public async clear (): Promise<void> {
await this.storageArea.clear();
}
}