{"version":3,"file":"useLiveQueryEffect.cjs","names":["useOptionalDbClient","useRef","prepareLiveQueryValue","createEffect"],"sources":["../../src/useLiveQueryEffect.ts"],"sourcesContent":["import { useEffect, useRef } from 'react'\nimport { createEffect, prepareLiveQueryValue } from '@tanstack/db'\nimport { useOptionalDbClient } from './DbProvider'\nimport { getPreparedSources } from './source-id-bindings'\nimport type { EffectConfig } from '@tanstack/db'\n\n/**\n * React hook for creating a reactive effect that fires handlers when rows\n * enter, exit, or update within a query result.\n *\n * The effect is created on mount and disposed on unmount. If `deps` change,\n * the previous effect is disposed and a new one is created.\n *\n * @example\n * ```tsx\n * function ChatComponent() {\n *   useLiveQueryEffect(\n *     {\n *       query: (q) => q.from({ msg: messages }).where(({ msg }) => eq(msg.role, 'user')),\n *       skipInitial: true,\n *       onEnter: async (event) => {\n *         await generateResponse(event.value)\n *       },\n *     },\n *     []\n *   )\n *\n *   return <div>...</div>\n * }\n * ```\n */\nexport function useLiveQueryEffect<\n  TRow extends object = Record<string, unknown>,\n  TKey extends string | number = string | number,\n>(config: EffectConfig<TRow, TKey>, deps: React.DependencyList = []): void {\n  const client = useOptionalDbClient()\n  const configRef = useRef<EffectConfig<TRow, TKey>>(config)\n  configRef.current = config\n\n  useEffect(() => {\n    const query = prepareLiveQueryValue(config.query, client)\n    if (!client) getPreparedSources(query, client, `useLiveQueryEffect`)\n    const effect = createEffect<TRow, TKey>({\n      id: config.id,\n      query: query as EffectConfig<TRow, TKey>[`query`],\n      skipInitial: config.skipInitial,\n      onEnter: (event, ctx) => configRef.current.onEnter?.(event, ctx),\n      onUpdate: (event, ctx) => configRef.current.onUpdate?.(event, ctx),\n      onExit: (event, ctx) => configRef.current.onExit?.(event, ctx),\n      onBatch: (events, ctx) => configRef.current.onBatch?.(events, ctx),\n      onError: config.onError\n        ? (error, event) => configRef.current.onError?.(error, event)\n        : undefined,\n      onSourceError: config.onSourceError\n        ? (error) => configRef.current.onSourceError?.(error)\n        : undefined,\n    })\n\n    return () => {\n      // React cannot await cleanup. Report a failed source release.\n      void effect.dispose().catch((error) => {\n        console.error(`[useLiveQueryEffect] failed to dispose effect:`, error)\n      })\n    }\n  }, [...deps, client])\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,SAAgB,mBAGd,QAAkC,OAA6B,CAAC,GAAS;CACzE,MAAM,SAASA,mBAAAA,oBAAoB;CACnC,MAAM,aAAA,GAAYC,MAAAA,OAAAA,CAAiC,MAAM;CACzD,UAAU,UAAU;CAEpB,CAAA,GAAA,MAAA,UAAA,OAAgB;EACd,MAAM,SAAA,GAAQC,aAAAA,sBAAAA,CAAsB,OAAO,OAAO,MAAM;EACxD,IAAI,CAAC,QAAQ,2BAAA,mBAAmB,OAAO,QAAQ,oBAAoB;EACnE,MAAM,UAAA,GAASC,aAAAA,aAAAA,CAAyB;GACtC,IAAI,OAAO;GACJ;GACP,aAAa,OAAO;GACpB,UAAU,OAAO,QAAQ,UAAU,QAAQ,UAAU,OAAO,GAAG;GAC/D,WAAW,OAAO,QAAQ,UAAU,QAAQ,WAAW,OAAO,GAAG;GACjE,SAAS,OAAO,QAAQ,UAAU,QAAQ,SAAS,OAAO,GAAG;GAC7D,UAAU,QAAQ,QAAQ,UAAU,QAAQ,UAAU,QAAQ,GAAG;GACjE,SAAS,OAAO,WACX,OAAO,UAAU,UAAU,QAAQ,UAAU,OAAO,KAAK,IAC1D,KAAA;GACJ,eAAe,OAAO,iBACjB,UAAU,UAAU,QAAQ,gBAAgB,KAAK,IAClD,KAAA;EACN,CAAC;EAED,aAAa;GAEX,OAAY,QAAQ,CAAC,CAAC,OAAO,UAAU;IACrC,QAAQ,MAAM,kDAAkD,KAAK;GACvE,CAAC;EACH;CACF,GAAG,CAAC,GAAG,MAAM,MAAM,CAAC;AACtB"}