UNPKG

@tanstack/react-db

Version:

React integration for @tanstack/db

57 lines (56 loc) 2.56 kB
"use client"; "use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const react = require("react"); const useLiveQuery = require("./useLiveQuery.cjs"); const liveQueryInternals = require("./live-query-internals.cjs"); function useLiveSuspenseQuery(configOrQueryOrCollection, deps) { const promiseRef = react.useRef(null); const collectionRef = react.useRef(null); const hasBeenReadyRef = react.useRef(false); const result = deps === void 0 ? useLiveQuery.useLiveQuery(configOrQueryOrCollection) : useLiveQuery.useLiveQuery(configOrQueryOrCollection, deps); const queryInfo = liveQueryInternals.getLiveQueryResultInfo(result); if (collectionRef.current !== result.collection) { promiseRef.current = null; collectionRef.current = result.collection; hasBeenReadyRef.current = false; } if (!result.isEnabled) { throw new Error( `useLiveSuspenseQuery does not support disabled queries (callback returned undefined/null). The Suspense pattern requires data to always be defined (T, not T | undefined). Solutions: 1) Use conditional rendering - don't render the component until the condition is met. 2) Use useLiveQuery instead, which supports disabled queries with the 'isEnabled' flag.` ); } const collectionStatus = result.collection.status; if (result.isReady) { hasBeenReadyRef.current = true; promiseRef.current = null; } const observerError = queryInfo.observer.getError(); if (observerError !== void 0 && !hasBeenReadyRef.current) { promiseRef.current = null; throw observerError; } if (collectionStatus === `error` && !hasBeenReadyRef.current) { promiseRef.current = null; throw new Error(`Collection "${result.collection.id}" failed to load`); } if (!hasBeenReadyRef.current && (result.isLoading || result.isIdle)) { if (queryInfo.client?._isSsrStreamingEnabled() && !queryInfo.queryHash) { const reason = queryInfo.identityError ? `${queryInfo.identityError.reason} at ${queryInfo.identityError.path}` : `the query has no stable identity`; throw new Error( `Cannot stream this live query during SSR because ${reason}. Provide an explicit serializable queryKey.` ); } if (!promiseRef.current) { promiseRef.current = queryInfo.observer.preload(); } throw promiseRef.current; } return { state: result.state, data: result.data, collection: result.collection }; } exports.useLiveSuspenseQuery = useLiveSuspenseQuery; //# sourceMappingURL=useLiveSuspenseQuery.cjs.map