sync-idb-kvs
Version:
Synchronous IndexedDB Key-Value Store(Requires async Initialization)
57 lines (56 loc) • 2.05 kB
TypeScript
import MutablePromise from "mutable-promise";
export interface IStorage<T> {
storageType: string;
setItem(key: string, value: T): void;
getItem(key: string): T | null;
removeItem(key: string): void;
itemExists(key: string): boolean;
keys(): IterableIterator<string>;
reload(key: string): Promise<T | null>;
waitForCommit(): Promise<void>;
}
export type SyncIDBStorageOptions = {
lazy?: 0 | 1 | 2;
};
export declare class SyncIDBStorage<T> implements IStorage<T> {
asyncStorage: AsyncIDBStorage<T>;
channelName: string;
storageType: string;
memoryCache: Record<string, T>;
loadedAll: boolean;
loadingPromise?: Promise<void>;
passiveLoadingPromise: MutablePromise<unknown>;
getLoadingPromise(passive?: boolean): MutablePromise<unknown> | Promise<void>;
static create<T>(dbName: string, initialData: Record<string, T>, opt?: SyncIDBStorageOptions): Promise<SyncIDBStorage<T>>;
ensureLoaded(): void;
constructor(asyncStorage: AsyncIDBStorage<T>, channelName: string);
getItem(key: string): T | null;
setItem(key: string, value: T): void;
removeItem(key: string): void;
itemExists(key: string): boolean;
keys(): IterableIterator<string>;
reload(key: string): Promise<T | null>;
waitForCommit(): Promise<void>;
}
export declare function idbReqPromise<T>(request: IDBRequest<T>): Promise<T>;
export declare class AsyncIDBStorage<T> {
dbName: string;
initialData: Record<string, T>;
private db;
uncommitedCounter: UncommitCounter;
constructor(dbName: string | undefined, initialData: Record<string, T>);
initDB(s: SyncIDBStorage<T>): Promise<void>;
loadAllData(s: SyncIDBStorage<T>): Promise<void>;
getItem(key: string): Promise<T | null>;
setItem(key: string, value: T): Promise<void>;
removeItem(key: string): Promise<void>;
waitForCommit(): Promise<void>;
}
declare class UncommitCounter {
private value;
private promise;
inc(): void;
dec(): void;
wait(): Promise<void>;
}
export {};