UNPKG

@tanstack/react-db

Version:

React integration for @tanstack/db

111 lines (110 loc) • 5.1 kB
"use client"; const require_DbProvider = require("./DbProvider.cjs"); const require_useLiveQuery = require("./useLiveQuery.cjs"); let react = require("react"); let _tanstack_db = require("@tanstack/db"); // src/useLiveInfiniteQuery.ts var unpreparedQueryValue = Symbol(`unpreparedQueryValue`); function useLiveInfiniteQuery(queryFnOrCollection, config) { const pageShape = (0, _tanstack_db.readLiveQueryWindowPageShape)(config); const contextDbClient = require_DbProvider.useOptionalDbClient(); const dbClient = config.client ?? contextDbClient; const inputIsCollection = (0, _tanstack_db.isCollection)(queryFnOrCollection); const committedRef = (0, react.useRef)(null); const committed = committedRef.current; const renderedRef = (0, react.useRef)(null); const sourceIdsRef = (0, react.useRef)(null); const derivedIdentityProfilerRef = (0, react.useRef)(require_useLiveQuery.createDerivedIdentityProfiler()); const deferredCollections = /* @__PURE__ */ new Set(); const identityScope = () => require_useLiveQuery.getIdentityScope(sourceIdsRef.current ??= (0, _tanstack_db.createLiveQuerySourceIdClaims)(), dbClient, `useLiveInfiniteQuery`); const queryKey = (0, _tanstack_db.getLiveQueryKey)(config); let preparedQueryValue = unpreparedQueryValue; let dependencies; let derivedQuery = void 0; const rendered = renderedRef.current; let binding; try { if (!inputIsCollection) { if (queryKey !== void 0) dependencies = queryKey; else if (committed !== null && committed.derivedQuery === queryFnOrCollection && committed.binding.scope === dbClient) { derivedQuery = queryFnOrCollection; dependencies = committed.binding.dependencies; } else { const preparation = require_useLiveQuery.prepareDerivedQuery(queryFnOrCollection, derivedIdentityProfilerRef.current, identityScope(), deferredCollections); preparedQueryValue = preparation.value; derivedQuery = queryFnOrCollection; dependencies = preparation.identityDeps; } } const compileQuery = () => { if (preparedQueryValue === unpreparedQueryValue) preparedQueryValue = (0, _tanstack_db.prepareLiveQueryValue)(queryFnOrCollection, dbClient, deferredCollections); return preparedQueryValue; }; binding = (0, _tanstack_db.bindLiveQueryWindow)(committed?.binding ?? null, { ...pageShape, input: typeof queryFnOrCollection === `function` ? compileQuery : queryFnOrCollection, dependencies, scope: dbClient, client: dbClient, queryKey, recompile: `identity` }, rendered?.binding); } catch (error) { (0, _tanstack_db.rejectLiveQueryInput)(deferredCollections, error); } let currentRenderState = committed; if (binding !== committed?.binding) { const newCollection = binding.collection !== committed?.binding.collection; if (newCollection && binding.collection === rendered?.binding.collection) for (const deferred of rendered.deferredCollections) deferredCollections.add(deferred); const reusesRendered = newCollection && binding.collection === rendered?.binding.collection; currentRenderState = { binding, derivedQuery, deferredCollections, cancelReclaimRelease: reusesRendered ? rendered.cancelReclaimRelease : newCollection && binding.ownsCollection ? (0, _tanstack_db.releaseDeferredOnReclaim)(binding.collection, deferredCollections) : void 0, warned: false }; if (binding.supplied) renderedRef.current = null; else if (newCollection && binding.ownsCollection) renderedRef.current = currentRenderState; } const renderState = currentRenderState; const controller = renderState.binding.controller; const subscribe = (0, react.useCallback)((onStoreChange) => { const unsubscribe = controller.subscribe(onStoreChange); committedRef.current = renderState; if (renderState.binding.warning && !renderState.warned) { renderState.warned = true; console.warn(renderState.binding.warning); } renderState.cancelReclaimRelease?.(); (0, _tanstack_db.resumeDeferredLiveQueryCollections)(renderState.deferredCollections); return unsubscribe; }, [controller, renderState]); const getSnapshot = (0, react.useCallback)(() => controller.getSnapshot(), [controller]); const snapshot = (0, react.useSyncExternalStore)(subscribe, getSnapshot, () => controller.getServerSnapshot()); const fetchNextPage = (0, react.useCallback)(() => (0, _tanstack_db.fetchNextLiveQueryWindowPage)(controller), [controller]); return { data: snapshot.data, get state() { return snapshot.state; }, status: snapshot.status, isLoading: snapshot.isLoading, isReady: snapshot.isReady, isIdle: snapshot.isIdle, isLoadingError: snapshot.isLoadingError, isCrashed: snapshot.isCrashed, isCleanedUp: snapshot.isCleanedUp, isSyncError: snapshot.isSyncError, collection: snapshot.collection, isEnabled: snapshot.isEnabled, pages: snapshot.pages, pageParams: snapshot.pageParams, fetchNextPage, hasNextPage: snapshot.hasNextPage, isFetchingNextPage: snapshot.isFetchingNextPage, health: snapshot.health }; } exports.useLiveInfiniteQuery = useLiveInfiniteQuery; //# sourceMappingURL=useLiveInfiniteQuery.cjs.map