mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-12 02:29:59 +00:00
Add TypedStorageArea
This commit is contained in:
@@ -4,8 +4,13 @@ import defaultOptions from "../defaultOptions";
|
|||||||
|
|
||||||
import { ReceiverSelectorType } from "../background/receiverSelector";
|
import { ReceiverSelectorType } from "../background/receiverSelector";
|
||||||
import { TypedEventTarget } from "./typedEvents";
|
import { TypedEventTarget } from "./typedEvents";
|
||||||
|
import { TypedStorageArea } from "./typedStorage";
|
||||||
|
|
||||||
|
|
||||||
|
const storageArea = new TypedStorageArea<{
|
||||||
|
options: Options
|
||||||
|
}>(browser.storage.sync);
|
||||||
|
|
||||||
export interface Options {
|
export interface Options {
|
||||||
bridgeApplicationName: string;
|
bridgeApplicationName: string;
|
||||||
mediaEnabled: boolean;
|
mediaEnabled: boolean;
|
||||||
@@ -89,9 +94,7 @@ export default new class extends TypedEventTarget<EventMap> {
|
|||||||
* Options interface type.
|
* Options interface type.
|
||||||
*/
|
*/
|
||||||
public async getAll (): Promise<Options> {
|
public async getAll (): Promise<Options> {
|
||||||
const { options } = await browser.storage.sync.get(
|
const { options } = await storageArea.get("options");
|
||||||
"options") as { options: Options };
|
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +103,7 @@ export default new class extends TypedEventTarget<EventMap> {
|
|||||||
* Returns storage promise.
|
* Returns storage promise.
|
||||||
*/
|
*/
|
||||||
public async setAll (options: Options): Promise<void> {
|
public async setAll (options: Options): Promise<void> {
|
||||||
return browser.storage.sync.set({ options });
|
return storageArea.set({ options })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
80
ext/src/lib/typedStorage.ts
Normal file
80
ext/src/lib/typedStorage.ts
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user