export type Awaitable = T | Promise; export type RestateSetStateAction = T | ((previousValue: T) => T); export type RestateKind = 'memory' | 'persistent'; export type RestateAction = 'set' | 'reset' | 'restore'; export type RestateEventType = | 'change' | 'reset' | 'restore' | 'start' | 'stop' | 'initialize' | 'ready' | 'save' | 'clearStorage' | 'error' | '*'; export interface UpdaterProps { getState(): T; setState(value: RestateSetStateAction): void; /** The value captured when the updater starts. Use getState() for the latest value. */ value: T; } export type UpdaterFunction = (props: UpdaterProps) => (() => void) | void; export interface RestateEvent { readonly type: string; readonly action?: RestateAction; readonly store: UseRestateFn; readonly value: T; readonly previousValue: T; readonly revision?: number; readonly changed?: boolean; readonly timestamp: number; readonly key?: string; readonly storageKey?: string; readonly error?: unknown; readonly [detail: string]: unknown; } export type RestateListener = ( value: T, previousValue: T, event: RestateEvent ) => void; export type RestateEventListener = (event: RestateEvent) => void; export interface UseRestateFn { (): T; readonly id: string; readonly kind: RestateKind; get(): T; getInitial(): T; set(value: RestateSetStateAction): void; reset(): T; subscribe(listener: RestateListener): () => void; on(type: RestateEventType | (string & {}), listener: RestateEventListener): () => void; listenerCount(type?: string): number; isRunning(): boolean; start(): void; stop(): void; destroy(): void; } export interface RestateStorage { readonly kind?: string; isAvailable?(): boolean; getItem(key: string): Awaitable; setItem(key: string, value: string): Awaitable; removeItem(key: string): Awaitable; clear?(): Awaitable; } export interface RestateSerializer { serialize(value: T): string; deserialize(value: string): T; } export interface IndexedDBStorageOptions { databaseName?: string; storeName?: string; version?: number; /** Custom IDBFactory-compatible value, primarily for non-browser runtimes and tests. */ indexedDB?: any; } export interface MMKVLike { getString(key: string): string | undefined; set(key: string, value: string): void; remove?(key: string): void; delete?(key: string): void; clearAll(): void; } export interface MMKVStorageOptions { /** Reuse an existing MMKV instance instead of creating one. */ mmkv?: MMKVLike; /** Inject the react-native-mmkv module, primarily for tests. */ mmkvModule?: { createMMKV?(configuration?: Record): MMKVLike; MMKV?: new (configuration?: Record) => MMKVLike; }; configuration?: Record; id?: string; } export interface PersistedRestateOptions { key: string; /** Defaults to "@ryujaewan/restate". Use an empty string to disable namespacing. */ namespace?: string; updater?: UpdaterFunction; /** Overrides the platform default (IndexedDB on web, MMKV on React Native). */ storage?: RestateStorage; storageOptions?: IndexedDBStorageOptions | MMKVStorageOptions; serializer?: RestateSerializer; version?: number; migrate?(value: unknown, storedVersion: number, currentVersion: number): T; } export interface PersistedRestateStatus { readonly ready: boolean; readonly operation: string | null; readonly available: boolean; readonly error: unknown; readonly lastSavedAt: number | null; readonly lastRestoredAt: number | null; } export interface PersistedRestateFn extends UseRestateFn { readonly kind: 'persistent'; readonly key: string; readonly storageKey: string; readonly storage: RestateStorage; readonly ready: Promise; initialize(): Promise; save(): Promise; restore(): Promise; hasSaved(): Promise; clearStorage(): Promise; remove(): Promise; /** Resets memory to the declared initial value and removes the saved value. */ resetAll(): Promise; status(): PersistedRestateStatus; } export interface RestateManagerEvent { readonly type: string; readonly timestamp: number; readonly store?: UseRestateFn; readonly stores?: ReadonlyArray>; readonly error?: unknown; readonly [detail: string]: unknown; } export interface RestateManager { list(): Array>; get(idOrKey: string): UseRestateFn | undefined; size(): number; subscribe(listener: (event: RestateManagerEvent) => void): () => void; resetAll(): unknown[]; saveAll(): Promise; restoreAll(): Promise; clearStorageAll(): Promise; resetAllAndClearStorage(): Promise; destroyAll(): void; } export declare function restate( initialValue: T, updater?: UpdaterFunction ): UseRestateFn; export declare function persistedRestate( initialValue: T, options: PersistedRestateOptions ): PersistedRestateFn; export declare function createIndexedDBStorage( options?: IndexedDBStorageOptions ): RestateStorage; export declare function createMMKVStorage( options?: MMKVStorageOptions ): RestateStorage; export declare const restateManager: RestateManager;