{"version":3,"file":"useReactiveRender.cjs","names":["useForceUpdate"],"sources":["../../../../src/api/hooks/utils/useReactiveRender.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { effect } from '@pastweb/tools/reactivity';\nimport { useForceUpdate } from '../../../useForceUpdate';\n\n/**\n * Reads enumerable fields from a reactive tools state object.\n *\n * Accessing each field while a tools `effect` is active registers those fields\n * as dependencies. `useReactiveRender` captures the field names from the first\n * state object and then uses this helper to read the matching values.\n *\n * @param target - Reactive state object returned by a tools API hook.\n * @returns Current values for the enumerable fields.\n */\nexport function readReactiveFields(target: Record<PropertyKey, any>): unknown[] {\n  return Object.keys(target).map(key => target[key]);\n}\n\n/**\n * Bridges a tools reactive object into React rendering.\n *\n * The core API hooks from `@pastweb/tools` own the query/mutation state\n * machine. This helper subscribes to their reactive fields and forces a React\n * render whenever one changes, so React components can consume the same object\n * directly.\n *\n * @typeParam TState - Reactive state object shape.\n * @param createState - Factory called once to create the tools reactive state.\n * @param getSnapshot - Optional custom dependency reader. If omitted, the\n * enumerable keys from the first state object are observed.\n * @returns The created reactive state object.\n */\nexport function useReactiveRender<TState extends Record<PropertyKey, any>>(\n  createState: () => TState,\n  getSnapshot?: (state: TState) => unknown[],\n): TState {\n  const state = useRef<TState | null>(null);\n  const stateKeys = useRef<string[]>([]);\n  const forceUpdate = useForceUpdate();\n\n  if (!state.current) {\n    state.current = createState();\n    stateKeys.current = Object.keys(state.current);\n  }\n\n  const readSnapshot = useCallback((target: TState) => {\n    if (getSnapshot) return getSnapshot(target);\n\n    return stateKeys.current.map(key => target[key]);\n  }, [getSnapshot]);\n\n  useEffect(() => {\n    effect(forceUpdate, () => readSnapshot(state.current as TState));\n  }, [forceUpdate, readSnapshot]);\n\n  return state.current;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAcA,SAAgB,mBAAmB,QAA6C;CAC9E,OAAO,OAAO,KAAK,MAAM,CAAC,CAAC,KAAI,QAAO,OAAO,IAAI;AACnD;;;;;;;;;;;;;;;AAgBA,SAAgB,kBACd,aACA,aACQ;CACR,MAAM,SAAA,GAAA,MAAA,OAAA,CAA8B,IAAI;CACxC,MAAM,aAAA,GAAA,MAAA,OAAA,CAA6B,CAAC,CAAC;CACrC,MAAM,cAAcA,sCAAAA,eAAe;CAEnC,IAAI,CAAC,MAAM,SAAS;EAClB,MAAM,UAAU,YAAY;EAC5B,UAAU,UAAU,OAAO,KAAK,MAAM,OAAO;CAC/C;CAEA,MAAM,gBAAA,GAAA,MAAA,YAAA,EAA4B,WAAmB;EACnD,IAAI,aAAa,OAAO,YAAY,MAAM;EAE1C,OAAO,UAAU,QAAQ,KAAI,QAAO,OAAO,IAAI;CACjD,GAAG,CAAC,WAAW,CAAC;CAEhB,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,CAAA,GAAA,0BAAA,OAAA,CAAO,mBAAmB,aAAa,MAAM,OAAiB,CAAC;CACjE,GAAG,CAAC,aAAa,YAAY,CAAC;CAE9B,OAAO,MAAM;AACf"}