Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow intersecting stale results #168

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions query/src/lib/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type UnifiedTypes<T> = T extends Array<QueryObserverResult<any>>
*
* This operator is used to merge multiple queries into one.
* It will return a new base query result that will merge the results of all the queries.
* If you pass the 'intersectStaleData' flag, it will also intersect unsuccessful result in case data for all queries is present.
*
* @example
*
Expand All @@ -128,13 +129,25 @@ type UnifiedTypes<T> = T extends Array<QueryObserverResult<any>>
* intersectResults$(({ todos, posts }) => {
* return { ... }
* })
* )
* );
*
* @example
*
* const query = combineLatest([todos.result$, posts.result$]).pipe(
* intersectResults$(([todos, posts]) => {
* return { ... }
* })
* );
*
* @example
*
* const query = combineLatest({
* todos: todos.result$,
* posts: posts.result$,
* }).pipe(
* intersectResults$(({ todos, posts }) => {
* return { ... }
* }, { intersectStaleData: true });
* )
*/
export function intersectResults$<
Expand All @@ -144,6 +157,7 @@ export function intersectResults$<
R,
>(
mapFn: (values: UnifiedTypes<T>) => R,
options?: { intersectStaleData: boolean },
): OperatorFunction<T, QueryObserverResult<R> & { all: T }> {
return map((values) => {
const isArray = Array.isArray(values);
Expand All @@ -162,20 +176,14 @@ export function intersectResults$<
refetch,
} as unknown as QueryObserverResult<R> & { all: T };

if (mappedResult.isSuccess) {
if (isArray) {
mappedResult.data = mapFn(
toArray.map((r) => r.data) as UnifiedTypes<T>,
);
} else {
const data = Object.entries(values).reduce((acc, [key, value]) => {
if (mappedResult.isSuccess || (options?.intersectStaleData && toArray.every((r) => !!r.data))) {
const data = isArray
? toArray.map((r) => r.data) as UnifiedTypes<T>
: Object.entries(values).reduce((acc, [key, value]) => {
acc[key as keyof UnifiedTypes<T>] = value.data;

return acc;
}, {} as UnifiedTypes<T>);

mappedResult.data = mapFn(data);
}
mappedResult.data = mapFn(data);
}

return mappedResult;
Expand Down
38 changes: 21 additions & 17 deletions query/src/lib/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type UnifiedTypes<T> = T extends Array<Signal<QueryObserverBaseResult<any>>>
*
* This function is used to merge multiple signal queries into one.
* It will return a new base query result that will merge the results of all the queries.
* Note that it should be used inside injection context
* Note that it should be used inside injection context.
* If you pass the 'intersectStaleData' flag, it will also intersect unsuccessful result in case data for all queries is present.
*
* @example
*
Expand All @@ -35,8 +36,7 @@ type UnifiedTypes<T> = T extends Array<Signal<QueryObserverBaseResult<any>>>
* posts: posts.result$,
* }, ({ todos, posts }) => {
* return todos + posts;
* })
*
* });
*
* @example
*
Expand All @@ -49,6 +49,15 @@ type UnifiedTypes<T> = T extends Array<Signal<QueryObserverBaseResult<any>>>
* return todoOne.title + todoTwo.title;
* }
* );
*
* @example
*
* const query = intersetResults({
* todos: todos.result$,
* posts: posts.result$,
* }, ({ todos, posts }) => {
* return todos + posts;
* }, { intersectStaleData: true });
*/
export function intersectResults<
T extends
Expand All @@ -58,10 +67,17 @@ export function intersectResults<
>(
signals: T,
mapFn: (values: UnifiedTypes<T>) => R,
options?: { intersectStaleData: boolean }
): Signal<QueryObserverResult<R> & { all: T }> {
const isArray = Array.isArray(signals);
const toArray = isArray ? signals : Object.values(signals);
const refetch = () => Promise.all(toArray.map(v => v().refetch()));
const intersectData = isArray
? () => toArray.map((r) => r().data) as UnifiedTypes<T>
: () => Object.entries(signals).reduce((acc, [key, value]) => {
acc[key as keyof UnifiedTypes<T>] = value().data;
return acc;
}, {} as UnifiedTypes<T>);

return computed(() => {
const mappedResult = {
Expand All @@ -76,20 +92,8 @@ export function intersectResults<
refetch,
} as unknown as QueryObserverResult<R> & { all: T };

if (mappedResult.isSuccess) {
if (isArray) {
mappedResult.data = mapFn(
toArray.map((r) => r().data) as UnifiedTypes<T>,
);
} else {
const data = Object.entries(signals).reduce((acc, [key, value]) => {
acc[key as keyof UnifiedTypes<T>] = value().data;

return acc;
}, {} as UnifiedTypes<T>);

mappedResult.data = mapFn(data);
}
if (mappedResult.isSuccess || (options?.intersectStaleData && toArray.every((v) => !!v().data))) {
mappedResult.data = mapFn(intersectData());
}

return mappedResult;
Expand Down
Loading