Skip to content

Commit

Permalink
feat(signals): Add refetch functionality to intersected results (#163)
Browse files Browse the repository at this point in the history
* feat(signals): Add refetch functionality to intersected results

This change ensures that all intersected queries are refetched when "refetch" is called on the combined result.

* Update signals.ts

* Update operators.ts
  • Loading branch information
Zacherl authored Mar 5, 2024
1 parent f61fa62 commit 5a910cc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 2 additions & 0 deletions query/src/lib/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export function intersectResults$<
return map((values) => {
const isArray = Array.isArray(values);
const toArray = isArray ? values : Object.values(values);
const refetch = () => Promise.all(toArray.map(v => v.refetch()));

const mappedResult = {
all: values,
Expand All @@ -158,6 +159,7 @@ export function intersectResults$<
isFetching: toArray.some((v) => v.isFetching),
error: toArray.find((v) => v.isError)?.error,
data: undefined,
refetch,
} as unknown as QueryObserverResult<R> & { all: T };

if (mappedResult.isSuccess) {
Expand Down
8 changes: 5 additions & 3 deletions query/src/lib/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ export function intersectResults<
signals: T,
mapFn: (values: UnifiedTypes<T>) => R,
): Signal<QueryObserverResult<R> & { all: T }> {
return computed(() => {
const isArray = Array.isArray(signals);
const toArray = isArray ? signals : Object.values(signals);
const isArray = Array.isArray(signals);
const toArray = isArray ? signals : Object.values(signals);
const refetch = () => Promise.all(toArray.map(v => v().refetch()));

return computed(() => {
const mappedResult = {
all: signals,
isSuccess: toArray.every((v) => v().isSuccess),
Expand All @@ -72,6 +73,7 @@ export function intersectResults<
isFetching: toArray.some((v) => v().isFetching),
error: toArray.find((v) => v().isError)?.error,
data: undefined,
refetch,
} as unknown as QueryObserverResult<R> & { all: T };

if (mappedResult.isSuccess) {
Expand Down

0 comments on commit 5a910cc

Please sign in to comment.