Skip to content

Commit

Permalink
fix(queries): Fix async frontend queries cancel behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
webjunkie committed Nov 23, 2023
1 parent dc2499d commit 2a8fefd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 10 additions & 2 deletions frontend/src/lib/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,16 @@ export function idToKey(array: Record<string, any>[], keyField: string = 'id'):
return object
}

export function delay(ms: number): Promise<number> {
return new Promise((resolve) => window.setTimeout(resolve, ms))
export function delay(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(resolve, ms)
if (signal) {
signal.addEventListener('abort', () => {
clearTimeout(timeoutId)
reject(new DOMException('Aborted', 'AbortError'))
})
}
})
}

export function clearDOMTextSelection(): void {
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/queries/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
isTimeToSeeDataSessionsQuery,
} from './utils'

const QUERY_ASYNC_MAX_INTERVAL_SECONDS = 10
const QUERY_ASYNC_MAX_INTERVAL_SECONDS = 5
const QUERY_ASYNC_TOTAL_POLL_SECONDS = 300

//get export context for a given query
Expand Down Expand Up @@ -115,15 +115,9 @@ async function executeQuery<N extends DataNode = DataNode>(
let currentDelay = 300 // start low, because all queries will take at minimum this

while (performance.now() - pollStart < QUERY_ASYNC_TOTAL_POLL_SECONDS * 1000) {
await delay(currentDelay)
await delay(currentDelay, methodOptions?.signal)
currentDelay = Math.min(currentDelay * 2, QUERY_ASYNC_MAX_INTERVAL_SECONDS * 1000)

if (methodOptions?.signal?.aborted) {
const customAbortError = new Error('Query aborted')
customAbortError.name = 'AbortError'
throw customAbortError
}

const statusResponse = await api.queryStatus.get(response.id)

if (statusResponse.complete || statusResponse.error) {
Expand Down

0 comments on commit 2a8fefd

Please sign in to comment.