Skip to content

Commit

Permalink
feat: unref useQueries options (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianOsipiuk authored Aug 22, 2022
1 parent c0fde8b commit 5ad9859
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"vue": "3.2.27",
"vue-query": ">=1.0.0"
"vue-query": "^1.25.2"
},
"devDependencies": {
"@vitejs/plugin-vue": "2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/nuxt-simple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@nuxtjs/composition-api": "^0.28.0",
"nuxt": "^2.0.0",
"vue-query": ">=1.0.0"
"vue-query": "^1.25.2"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
Expand Down
26 changes: 13 additions & 13 deletions src/vue/useQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
reactive,
readonly,
watch,
unref,
Ref,
isRef,
isReactive,
Expand All @@ -18,6 +17,7 @@ import type {

import { useQueryClient } from "./useQueryClient";
import { UseQueryOptions } from "./useQuery";
import { cloneDeepUnref } from "./utils";

// Avoid TS depth-limit error in case of large array literal
type MAXIMUM_DEPTH = 20;
Expand Down Expand Up @@ -138,28 +138,28 @@ type UseQueriesOptionsArg<T extends any[]> = readonly [...UseQueriesOptions<T>];
export function useQueries<T extends any[]>(
queries: Ref<UseQueriesOptionsArg<T>> | UseQueriesOptionsArg<T>
): Readonly<UseQueriesResults<T>> {
const queryClientKey = (unref(queries) as UseQueriesOptionsArg<T>)[0]
?.queryClientKey;
const unreffedQueries = cloneDeepUnref(queries) as UseQueriesOptionsArg<T>;

const queryClientKey = unreffedQueries[0]?.queryClientKey;
const queryClient = useQueryClient(queryClientKey);
const defaultedQueries = (unref(queries) as UseQueriesOptionsArg<T>).map(
(options) => {
return queryClient.defaultQueryObserverOptions(options);
}
);
const defaultedQueries = unreffedQueries.map((options) => {
return queryClient.defaultQueryObserverOptions(options);
});

const observer = new QueriesObserver(queryClient, defaultedQueries);
const state = reactive(observer.getCurrentResult());

const unsubscribe = observer.subscribe((result) => {
state.splice(0, state.length, ...result);
});

if (isRef(queries) || isReactive(queries)) {
watch(queries, () => {
const defaulted = (unref(queries) as UseQueriesOptionsArg<T>).map(
(options) => {
return queryClient.defaultQueryObserverOptions(options);
}
);
const defaulted = (
cloneDeepUnref(queries) as UseQueriesOptionsArg<T>
).map((options) => {
return queryClient.defaultQueryObserverOptions(options);
});
observer.setQueries(defaulted);
});
}
Expand Down

1 comment on commit 5ad9859

@vercel
Copy link

@vercel vercel bot commented on 5ad9859 Aug 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vue-query – ./

vue-query-git-main-damianosipiuk.vercel.app
vue-query.vercel.app
vue-query-damianosipiuk.vercel.app

Please sign in to comment.