Skip to content

Commit 175c3af

Browse files
riqtsmarkerikson
authored andcommitted
Forced into the thunk working InfQuery handling for api proof of concept
1 parent fd2c6a1 commit 175c3af

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export type StartQueryActionCreatorOptions = {
6868
[forceQueryFnSymbol]?: () => QueryReturnValue
6969
}
7070

71-
export interface StartInfiniteQueryActionCreatorOptions {
71+
export type StartInfiniteQueryActionCreatorOptions = {
7272
subscribe?: boolean
7373
forceRefetch?: boolean | number
7474
subscriptionOptions?: SubscriptionOptions

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import type {
2727
import { calculateProvidedBy, isQueryDefinition } from '../endpointDefinitions'
2828
import { HandledError } from '../HandledError'
2929
import type { UnwrapPromise } from '../tsHelpers'
30+
import type { InfiniteQueryDefinition } from '@internal/query/endpointDefinitions'
3031
import type {
3132
RootState,
3233
QueryKeys,
@@ -37,6 +38,7 @@ import type {
3738
import { QueryStatus } from './apiState'
3839
import type {
3940
QueryActionCreatorResult,
41+
StartInfiniteQueryActionCreatorOptions,
4042
StartQueryActionCreatorOptions,
4143
} from './buildInitiate'
4244
import { forceQueryFnSymbol, isUpsertQuery } from './buildInitiate'
@@ -55,6 +57,12 @@ export type BuildThunksApiEndpointQuery<
5557
Definition extends QueryDefinition<any, any, any, any, any>,
5658
> = Matchers<QueryThunk, Definition>
5759

60+
export type ApiEndpointInfiniteQuery<
61+
Definition extends InfiniteQueryDefinition<any, any, any, any, any>,
62+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
63+
Definitions extends EndpointDefinitions,
64+
> = Matchers<QueryThunk, Definition>
65+
5866
export type BuildThunksApiEndpointMutation<
5967
Definition extends MutationDefinition<any, any, any, any, any>,
6068
> = Matchers<MutationThunk, Definition>
@@ -111,17 +119,16 @@ export type QueryThunkArg = QuerySubstateIdentifier &
111119
endpointName: string
112120
}
113121

114-
export interface InfiniteQueryThunkArg
115-
extends QuerySubstateIdentifier,
116-
StartInfiniteQueryActionCreatorOptions {
117-
type: `query`
118-
originalArgs: unknown
119-
endpointName: string
120-
data: InfiniteData<unknown>
121-
param: unknown
122-
previous?: boolean
123-
direction?: 'forward' | 'backwards'
124-
}
122+
export type InfiniteQueryThunkArg = QuerySubstateIdentifier &
123+
StartInfiniteQueryActionCreatorOptions & {
124+
type: `query`
125+
originalArgs: unknown
126+
endpointName: string
127+
data: InfiniteData<unknown>
128+
param: unknown
129+
previous?: boolean
130+
direction?: 'forward' | 'backwards'
131+
}
125132

126133
type MutationThunkArg = {
127134
type: 'mutation'
@@ -422,8 +429,6 @@ export function buildThunks<
422429
if (forceQueryFn) {
423430
result = forceQueryFn()
424431
} else if (endpointDefinition.query) {
425-
//TODO: these will come from the hook/initiate/middleware?
426-
let pages: number
427432
const oldPages: any[] = []
428433
const oldPageParams: any[] = []
429434

@@ -439,7 +444,7 @@ export function buildThunks<
439444
}
440445

441446
const page = await baseQuery(
442-
endpointDefinition.query(arg.originalArgs),
447+
endpointDefinition.query(param),
443448
baseQueryApi,
444449
endpointDefinition.extraOptions as any,
445450
)
@@ -456,7 +461,7 @@ export function buildThunks<
456461
}
457462

458463
if ('infiniteQueryOptions' in endpointDefinition) {
459-
if (arg.direction && arg.data.pages.length) {
464+
if ('direction' in arg && arg.direction && arg.data.pages.length) {
460465
const previous = arg.direction === 'backwards'
461466
const pageParamFn = previous
462467
? getPreviousPageParam
@@ -481,6 +486,7 @@ export function buildThunks<
481486

482487
// Fetch remaining pages
483488
for (let i = 1; i < remainingPages; i++) {
489+
// @ts-ignore
484490
const param = getNextPageParam(
485491
arg.infiniteQueryOptions,
486492
result.data as InfiniteData<unknown>,
@@ -714,8 +720,13 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
714720
InfiniteQueryThunkArg,
715721
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
716722
>(`${reducerPath}/executeQuery`, executeEndpoint, {
717-
getPendingMeta() {
718-
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true }
723+
getPendingMeta(queryThunkArgs) {
724+
return {
725+
startedTimeStamp: Date.now(),
726+
[SHOULD_AUTOBATCH]: true,
727+
direction: queryThunkArgs.arg.direction,
728+
data: queryThunkArgs.arg.data,
729+
}
719730
},
720731
condition(queryThunkArgs, { getState }) {
721732
const state = getState()
@@ -727,6 +738,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
727738
const previousArg = requestState?.originalArgs
728739
const endpointDefinition =
729740
endpointDefinitions[queryThunkArgs.endpointName]
741+
const direction = queryThunkArgs.direction
730742

731743
// Order of these checks matters.
732744
// In order for `upsertQueryData` to successfully run while an existing request is in flight,
@@ -758,7 +770,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
758770
}
759771

760772
// Pull from the cache unless we explicitly force refetch or qualify based on time
761-
if (fulfilledVal) {
773+
if (fulfilledVal && !direction) {
762774
// Value is cached and we didn't specify to refresh, skip it.
763775
return false
764776
}

0 commit comments

Comments
 (0)