Skip to content

Commit

Permalink
WIP: add baseQueryMeta to thunks, meta to lifecycle results
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed May 27, 2021
1 parent c125c38 commit c0f2848
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ export type AsyncThunkRejectedActionCreator<
aborted: boolean
condition: boolean
} & (
| { rejectedWithValue: false }
| ({ rejectedWithValue: false } & {
[K in keyof GetRejectedMeta<ThunkApiConfig>]?: undefined
})
| ({ rejectedWithValue: true } & GetRejectedMeta<ThunkApiConfig>)
)
>
Expand Down
7 changes: 6 additions & 1 deletion src/query/core/buildMiddleware/cacheLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ export const build: SubMiddlewareBuilder = ({
} else if (isFullfilledThunk(action)) {
const lifecycle = lifecycleMap[cacheKey]
if (lifecycle?.valueResolved) {
lifecycle.valueResolved({ data: action.payload })
lifecycle.valueResolved(
{
data: action.payload,
meta: action.meta.baseQueryMeta,
} as any /* TODO typings */
)
delete lifecycle.valueResolved
}
} else if (
Expand Down
22 changes: 15 additions & 7 deletions src/query/core/buildMiddleware/queryLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,23 @@ export const build: SubMiddlewareBuilder = ({
onQueryStarted(originalArgs, lifecycleApi)
}
} else if (isFullfilledThunk(action)) {
const { requestId } = action.meta
lifecycleMap[requestId]?.resolve({ data: action.payload })
const { requestId, baseQueryMeta } = action.meta
lifecycleMap[requestId]?.resolve(
{
data: action.payload,
meta: baseQueryMeta,
} as any /* TODO typings for this */
)
delete lifecycleMap[requestId]
} else if (isRejectedThunk(action)) {
const { requestId, rejectedWithValue } = action.meta
lifecycleMap[requestId]?.reject({
error: action.payload ?? action.error,
isUnhandledError: !rejectedWithValue,
})
const { requestId, rejectedWithValue, baseQueryMeta } = action.meta
lifecycleMap[requestId]?.reject(
{
error: action.payload ?? action.error,
isUnhandledError: !rejectedWithValue,
meta: baseQueryMeta,
} as any /* TODO typings for this */
)
delete lifecycleMap[requestId]
}

Expand Down
20 changes: 4 additions & 16 deletions src/query/core/buildSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import {
} from './apiState'
import {
calculateProvidedByThunk,
MutationThunk,
MutationThunkArg,
QueryThunk,
QueryThunkArg,
ThunkResult,
} from './buildThunks'
Expand Down Expand Up @@ -78,22 +80,8 @@ export function buildSlice({
config,
}: {
reducerPath: string
queryThunk: AsyncThunk<
ThunkResult,
QueryThunkArg,
{
pendingMeta: { startedTimeStamp: number }
fulfilledMeta: { fulfilledTimeStamp: number }
}
>
mutationThunk: AsyncThunk<
ThunkResult,
MutationThunkArg,
{
pendingMeta: { startedTimeStamp: number }
fulfilledMeta: { fulfilledTimeStamp: number }
}
>
queryThunk: QueryThunk
mutationThunk: MutationThunk
context: ApiContext<EndpointDefinitions>
assertTagType: AssertTagTypes
config: Omit<ConfigState<string>, 'online' | 'focused'>
Expand Down
11 changes: 9 additions & 2 deletions src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ export type ThunkResult = unknown

export type ThunkApiMetaConfig = {
pendingMeta: { startedTimeStamp: number }
fulfilledMeta: { fulfilledTimeStamp: number }
fulfilledMeta: {
fulfilledTimeStamp: number
baseQueryMeta: unknown
}
rejectedMeta: {
baseQueryMeta: unknown
}
}
export type QueryThunk = AsyncThunk<
ThunkResult,
Expand Down Expand Up @@ -306,11 +312,12 @@ export function buildThunks<
await transformResponse(result.data, result.meta),
{
fulfilledTimeStamp: Date.now(),
baseQueryMeta: result.meta,
}
)
} catch (error) {
if (error instanceof HandledError) {
return rejectWithValue(error.value)
return rejectWithValue(error.value, { baseQueryMeta: error.meta })
}
throw error
}
Expand Down
8 changes: 7 additions & 1 deletion src/query/tests/cacheLifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ describe.each([['query'], ['mutation']] as const)(
await fakeTimerWaitFor(() => {
expect(gotFirstValue).toHaveBeenCalled()
})
expect(gotFirstValue).toHaveBeenCalledWith({ data: { value: 'success' } })
expect(gotFirstValue).toHaveBeenCalledWith({
data: { value: 'success' },
meta: {
request: expect.any(Request),
response: expect.any(Object), // Response is not available in jest env
},
})
expect(onCleanup).not.toHaveBeenCalled()

promise.unsubscribe(), await waitMs()
Expand Down
12 changes: 11 additions & 1 deletion src/query/tests/queryLifecycle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ describe.each([['query'], ['mutation']] as const)(
storeRef.store.dispatch(extended.endpoints.injected.initiate('arg'))
expect(onStart).toHaveBeenCalledWith('arg')
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledWith({ data: { value: 'success' } })
expect(onSuccess).toHaveBeenCalledWith({
data: { value: 'success' },
meta: {
request: expect.any(Request),
response: expect.any(Object), // Response is not available in jest env
},
})
})
})

Expand Down Expand Up @@ -90,6 +96,10 @@ describe.each([['query'], ['mutation']] as const)(
data: { value: 'error' },
},
isUnhandledError: false,
meta: {
request: expect.any(Request),
response: expect.any(Object), // Response is not available in jest env
},
})
})
expect(onSuccess).not.toHaveBeenCalled()
Expand Down

0 comments on commit c0f2848

Please sign in to comment.