Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add currentData property to hook results. #1500

Merged
merged 1 commit into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ export type UseQueryStateResult<

type UseQueryStateBaseResult<D extends QueryDefinition<any, any, any, any>> =
QuerySubState<D> & {
/**
* Where `data` tries to hold data as much as possible, also re-using
* data from the last arguments passed into the hook, this property
* will always contain the received data from the query, for the current query arguments.
*/
currentData?: ResultTypeFrom<D>
/**
* Query has not started yet.
*/
Expand Down Expand Up @@ -342,11 +348,21 @@ type UseQueryStateDefaultResult<D extends QueryDefinition<any, any, any, any>> =
| { isLoading: true; isFetching: boolean; data: undefined }
| ({
isSuccess: true
isFetching: boolean
isFetching: true
error: undefined
} & Required<
Pick<UseQueryStateBaseResult<D>, 'data' | 'fulfilledTimeStamp'>
>)
| ({
isSuccess: true
isFetching: false
error: undefined
} & Required<
Pick<
UseQueryStateBaseResult<D>,
'data' | 'fulfilledTimeStamp' | 'currentData'
>
>)
| ({ isError: true } & Required<
Pick<UseQueryStateBaseResult<D>, 'error'>
>)
Expand Down Expand Up @@ -420,6 +436,7 @@ const queryStatePreSelector = (
return {
...currentState,
data,
currentData: currentState.data,
Copy link

@HansBrende HansBrende Sep 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I am wrong, but wouldn't this be basically the equivalent of doing:

const currentData = isFetching ? undefined : data;

in our own code? I would think you'd need to examine the args associated with lastResult to see if they are shallowly equal to the args of currentState, something like:

currentData = currentState.data ?? (lastResult?.arg === currentState.arg ? lastResult?.data : undefined)

if the comment

this property will always contain the received data from the query, for the current query arguments

is to be true. (i.e., distinguishing cases of polling from cases of arg changes).

But, correct me if I am wrong here. I am not too familiar with the internal workings of this codebase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currentState is always for the currently selected endpoint and lastResult is the last result the hook had cached. So lastResult can also be from another arg, but currentState never can.

Copy link

@HansBrende HansBrende Sep 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phryneas Right... but my question is: this solution doesn't seem to distinguish between polling and arg changes.
The behavior I'd expect for currentData is that if the args haven't changed (e.g., because of polling), then it would use the last result for those args (which might be cached in lastResult in that case, if a refetch is currently taking place).

Copy link
Member Author

@phryneas phryneas Sep 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data once present for the current args will not disappear from the cache while polling, and this essentially gives you data for the current args directly from the cache.
Once data is set in the cache, it will never be removed unless there is new data to replace it with, even while loading or an error occurs.
See the slice definition here:

.addCase(queryThunk.pending, (draft, { meta, meta: { arg } }) => {
if (arg.subscribe) {
// only initialize substate if we want to subscribe to it
draft[arg.queryCacheKey] ??= {
status: QueryStatus.uninitialized,
endpointName: arg.endpointName,
}
}
updateQuerySubstateIfExists(draft, arg.queryCacheKey, (substate) => {
substate.status = QueryStatus.pending
substate.requestId = meta.requestId
substate.originalArgs = arg.originalArgs
substate.startedTimeStamp = meta.startedTimeStamp
})
})
.addCase(queryThunk.fulfilled, (draft, { meta, payload }) => {
updateQuerySubstateIfExists(
draft,
meta.arg.queryCacheKey,
(substate) => {
if (substate.requestId !== meta.requestId) return
substate.status = QueryStatus.fulfilled
substate.data = copyWithStructuralSharing(substate.data, payload)
delete substate.error
substate.fulfilledTimeStamp = meta.fulfilledTimeStamp
}
)
})
.addCase(
queryThunk.rejected,
(draft, { meta: { condition, arg, requestId }, error, payload }) => {
updateQuerySubstateIfExists(
draft,
arg.queryCacheKey,
(substate) => {
if (condition) {
// request was aborted due to condition (another query already running)
} else {
// request failed
if (substate.requestId !== requestId) return
substate.status = QueryStatus.rejected
substate.error = (payload ?? error) as any
}
}
)
}
)
},

isFetching,
isLoading,
isSuccess,
Expand Down
14 changes: 14 additions & 0 deletions packages/toolkit/src/query/tests/unionTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ describe.skip('TS only tests', () => {
expectExactType(false as false)(result.isError)
}

expectExactType('' as string | undefined)(result.currentData)
// @ts-expect-error
expectExactType('' as string)(result.currentData)

if (result.isSuccess) {
if (!result.isFetching) {
expectExactType('' as string)(result.currentData)
} else {
expectExactType('' as string | undefined)(result.currentData)
// @ts-expect-error
expectExactType('' as string)(result.currentData)
}
}

// @ts-expect-error
expectType<never>(result)
// is always one of those four
Expand Down