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

feat(query): add useQueriesState composable #35

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {
serialize,
type UseQueryEntry,
} from './query-store'
export { useQueriesState } from './use-queries-state'

export { TreeMapNode, type EntryNodeKey } from './tree-map'

Expand Down
4 changes: 2 additions & 2 deletions src/query-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {

function ensureEntry<TResult = unknown, TError = ErrorDefault>(
keyRaw: EntryKey,
options: UseQueryOptionsWithDefaults<TResult, TError>,
options?: UseQueryOptionsWithDefaults<TResult, TError>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Creating a query entry without options risks errors if the key is invalidated and the options are accessed. Probably, we should consider avoiding creating query entities in UseQueryState and return undefined/null instead.

): UseQueryEntry<TResult, TError> {
if (process.env.NODE_ENV !== 'production' && keyRaw.length === 0) {
throw new Error(
Expand All @@ -188,7 +188,7 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {
cachesRaw.set(
key,
(entry = scope.run(() =>
createQueryEntry(key, options.initialData?.()),
createQueryEntry(key, options?.initialData?.()),
)!),
)
}
Expand Down
31 changes: 31 additions & 0 deletions src/use-queries-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { MaybeRefOrGetter } from 'vue'
import { computed, toValue } from 'vue'
import type { EntryKey } from './entry-options'
import type { QueryStatus } from './query-store'
import { useQueryCache } from './query-store'

interface QueryState {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use _UseQueryEntry_State type from query-store.ts here?

data: unknown
error: unknown
status: QueryStatus
}

// TODO: accept also a since key (instead of an array) as argument
// TODO: accept a ref for `keys` (to handle adding/removing entries for example)?
export function useQueriesState(keys: Array<MaybeRefOrGetter<EntryKey>>) {
const { ensureEntry } = useQueryCache()
return computed(() => {
return keys.map<QueryState[]>(
// TODO: fix TS issue
(key) => {
const query = ensureEntry(toValue(key))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add and use the findQuery function here? It will work if we decide not to create a query in useQueriesState in case it has not been created

return {
data: query.data.value,
error: query.error.value,
status: query.status.value,
options: query.options,
}
},
)
})
}
Loading