(
opts: LoadSubsetOptions = {},
queryFunction: typeof queryFn = queryFn,
)
| 1079 | }) |
| 1080 | |
| 1081 | const createQueryFromOpts = ( |
| 1082 | opts: LoadSubsetOptions = {}, |
| 1083 | queryFunction: typeof queryFn = queryFn, |
| 1084 | ): true | Promise<void> => { |
| 1085 | if (!startupRetentionSettled) { |
| 1086 | return startupRetentionMaintenancePromise.then(() => { |
| 1087 | const resumed = createQueryFromOpts(opts, queryFunction) |
| 1088 | return resumed === true ? undefined : resumed |
| 1089 | }) |
| 1090 | } |
| 1091 | |
| 1092 | // Generate key using common function |
| 1093 | const key = generateQueryKeyFromOptions(opts) |
| 1094 | const hashedQueryKey = hashKey(key) |
| 1095 | const extendedMeta = { ...meta, loadSubsetOptions: opts } |
| 1096 | const retainedEntry = metadata?.collection.get( |
| 1097 | `${QUERY_COLLECTION_GC_PREFIX}${hashedQueryKey}`, |
| 1098 | ) |
| 1099 | if ( |
| 1100 | parsePersistedQueryRetentionEntry(retainedEntry, hashedQueryKey) !== |
| 1101 | undefined |
| 1102 | ) { |
| 1103 | retainedQueriesPendingRevalidation.add(hashedQueryKey) |
| 1104 | } |
| 1105 | cancelPersistedRetentionExpiry(hashedQueryKey) |
| 1106 | |
| 1107 | validateQueryKeyPrefix(key) |
| 1108 | |
| 1109 | if (state.observers.has(hashedQueryKey)) { |
| 1110 | // We already have a query for this queryKey |
| 1111 | // Increment reference count since another consumer is using this observer |
| 1112 | queryRefCounts.set( |
| 1113 | hashedQueryKey, |
| 1114 | (queryRefCounts.get(hashedQueryKey) || 0) + 1, |
| 1115 | ) |
| 1116 | |
| 1117 | // Get the current result and return based on its state |
| 1118 | const observer = state.observers.get(hashedQueryKey)! |
| 1119 | const currentResult = observer.getCurrentResult() |
| 1120 | |
| 1121 | if (currentResult.isSuccess) { |
| 1122 | // Data is already available, return true synchronously |
| 1123 | return true |
| 1124 | } else if (currentResult.isError) { |
| 1125 | // Error already occurred, reject immediately |
| 1126 | return Promise.reject(currentResult.error) |
| 1127 | } else { |
| 1128 | // Check QueryClient cache directly - observer's getCurrentResult() may show |
| 1129 | // a loading state even when data exists in cache. This happens because observer |
| 1130 | // state can lag behind the QueryClient cache during unsubscribe/resubscribe |
| 1131 | // cycles (e.g., when a live query is cleaned up and recreated). |
| 1132 | const cachedData = queryClient.getQueryData(key) |
| 1133 | if (cachedData !== undefined) { |
| 1134 | return true |
| 1135 | } |
| 1136 | |
| 1137 | // Query is still loading, wait for the first result |
| 1138 | return new Promise<void>((resolve, reject) => { |
no test coverage detected