Skip to content

Commit

Permalink
revert refactor i did for cancellation when we wanted it to throw
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Sep 20, 2024
1 parent a9148ba commit f01a06b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
47 changes: 30 additions & 17 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { focusManager } from './focusManager'
import { notifyManager } from './notifyManager'
import { fetchState } from './query'
import { Subscribable } from './subscribable'
import { isThenableEqual, pendingThenable } from './thenable'
import { pendingThenable } from './thenable'
import {
isServer,
isValidTimeout,
Expand All @@ -15,7 +15,7 @@ import {
} from './utils'
import type { FetchOptions, Query, QueryState } from './query'
import type { QueryClient } from './queryClient'
import type { Thenable } from './thenable'
import type { PendingThenable, Thenable } from './thenable'
import type {
DefaultError,
DefaultedQueryObserverOptions,
Expand Down Expand Up @@ -618,33 +618,46 @@ export class QueryObserver<
}

if (this.options.experimental_prefetchInRender) {
const nextThenable = (() => {
const thenable = pendingThenable<TData>()

const finalizeThenableIfPossible = (thenable: PendingThenable<TData>) => {
if (nextResult.status === 'error') {
thenable.reject(nextResult.error)
} else if (nextResult.data !== undefined) {
thenable.resolve(nextResult.data)
}
return thenable as Thenable<TData>
})()
}

const prevThenable = this.#currentThenable
/**
* Create a new thenable and result promise when the results have changed
*/
const recreateThenable = () => {
const pending =
(this.#currentThenable =
nextResult.promise =
pendingThenable())

finalizeThenableIfPossible(pending)
}

const prevThenable = this.#currentThenable
switch (prevThenable.status) {
case 'pending':
if (nextThenable.status === 'fulfilled') {
prevThenable.resolve(nextThenable.value)
} else if (nextThenable.status === 'rejected') {
prevThenable.reject(nextThenable.reason)
}
// Finalize the previous thenable if it was pending
finalizeThenableIfPossible(prevThenable)
break

case 'fulfilled':
if (
nextResult.status === 'error' ||
nextResult.data !== prevThenable.value
) {
recreateThenable()
}
break
case 'rejected':
if (!isThenableEqual(prevThenable, nextThenable)) {
// Replace the thenable when the results have changed
this.#currentThenable = nextResult.promise = nextThenable
if (
nextResult.status !== 'error' ||
nextResult.error !== prevThenable.reason
) {
recreateThenable()
}
break
}
Expand Down
10 changes: 0 additions & 10 deletions packages/query-core/src/thenable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@
interface Fulfilled<T> {
status: 'fulfilled'
value: T

reason?: never
}
interface Rejected {
status: 'rejected'
reason: unknown

value?: never
}
interface Pending<T> {
status: 'pending'

reason?: never
value?: never
/**
* Resolve the promise with a value.
* Will remove the `resolve` and `reject` properties from the promise.
Expand Down Expand Up @@ -86,7 +80,3 @@ export function pendingThenable<T>(): PendingThenable<T> {

return thenable
}

export function isThenableEqual<T>(a: Thenable<T>, b: Thenable<T>): boolean {
return a.status === b.status && a.value === b.value && a.reason === b.reason
}

0 comments on commit f01a06b

Please sign in to comment.