Skip to content

Commit

Permalink
feat: upgraded with latest react-query release
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed May 3, 2024
1 parent 16bee87 commit 640679b
Show file tree
Hide file tree
Showing 19 changed files with 1,383 additions and 1,310 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"rollup-plugin-node-externals": "^7.0.1",
"rxjs": "^7.8.0",
"semantic-release": "^23.0.2",
"typescript": "^5.0.4",
"typescript": "5.2.2",
"vite": "^5.1.3",
"vite-plugin-dts": "^3.6.3",
"vitest": "^1.3.0"
Expand Down
100 changes: 62 additions & 38 deletions src/lib/queries/client/QueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
type FetchQueryOptions,
type QueryFilters,
type SetDataOptions,
type RefetchOptions,
type QueryOptions
type RefetchOptions
} from "./queries/types"
import {
type RefetchQueryFilters,
Expand All @@ -26,15 +25,22 @@ import {
type DefaultedQueryObserverOptions,
type QueryObserverOptions
} from "./queries/observer/types"
import { functionalUpdate, hashQueryKeyByOptions } from "./queries/utils"
import { type NoInfer } from "../../utils/types"
import {
functionalUpdate,
hashQueryKeyByOptions,
skipToken
} from "./queries/utils"
import { type NoInfer, type OmitKeyof } from "../../utils/types"
import { type QueryState } from "./queries/query/types"
import { type CancelOptions } from "./queries/retryer/types"
import { hashKey } from "./keys/hashKey"
import { partialMatchKey } from "./keys/partialMatchKey"

export interface DefaultOptions<TError = DefaultError> {
queries?: Omit<QueryObserverOptions<unknown, TError>, "suspense">
queries?: OmitKeyof<
QueryObserverOptions<unknown, TError>,
"suspense" | "queryKey"
>
mutations?: MutationObserverOptions<unknown, TError, unknown, unknown>
}

Expand Down Expand Up @@ -99,7 +105,8 @@ export class QueryClient {
TQueryKey extends QueryKey = QueryKey,
TPageParam = never
>(
options?:
options:
| QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>
| QueryObserverOptions<
TQueryFnData,
TError,
Expand All @@ -122,7 +129,7 @@ export class QueryClient {
TQueryData,
TQueryKey
> {
if (options?._defaulted) {
if (options._defaulted) {
return options as DefaultedQueryObserverOptions<
TQueryFnData,
TError,
Expand All @@ -134,32 +141,36 @@ export class QueryClient {

const defaultedOptions = {
...this.#defaultOptions.queries,
...(options?.queryKey && this.getQueryDefaults(options.queryKey)),
...this.getQueryDefaults(options.queryKey),
...options,
_defaulted: true
}

if (!defaultedOptions.queryHash) {
defaultedOptions.queryHash = hashQueryKeyByOptions(
defaultedOptions.queryKey,
defaultedOptions as QueryOptions<any, any, any, any, never>
defaultedOptions
)
}

// dependent default values
if (typeof defaultedOptions.refetchOnReconnect === "undefined") {
if (defaultedOptions.refetchOnReconnect === undefined) {
defaultedOptions.refetchOnReconnect =
defaultedOptions.networkMode !== "always"
}
if (typeof defaultedOptions.throwOnError === "undefined") {
if (defaultedOptions.throwOnError === undefined) {
defaultedOptions.throwOnError = !!defaultedOptions.suspense
}

if (!defaultedOptions.networkMode && defaultedOptions.persister) {
defaultedOptions.networkMode = "offlineFirst"
}

if (
typeof defaultedOptions.networkMode === "undefined" &&
defaultedOptions.persister
defaultedOptions.enabled !== true &&
defaultedOptions.queryFn === skipToken
) {
defaultedOptions.networkMode = "offlineFirst"
defaultedOptions.enabled = false
}

return defaultedOptions as DefaultedQueryObserverOptions<
Expand All @@ -178,13 +189,9 @@ export class QueryClient {
TQueryKey extends QueryKey = QueryKey,
TPageParam = never
>(
options: FetchQueryOptions<
TQueryFnData,
TError,
TData,
TQueryKey,
TPageParam
>
options:
| FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>
| FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>
): Promise<TData> {
const defaultedOptions = this.defaultQueryOptions(options)

Expand Down Expand Up @@ -265,34 +272,27 @@ export class QueryClient {
>(queryKey: TTaggedQueryKey): TInferredQueryFnData | undefined
getQueryData(queryKey: QueryKey) {
const options = this.defaultQueryOptions({ queryKey })

return this.#queryCache.get(options.queryHash)?.state.data
}

setQueryData<
TQueryFnData = unknown,
TaggedQueryKey extends QueryKey = QueryKey,
TInferredQueryFnData = TaggedQueryKey extends DataTag<
TTaggedQueryKey extends QueryKey = QueryKey,
TInferredQueryFnData = TTaggedQueryKey extends DataTag<
unknown,
infer TaggedValue
>
? TaggedValue
: TQueryFnData
>(
queryKey: TaggedQueryKey,
queryKey: TTaggedQueryKey,
updater: Updater<
NoInfer<TInferredQueryFnData> | undefined,
NoInfer<TInferredQueryFnData> | undefined
>,
options?: SetDataOptions
): TInferredQueryFnData | undefined {
const query = this.#queryCache.find<TInferredQueryFnData>({ queryKey })
const prevData = query?.state.data
const data = functionalUpdate(updater, prevData)

if (typeof data === "undefined") {
return undefined
}

const defaultedOptions = this.defaultQueryOptions<
any,
any,
Expand All @@ -301,6 +301,16 @@ export class QueryClient {
QueryKey
>({ queryKey })

const query = this.#queryCache.get<TInferredQueryFnData>(
defaultedOptions.queryHash
)
const prevData = query?.state.data
const data = functionalUpdate(updater, prevData)

if (data === undefined) {
return undefined
}

return this.#queryCache
.build(this, defaultedOptions)
.setData(data, { ...options, manual: true })
Expand All @@ -322,10 +332,21 @@ export class QueryClient {
return result
}

getQueryState<TQueryFnData = unknown, TError = DefaultError>(
queryKey: QueryKey
): QueryState<TQueryFnData, TError> | undefined {
return this.#queryCache.find<TQueryFnData, TError>({ queryKey })?.state
getQueryState<
TQueryFnData = unknown,
TError = DefaultError,
TTaggedQueryKey extends QueryKey = QueryKey,
TInferredQueryFnData = TTaggedQueryKey extends DataTag<
unknown,
infer TaggedValue
>
? TaggedValue
: TQueryFnData
>(
queryKey: TTaggedQueryKey
): QueryState<TInferredQueryFnData, TError> | undefined {
return this.#queryCache.find<TInferredQueryFnData, TError>({ queryKey })
?.state
}

setMutationDefaults(
Expand All @@ -352,10 +373,13 @@ export class QueryClient {

getQueryDefaults(
queryKey: QueryKey
): QueryObserverOptions<any, any, any, any, any> {
): OmitKeyof<QueryObserverOptions<any, any, any, any, any>, "queryKey"> {
const defaults = [...this.#queryDefaults.values()]

let result: QueryObserverOptions<any, any, any, any, any> = {}
let result: OmitKeyof<
QueryObserverOptions<any, any, any, any, any>,
"queryKey"
> = {}

defaults.forEach((queryDefault) => {
if (partialMatchKey(queryKey, queryDefault.queryKey)) {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/queries/client/queries/cache/QueryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export class QueryCache {

build<TQueryFnData, TError, TData, TQueryKey extends QueryKey>(
client: QueryClient,
options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
options: WithRequired<
QueryOptions<TQueryFnData, TError, TData, TQueryKey>,
"queryKey"
>,
state?: QueryState<TData, TError>
): Query<TQueryFnData, TError, TData, TQueryKey> {
const queryKey = options.queryKey ?? ([nanoid()] as unknown as TQueryKey)
Expand Down
Loading

0 comments on commit 640679b

Please sign in to comment.