UNPKG

@tanstack/react-db

Version:

React integration for @tanstack/db

34 lines (33 loc) • 1.52 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const react = require("react"); const db = require("@tanstack/db"); const DbProvider = require("./DbProvider.cjs"); const sourceIdBindings = require("./source-id-bindings.cjs"); function useLiveQueryEffect(config, deps = []) { const client = DbProvider.useOptionalDbClient(); const configRef = react.useRef(config); configRef.current = config; react.useEffect(() => { const query = db.prepareLiveQueryValue(config.query, client); if (!client) sourceIdBindings.getPreparedSources(query, client, `useLiveQueryEffect`); const effect = db.createEffect({ id: config.id, query, skipInitial: config.skipInitial, onEnter: (event, ctx) => configRef.current.onEnter?.(event, ctx), onUpdate: (event, ctx) => configRef.current.onUpdate?.(event, ctx), onExit: (event, ctx) => configRef.current.onExit?.(event, ctx), onBatch: (events, ctx) => configRef.current.onBatch?.(events, ctx), onError: config.onError ? (error, event) => configRef.current.onError?.(error, event) : void 0, onSourceError: config.onSourceError ? (error) => configRef.current.onSourceError?.(error) : void 0 }); return () => { void effect.dispose().catch((error) => { console.error(`[useLiveQueryEffect] failed to dispose effect:`, error); }); }; }, [...deps, client]); } exports.useLiveQueryEffect = useLiveQueryEffect; //# sourceMappingURL=useLiveQueryEffect.cjs.map