import type { Writable } from './store'; export interface PrimitiveState extends Writable { readonly isState: true; readonly value: T; onChange: (cb: (v: T) => void) => void; set(this: void, value: T): void; refresh(): void; } export interface ArrayState extends PrimitiveState { push: (item: T) => void; } export interface MapState extends PrimitiveState> { /** * Set value and inform subscribers. * * Note: To update a map, use the `setKey` and `deleteKey` methods. */ set: (value: Map) => void; setKey: (key: K, value: V) => void; deleteKey: (key: K) => void; } export interface SetState extends PrimitiveState> { add: (item: T) => void; delete: (item: T) => void; } export type UnionToIntersection = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never; export type IsUnion = [T] extends [UnionToIntersection] ? false : true; export type UnionState = { set: (value: T) => void; } & Omit, 'set'>; /** * A state store is a traditional observable store with additional methods for Arrays, Maps, Sets, * getting and setting the value, reacting to changes, and easily persisting to local storage. * * @param {(v: T) => void} set - Sets the value of the store. * @param {(v: T) => T} update - Provides the current value, and updates it to the returned value. * @param {(v: T) => void} subscribe - Subscribes to the store. * @param {() => T} get - Returns the current value of the store. * @param {() => void} refresh - Refreshes the store. * @param {(cb: (v: T) => void) => void} onChange - Adds a callback that runs when the store changes. * @param {() => void} destroy - Destroys the store and cleans up all listeners. * @param {boolean} isState - Whether this is a state store. * todo - A `subscribeOnce` or `subscribeFor` could be nice. */ export type State = IsUnion extends true ? UnionState : T extends Array ? ArrayState : T extends Map ? MapState : T extends Set ? SetState : PrimitiveState; export interface StateOptions extends Partial> { /** * If provided, the store will be persisted to local storage * under the specified key. * @default undefined */ key?: string; /** * If provided, localStorage updates will be debounced by * the specified number of milliseconds. If both `debounce` * and `throttle` are provided, `debounce` will take precedence. */ debounce?: number; /** * Optional callback function that runs after the store is * updated and all subscribers have been notified. */ onChange?: (v: T) => void; } /** * An advanced store factory with additional features: * * - Support for Maps, Sets, and Arrays (enabling methods like `.push` and `.add`). * - A `.get` method for retrieving the current value of the store. * - Optional `onChange` callback for adding side effects without subscribing. * - Optional `key` argument for persisting the store to local storage. * * @param defaultValue - The default value of the store. * @param options - {@link StateOptions} * * @example * ```svelte * * *

{$foo} {$bar} {$baz}

* ``` */ export declare function state(defaultValue: T, options?: StateOptions): State; export declare function isState(v: any): v is State; export declare function fromState(state: T | State): T;