All files / use-map/src types.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                                                                                                                                                   
/**
 * Accepted initial value for {@link useMap}.
 *
 * Mirrors `useState`'s lazy-initializer support: pass a `Map`, any iterable of
 * key/value tuples (e.g. an array of pairs), or a function returning one of
 * those (evaluated once on mount).
 *
 * @template K - Key type.
 * @template V - Value type.
 *
 * @example
 * ```ts
 * useMap<string, number>();                                  // empty
 * useMap(new Map([["a", 1]]));                                 // from a Map
 * useMap<string, number>([["a", 1], ["b", 2]]);               // from tuples
 * useMap(() => new Map(expensiveInit()));                      // lazy
 * ```
 */
export type MapInitializer<K, V> =
  | Map<K, V>
  | Iterable<readonly [K, V]>
  | (() => Map<K, V> | Iterable<readonly [K, V]>);
 
/**
 * Stable action handlers returned by {@link useMap}. Every function keeps a
 * stable identity across renders, so the actions object is safe to use as a
 * `useEffect`/`useMemo` dependency.
 *
 * @template K - Key type.
 * @template V - Value type.
 */
export interface UseMapActions<K, V> {
  /**
   * Set (or overwrite) the value for a key. Setting a key to a value it already
   * holds (by `Object.is`) is a no-op and does not trigger a re-render.
   */
  set: (key: K, value: V) => void;
 
  /**
   * Replace the entire map contents with the given entries. Accepts a `Map` or
   * any iterable of key/value tuples.
   */
  setAll: (entries: Iterable<readonly [K, V]>) => void;
 
  /**
   * Remove a key. Removing a key that is not present is a no-op and does not
   * trigger a re-render.
   */
  remove: (key: K) => void;
 
  /**
   * Reset the map back to the initial value provided at mount (a fresh copy, so
   * later mutations never affect the stored initial).
   */
  reset: () => void;
 
  /**
   * Remove every entry. Clearing an already-empty map is a no-op.
   */
  clear: () => void;
 
  /**
   * Read the current value for a key. Stable across renders and always reflects
   * the latest state.
   */
  get: (key: K) => V | undefined;
}
 
/**
 * Return type of {@link useMap}: a tuple of the current (read-only) map and the
 * stable action handlers.
 *
 * The map is typed as `ReadonlyMap` on purpose — mutating it directly (e.g.
 * `map.set(...)`) would bypass React state and break immutability, so the type
 * steers callers to the actions instead. Read access (`get`, `has`, `size`,
 * iteration) works as usual.
 *
 * @template K - Key type.
 * @template V - Value type.
 */
export type UseMapReturn<K, V> = [ReadonlyMap<K, V>, UseMapActions<K, V>];