UNPKG

@cedx/webstorage

Version:

Service for interacting with the Web Storage.

104 lines 3.52 kB
import { StorageEvent } from "./StorageEvent.js"; /** * Provides access to the [Web Storage](https://developer.mozilla.org/docs/Web/API/Web_Storage_API). */ export declare class Storage extends EventTarget implements Disposable, Iterable<[string, any], void, void> { #private; /** * The `change` event type. */ static readonly changeEvent = "webstorage:change"; /** * Creates a new storage service. * @param backend The underlying data store. * @param options An object providing values to initialize this instance. */ private constructor(); /** * The keys of this storage. */ get keys(): Set<string>; /** * The number of entries in this storage. */ get length(): number; /** * Creates a new local storage service. * @param options An object providing values to initialize the service. * @returns The newly created service. */ static local(options?: StorageOptions): Storage; /** * Creates a new session storage service. * @param options An object providing values to initialize the service. * @returns The newly created service. */ static session(options?: StorageOptions): Storage; /** * Releases any resources associated with this object. */ [Symbol.dispose](): void; /** * Returns a new iterator that allows iterating the entries of this storage. * @returns An iterator for the entries of this storage. */ [Symbol.iterator](): Iterator<[string, any], void, void>; /** * Removes all entries from this storage. */ clear(): void; /** * Removes the value associated with the specified key. * @param key The storage key. * @returns The value associated with the key before it was removed. */ delete<T>(key: string): T | null; /** * Cancels the subscription to the global storage events. */ dispose(): void; /** * Gets the deserialized value associated with the specified key. * @param key The storage key. * @returns The storage value, or `null` if the key does not exist or the value cannot be deserialized. */ get<T>(key: string): T | null; /** * Gets a value indicating whether this storage contains the specified key. * @param key The storage key. * @returns `true` if this storage contains the specified key, otherwise `false`. */ has(key: string): boolean; /** * Registers a function that will be invoked whenever the `change` event is triggered. * @param listener The event handler to register. * @returns This instance. */ onChange(listener: (event: StorageEvent) => void): this; /** * Serializes and associates a given `value` with the specified `key`. * @param key The storage key. * @param value The storage value. * @returns This instance. */ set(key: string, value: unknown): this; /** * Returns a JSON representation of this object. * @returns The JSON representation of this object. */ toJSON(): Array<[string, any]>; } /** * Defines the options of a {@link Storage} instance. */ export type StorageOptions = Partial<{ /** * A string prefixed to every key so that it is unique globally in the whole storage. */ keyPrefix: string; /** * Value indicating whether to listen to the global storage events. */ listenToGlobalEvents: boolean; }>; //# sourceMappingURL=Storage.d.ts.map