Skip to content

Commit

Permalink
feat: abort pending query signal on new query
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Mar 6, 2024
1 parent 83db1f7 commit 6b6195f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/query-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface UseQueryEntry<TResult = unknown, TError = unknown>
when: number

pending: null | {
abortController: AbortController
refreshCall: Promise<void>
when: number
}
Expand Down Expand Up @@ -253,9 +254,14 @@ export const useQueryCache = defineStore(QUERY_STORE_ID, () => {

// we create an object and verify we are the most recent pending request
// before doing anything
const signalController = new AbortController()
const { signal } = signalController
const abortController = new AbortController()
const { signal } = abortController
// abort any ongoing request
// TODO: test
entry.pending?.abortController.abort()

const pendingCall = (entry.pending = {
abortController,
refreshCall: entry
.options!.query({ signal })
.then((data) => {
Expand Down
2 changes: 1 addition & 1 deletion src/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export function useQuery<TResult, TError = ErrorDefault>(
}

// TODO: createQuery with access to other properties as arguments for advanced (maybe even recommended) usage
function _createQuery<TResult, TError = ErrorDefault>(
function _defineQuery<TResult, TError = ErrorDefault>(
setup: () => UseQueryOptions<TResult>,
) {
return () => useQuery<TResult, TError>(setup())
Expand Down

0 comments on commit 6b6195f

Please sign in to comment.