import { createStore } from 'zustand/vanilla' export interface KeyStats { /** Total times trigger() was called for this key. */ triggerCount: number /** Calls absorbed by the throttle window (same payload, returned existing promise). */ throttledCount: number /** Concurrent in-flight calls deduplicated (same key+params already running). */ dedupedCount: number /** Calls skipped by the shouldFetch predicate — no I/O, no events. */ shouldFetchSkipCount: number /** Native cache hits — data served from the native store without an API call. */ cacheHitCount: number /** Actual API calls initiated (queryFn executed). */ apiFetchCount: number /** Pipeline errors (any phase: fetch, validate, transform, write). */ errorCount: number } export type StatsState = Record export function defaultKeyStats(): KeyStats { return { triggerCount: 0, throttledCount: 0, dedupedCount: 0, shouldFetchSkipCount: 0, cacheHitCount: 0, apiFetchCount: 0, errorCount: 0, } } export function createStatsStore() { return createStore(() => ({})) } export type StatsStore = ReturnType export function incrementStat( store: StatsStore, key: string, field: keyof KeyStats, ): void { store.setState((state) => ({ [key]: { ...(state[key] ?? defaultKeyStats()), [field]: (state[key]?.[field] ?? 0) + 1, }, })) }