Skip to content

Commit

Permalink
refactor: rename RegisteredError to DefaultError
Browse files Browse the repository at this point in the history
this is the name that shows up in `UseQueryResult<number, DefaultError>` and `Registered` reads a bit weird
  • Loading branch information
TkDodo committed Feb 28, 2023
1 parent c2215d8 commit a22d056
Show file tree
Hide file tree
Showing 33 changed files with 182 additions and 198 deletions.
4 changes: 2 additions & 2 deletions packages/query-core/src/infiniteQueryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
InfiniteQueryObserverOptions,
InfiniteQueryObserverResult,
QueryKey,
RegisteredError,
DefaultError,
} from './types'
import type { QueryClient } from './queryClient'
import type { NotifyOptions, ObserverFetchOptions } from './queryObserver'
Expand All @@ -24,7 +24,7 @@ type InfiniteQueryObserverListener<TData, TError> = (

export class InfiniteQueryObserver<
TQueryFnData = unknown,
TError = RegisteredError,
TError = DefaultError,
TData = InfiniteData<TQueryFnData>,
TQueryData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
MutationOptions,
MutationStatus,
MutationMeta,
RegisteredError,
DefaultError,
} from './types'
import type { MutationCache } from './mutationCache'
import type { MutationObserver } from './mutationObserver'
Expand All @@ -23,7 +23,7 @@ interface MutationConfig<TData, TError, TVariables, TContext> {

export interface MutationState<
TData = unknown,
TError = RegisteredError,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> {
Expand Down Expand Up @@ -80,7 +80,7 @@ export type Action<TData, TError, TVariables, TContext> =

export class Mutation<
TData = unknown,
TError = RegisteredError,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> extends Removable {
Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/mutationCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MutationObserver } from './mutationObserver'
import type { NotifyEvent, MutationOptions, RegisteredError } from './types'
import type { NotifyEvent, MutationOptions, DefaultError } from './types'
import type { QueryClient } from './queryClient'
import { notifyManager } from './notifyManager'
import type { Action, MutationState } from './mutation'
Expand Down Expand Up @@ -126,7 +126,7 @@ export class MutationCache extends Subscribable<MutationCacheListener> {

find<
TData = unknown,
TError = RegisteredError,
TError = DefaultError,
TVariables = any,
TContext = unknown,
>(
Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/mutationObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
MutateOptions,
MutationObserverResult,
MutationObserverOptions,
RegisteredError,
DefaultError,
} from './types'
import { shallowEqualObjects } from './utils'

Expand All @@ -21,7 +21,7 @@ type MutationObserverListener<TData, TError, TVariables, TContext> = (

export class MutationObserver<
TData = unknown,
TError = RegisteredError,
TError = DefaultError,
TVariables = void,
TContext = unknown,
> extends Subscribable<
Expand Down
10 changes: 3 additions & 7 deletions packages/query-core/src/queriesObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
#result: QueryObserverResult[]
#queries: QueryObserverOptions[]
#observers: QueryObserver[]
#observersMap: Record<string, QueryObserver>

constructor(client: QueryClient, queries?: QueryObserverOptions[]) {
super()
Expand All @@ -35,7 +34,6 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
this.#queries = []
this.#result = []
this.#observers = []
this.#observersMap = {}

if (queries) {
this.setQueries(queries)
Expand Down Expand Up @@ -82,9 +80,6 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
)

const newObservers = newObserverMatches.map((match) => match.observer)
const newObserversMap = Object.fromEntries(
newObservers.map((observer) => [observer.options.queryHash, observer]),
)
const newResult = newObservers.map((observer) =>
observer.getCurrentResult(),
)
Expand All @@ -97,7 +92,6 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {
}

this.#observers = newObservers
this.#observersMap = newObserversMap
this.#result = newResult

if (!this.hasListeners()) {
Expand Down Expand Up @@ -166,7 +160,9 @@ export class QueriesObserver extends Subscribable<QueriesObserverListener> {

const getObserver = (options: QueryObserverOptions): QueryObserver => {
const defaultedOptions = this.#client.defaultQueryOptions(options)
const currentObserver = this.#observersMap[defaultedOptions.queryHash!]
const currentObserver = this.#observers.find(
(o) => o.options.queryHash === defaultedOptions.queryHash,
)
return (
currentObserver ?? new QueryObserver(this.#client, defaultedOptions)
)
Expand Down
8 changes: 4 additions & 4 deletions packages/query-core/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
CancelOptions,
SetDataOptions,
FetchStatus,
RegisteredError,
DefaultError,
} from './types'
import type { QueryCache } from './queryCache'
import type { QueryObserver } from './queryObserver'
Expand All @@ -34,7 +34,7 @@ interface QueryConfig<
state?: QueryState<TData, TError>
}

export interface QueryState<TData = unknown, TError = RegisteredError> {
export interface QueryState<TData = unknown, TError = DefaultError> {
data: TData | undefined
dataUpdateCount: number
dataUpdatedAt: number
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface FetchContext<

export interface QueryBehavior<
TQueryFnData = unknown,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> {
Expand Down Expand Up @@ -142,7 +142,7 @@ export interface SetStateOptions {

export class Query<
TQueryFnData = unknown,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> extends Removable {
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/queryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
NotifyEvent,
QueryKey,
QueryOptions,
RegisteredError,
DefaultError,
WithRequired,
} from './types'
import { notifyManager } from './notifyManager'
Expand Down Expand Up @@ -150,7 +150,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {

get<
TQueryFnData = unknown,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
Expand All @@ -165,7 +165,7 @@ export class QueryCache extends Subscribable<QueryCacheListener> {
return [...this.#queries.values()]
}

find<TQueryFnData = unknown, TError = RegisteredError, TData = TQueryFnData>(
find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(
filters: WithRequired<QueryFilters, 'queryKey'>,
): Query<TQueryFnData, TError, TData> | undefined {
if (typeof filters.exact === 'undefined') {
Expand Down
14 changes: 7 additions & 7 deletions packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
RefetchQueryFilters,
ResetOptions,
SetDataOptions,
RegisteredError,
DefaultError,
} from './types'
import type { QueryState } from './query'
import { QueryCache } from './queryCache'
Expand Down Expand Up @@ -177,7 +177,7 @@ export class QueryClient {
)
}

getQueryState<TQueryFnData = unknown, TError = RegisteredError>(
getQueryState<TQueryFnData = unknown, TError = DefaultError>(
queryKey: QueryKey,
): QueryState<TQueryFnData, TError> | undefined {
return this.#queryCache.find<TQueryFnData, TError>({ queryKey })?.state
Expand Down Expand Up @@ -272,7 +272,7 @@ export class QueryClient {

fetchQuery<
TQueryFnData,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = never,
Expand Down Expand Up @@ -301,7 +301,7 @@ export class QueryClient {

prefetchQuery<
TQueryFnData = unknown,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
Expand All @@ -312,7 +312,7 @@ export class QueryClient {

fetchInfiniteQuery<
TQueryFnData,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
Expand All @@ -331,7 +331,7 @@ export class QueryClient {

prefetchInfiniteQuery<
TQueryFnData,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
Expand Down Expand Up @@ -422,7 +422,7 @@ export class QueryClient {

defaultQueryOptions<
TQueryFnData = unknown,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DefaultedQueryObserverOptions, RegisteredError } from './types'
import type { DefaultedQueryObserverOptions, DefaultError } from './types'
import {
isServer,
isValidTimeout,
Expand Down Expand Up @@ -39,7 +39,7 @@ export interface ObserverFetchOptions extends FetchOptions {

export class QueryObserver<
TQueryFnData = unknown,
TError = RegisteredError,
TError = DefaultError,
TData = TQueryFnData,
TQueryData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
Expand Down
10 changes: 5 additions & 5 deletions packages/query-core/src/retryer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { focusManager } from './focusManager'
import { onlineManager } from './onlineManager'
import { sleep } from './utils'
import type { CancelOptions, NetworkMode, RegisteredError } from './types'
import type { CancelOptions, NetworkMode, DefaultError } from './types'

// TYPES

interface RetryerConfig<TData = unknown, TError = RegisteredError> {
interface RetryerConfig<TData = unknown, TError = DefaultError> {
fn: () => TData | Promise<TData>
abort?: () => void
onError?: (error: TError) => void
Expand All @@ -28,14 +28,14 @@ export interface Retryer<TData = unknown> {

export type RetryValue<TError> = boolean | number | ShouldRetryFunction<TError>

type ShouldRetryFunction<TError = RegisteredError> = (
type ShouldRetryFunction<TError = DefaultError> = (
failureCount: number,
error: TError,
) => boolean

export type RetryDelayValue<TError> = number | RetryDelayFunction<TError>

type RetryDelayFunction<TError = RegisteredError> = (
type RetryDelayFunction<TError = DefaultError> = (
failureCount: number,
error: TError,
) => number
Expand Down Expand Up @@ -63,7 +63,7 @@ export function isCancelledError(value: any): value is CancelledError {
return value instanceof CancelledError
}

export function createRetryer<TData = unknown, TError = RegisteredError>(
export function createRetryer<TData = unknown, TError = DefaultError>(
config: RetryerConfig<TData, TError>,
): Retryer<TData> {
let isRetryCancelled = false
Expand Down
Loading

0 comments on commit a22d056

Please sign in to comment.