/** * Configuration passed to `createIndexedDB()`. * * NOTE: this module manages a single global IndexedDB connection. Calling * `createIndexedDB()` more than once replaces the previous configuration * for any *future* connection attempts, but it will NOT reopen an * already-open connection with a different name/version. If you need more * than one database in the same app, namespace your stores within a single * database instead of calling `createIndexedDB()` multiple times. */ export interface IndexedDBConfig { /** Name of the IndexedDB database. */ dbName: string; /** Schema version. Bump this whenever `stores` changes. */ version?: number; /** Object store names to create (out-of-line keys, no keyPath). */ stores: string[]; } export type IndexedDBStatus = "idle" | "loading" | "ready" | "error"; export interface IndexedDBMeta { status: IndexedDBStatus; error: Error | null; /** Deletes the record from IndexedDB and resets local state to initialValue. */ remove: () => Promise; } type Updater = T | ((prev: T) => T); type SetValue = (value: Updater) => void; /** * Registers the IndexedDB configuration used by all `useIndexedDB` calls. * Safe to call multiple times (e.g. in tests); the connection itself is * opened lazily and only once, the first time a hook actually needs it. * * @param {IndexedDBConfig} config The configuration object defining the database. * @param {string} config.dbName Name of the IndexedDB database. * @param {number} [config.version] Schema version. Bump this whenever `stores` changes. * @param {string[]} config.stores Object store names to create (out-of-line keys, no keyPath). */ export declare function createIndexedDB(config: IndexedDBConfig): void; /** * React hook for storing and retrieving a single value in IndexedDB. * * ```ts * const [theme, setTheme, meta] = useIndexedDB('settings', 'theme', 'light'); * ``` * * - `value` starts as `initialValue` and is replaced by the persisted * value (if any) once the read from IndexedDB resolves. * - `setValue` accepts either a new value or an updater `(prev) => next`, * mirroring `useState`. Writes are optimistic: local (and other * same-tab) state updates immediately, the IndexedDB write happens * asynchronously, and other tabs are notified once it succeeds. * - `meta.status` reflects the IndexedDB read lifecycle only ("idle" before * the initial read has started, "loading" while it's in flight, "ready" * * @template T The type of the value being stored. * @param {string} storeName The name of the IndexedDB object store to use. * @param {IDBValidKey} key The key under which the value is stored. * @param {T | (() => T)} initialValue The initial value to use before the data is loaded from IndexedDB, or a function returning it. * @param {Object} [options] Optional configuration. * @param {boolean} [options.enabled=true] If false, pauses the hook from reading/writing to IndexedDB. * @returns {[T, (value: T | ((prev: T) => T)) => void, IndexedDBMeta]} A tuple containing the current value, a setter function, and a metadata object. */ export declare function useIndexedDB(storeName: string, key: IDBValidKey, initialValue: T | (() => T), options?: { enabled?: boolean; }): [T, SetValue, IndexedDBMeta]; export {};