use-idb-store
Version:
A React state hook that syncs state to IndexedDB for persistent, offline-friendly, and large-scale data storage.
50 lines (49 loc) • 1.72 kB
TypeScript
import { StoreEvent, StoreEventCallback } from "./types";
export declare class Store<T = any> {
private static instance;
protected name: string;
private db;
private schema?;
private isInitialized;
private initPromise;
private eventListeners;
private readonly MAX_RETRIES;
private readonly RETRY_DELAY;
private constructor();
static getInstance<T>(name: string, schema?: IDBObjectStoreParameters): Store<T>;
setup(): Promise<void>;
private ensureInitialized;
private getStore;
/**
* Retries an operation if it fails due to database version changes in other tabs
*/
private retryOperation;
getItem(key: string): Promise<T | null>;
getAllItems(): Promise<Record<string, T>>;
addItem(key: string, value: T): Promise<IDBValidKey>;
addOrUpdateItem(key: string, value: T): Promise<IDBValidKey>;
updateItem(key: string, value: Partial<T>): Promise<IDBValidKey>;
deleteItem(key: string): Promise<void>;
/**
* Emits an event to all registered listeners for a specific event type
* @param event - The type of event to emit ('change' or 'error')
* @param detail - The detail to emit with the event
*/
emit(event: StoreEvent, detail: any): void;
/**
* Registers an event listener for the specified store event
*/
on(event: StoreEvent, callback: StoreEventCallback): void;
/**
* Removes an event listener from the store
*/
off(event: StoreEvent, callback: StoreEventCallback): void;
/**
* Clears all items from the store.
*/
clear(): Promise<void>;
/**
* Destroys the store and deletes it from the database.
*/
destroy(): Promise<void>;
}