import { Focus, PickEquality } from '../types'; import { FocusAutoDispose } from './disposalGroup'; /** * Options for the map helper. */ export interface MapOptions { /** * Auto-dispose values when removed. * * - `false` / `undefined`: No auto-disposal (default) * - `true`: Dispose immediately via microtask * - `number`: Grace period in ms (e.g., `100` = 100ms delay) * - `string`: Named disposal group (shared across collections) * - `DisposalGroup`: Direct group instance * - `{ group, gracePeriodMs }`: Full options * * @example * ```ts * // Simple auto-dispose * map({ autoDispose: true }) * * // With 100ms grace period * map({ autoDispose: 100 }) * * // Named group - values moving between maps with same group name won't be disposed * map({ autoDispose: "cache" }) * * // Named group with grace period * map({ autoDispose: { group: "cache", gracePeriodMs: 100 } }) * * // Direct group instance * const group = disposalGroup(); * map({ autoDispose: group }) * ``` */ autoDispose?: FocusAutoDispose; /** * Called when value is added to the map. */ onAdded?: (value: T, key: string) => void; /** * Called when value is removed from the map. */ onRemoved?: (value: T, key: string) => void; } /** * Map API returned by the map() helper. */ export interface FocusMap { /** Get the current record (returns empty object if undefined/null) */ get(): Record; /** Get value by key */ at(key: string): T | undefined; /** Get number of entries */ size(): number; /** Check if record is empty */ isEmpty(): boolean; /** Check if key exists */ has(key: string): boolean; /** * Set value at key (auto-disposes old value if enabled). * Accepts direct value, reducer, or immer-style updater. * * @example * cache.set('count', 10); // Direct value * cache.set('count', prev => prev + 1); // Reducer (returns new value) * cache.set('user', draft => { draft.age++ }); // Updater (mutates draft) */ set(key: string, valueOrReducerOrUpdater: T | ((prev: T) => T | void)): void; /** * Ensure value exists at key, creating it if necessary. * * @example * const user = users.ensure('user-123', () => ({ id: 'user-123', name: 'New' })); */ ensure(key: string, create: () => T): T; /** * Swap values at two keys. * * @example * cache.swap('a', 'b'); // Swap values at keys 'a' and 'b' */ swap(keyA: string, keyB: string): void; /** Delete key(s) (auto-disposes values if enabled) */ delete(...keys: string[]): number; /** Delete keys matching predicate (auto-disposes values if enabled) */ deleteWhere(predicate: (value: T, key: string) => boolean): number; /** Clear all entries (auto-disposes all values if enabled) */ clear(): void; /** Replace entire record (auto-disposes old values if enabled) */ replace(record: Record): void; /** Get all keys */ keys(): string[]; /** Get all values */ values(): T[]; /** Get all entries */ entries(): [string, T][]; /** Create a pick selector for fine-grained reactivity */ pick(equality?: PickEquality | undefined | null>): Record; } /** * Create a map helper for object/record focus. * Handles undefined/null records gracefully. * * @example * ```ts * const cacheStore = store({ * name: 'cache', * state: { users: {} as Record }, * setup({ focus }) { * const users = focus('users').as(map()); * return { * setUser: (id: string, user: User) => users.set(id, user), * getUser: (id: string) => users.at(id), * removeUser: (id: string) => users.delete(id), * hasUser: (id: string) => users.has(id), * clearUsers: () => users.clear(), * }; * }, * }); * ``` * * @example * ```ts * // With named disposal group for cross-collection moves * const cacheStore = store({ * name: 'cache', * state: { * active: {} as Record, * archived: {} as Record, * }, * setup({ focus }) { * // Same group name = values moving between won't be disposed * const active = focus('active').as(map({ autoDispose: "sessions" })); * const archived = focus('archived').as(map({ autoDispose: "sessions" })); * * return { * archiveSession: (id: string) => { * const session = active.at(id); * if (session) { * active.delete(id); // Schedules disposal * archived.set(id, session); // Cancels disposal - same group! * } * }, * }; * }, * }); * ``` */ export declare function map(options?: MapOptions): (focus: Focus | undefined | null> | Focus | undefined> | Focus | null> | Focus>) => FocusMap; export { disposalGroup, getNamedGroup, type DisposalGroup, type FocusAutoDispose, type FocusAutoDisposeOptions, } from './disposalGroup'; //# sourceMappingURL=map.d.ts.map