Skip to content

Commit

Permalink
fix: query plugin was not awaiting results on initial load, so it was…
Browse files Browse the repository at this point in the history
… setting isLoaded to soon
  • Loading branch information
jmeistrich committed Sep 9, 2024
1 parent c293185 commit 1eee330
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/sync-plugins/tanstack-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function syncedQuery<
let observer: QueryObserver<TQueryFnData, TError, TData, TQueryKey> | undefined = undefined;
let latestOptions = defaultedOptions;
let queryKeyFromFn: TQueryKey;
let resolveInitialPromise: undefined | ((value: TData) => void) = undefined;

const origQueryKey = options.queryKey!;

Expand Down Expand Up @@ -73,10 +74,16 @@ export function syncedQuery<
// Create the observer
observer = new Observer!<TQueryFnData, TError, TData, TQueryKey>(queryClient!, latestOptions);

const get = () => {
const get = async () => {
// Get the initial optimistic results if it's already cached
const result = observer!.getOptimisticResult(latestOptions);

if (result.isLoading) {
await new Promise((resolve) => {
resolveInitialPromise = resolve;
});
}

return result.data!;
};

Expand All @@ -85,6 +92,10 @@ export function syncedQuery<
const unsubscribe = observer!.subscribe(
notifyManager.batchCalls((result) => {
if (result.status === 'success') {
if (resolveInitialPromise) {
resolveInitialPromise(result.data);
resolveInitialPromise = undefined;
}
update({ value: result.data });
}
}),
Expand Down

0 comments on commit 1eee330

Please sign in to comment.