Skip to content

Commit

Permalink
Merge branch 'main' into issues/8499
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Jan 6, 2025
2 parents 46b7e06 + 3104854 commit 2f69183
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions packages/vue-query/src/useQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,31 @@ export function useQueries<
defaultedQueries.value,
options as QueriesObserverOptions<TCombinedResult>,
)
const [, getCombinedResult] = observer.getOptimisticResult(
defaultedQueries.value,
(options as QueriesObserverOptions<TCombinedResult>).combine,
)
const state = shallowRef(getCombinedResult())

const getOptimisticResult = () => {
const [results, getCombinedResult] = observer.getOptimisticResult(
defaultedQueries.value,
(options as QueriesObserverOptions<TCombinedResult>).combine,
)

return getCombinedResult(
results.map((result, index) => {
return {
...result,
refetch: async (...args: Array<any>) => {
const [{ [index]: query }] = observer.getOptimisticResult(
defaultedQueries.value,
(options as QueriesObserverOptions<TCombinedResult>).combine,
)

return query!.refetch(...args)
},
}
}),
)
}

const state = shallowRef(getOptimisticResult())

let unsubscribe = () => {
// noop
Expand All @@ -313,38 +333,22 @@ export function useQueries<
if (!isRestoring) {
unsubscribe()
unsubscribe = observer.subscribe(() => {
const [, getCombinedResultRestoring] = observer.getOptimisticResult(
defaultedQueries.value,
(options as QueriesObserverOptions<TCombinedResult>).combine,
)
state.value = getCombinedResultRestoring()
state.value = getOptimisticResult()
})
// Subscription would not fire for persisted results
const [, getCombinedResultPersisted] = observer.getOptimisticResult(
defaultedQueries.value,
(options as QueriesObserverOptions<TCombinedResult>).combine,
)
state.value = getCombinedResultPersisted()

state.value = getOptimisticResult()
}
},
{ immediate: true },
)

watch(
defaultedQueries,
() => {
observer.setQueries(
defaultedQueries.value,
options as QueriesObserverOptions<TCombinedResult>,
)
const [, getCombinedResultPersisted] = observer.getOptimisticResult(
defaultedQueries.value,
(options as QueriesObserverOptions<TCombinedResult>).combine,
)
state.value = getCombinedResultPersisted()
},
{ flush: 'sync' },
)
watch(defaultedQueries, (queriesValue) => {
observer.setQueries(
queriesValue,
options as QueriesObserverOptions<TCombinedResult>,
)
state.value = getOptimisticResult()
})

onScopeDispose(() => {
unsubscribe()
Expand Down

0 comments on commit 2f69183

Please sign in to comment.