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

split up middleware, add OptionalPromise, add cache entry lifecycle #1034

Merged
merged 21 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
12 changes: 9 additions & 3 deletions src/query/core/buildMiddleware/cacheLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export const build: SubMiddlewareBuilder = ({
action.meta.arg.endpointName,
action.meta.arg.originalArgs,
cacheKey,
mwApi
mwApi,
action.meta.requestId
)
}
} else if (mutationThunk.pending.match(action)) {
Expand All @@ -41,7 +42,8 @@ export const build: SubMiddlewareBuilder = ({
action.meta.arg.endpointName,
action.meta.arg.originalArgs,
cacheKey,
mwApi
mwApi,
action.meta.requestId
)
}
} else if (isFullfilledThunk(action)) {
Expand Down Expand Up @@ -81,7 +83,8 @@ export const build: SubMiddlewareBuilder = ({
endpointName: string,
originalArgs: any,
queryCacheKey: string,
mwApi: SubMiddlewareApi
mwApi: SubMiddlewareApi,
requestId: string
) {
const onCacheEntryAdded =
context.endpointDefinitions[endpointName]?.onCacheEntryAdded
Expand All @@ -107,11 +110,14 @@ export const build: SubMiddlewareBuilder = ({
)
lifecycleMap[queryCacheKey] = lifecycle
const selector = (api.endpoints[endpointName] as any).select(originalArgs)
const extra = mwApi.dispatch((_, __, extra) => extra)
const runningHandler = onCacheEntryAdded(
originalArgs,
{
...mwApi,
getCacheEntry: () => selector(mwApi.getState()),
requestId,
extra,
},
{
firstValueResolved,
Expand Down
3 changes: 3 additions & 0 deletions src/query/core/buildMiddleware/queryLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ export const build: SubMiddlewareBuilder = ({
originalArgs
)

const extra = mwApi.dispatch((_, __, extra) => extra)
Copy link
Collaborator

Choose a reason for hiding this comment

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

🤣

Copy link
Member Author

Choose a reason for hiding this comment

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

Genius, isn't it? :D

I was like "the old API had it, so we mayybe need it here too?" and then "how on earth do I get that?". Yup.

onQuery(
originalArgs,
{
...mwApi,
getCacheEntry: () => selector(mwApi.getState()),
requestId,
extra,
},
{ resultPromise }
)
Expand Down
26 changes: 1 addition & 25 deletions src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,6 @@ export function buildThunks<
> = async (arg, { signal, rejectWithValue, ...api }) => {
const endpointDefinition = endpointDefinitions[arg.endpointName]

const context: Record<string, any> = {}
const queryApi:
| QueryApi<ReducerPath, any>
| MutationApi<ReducerPath, any> = {
...api,
context,
}

if (endpointDefinition.onStart)
endpointDefinition.onStart(arg.originalArgs, queryApi)

try {
let transformResponse: (
baseQueryReturnValue: any,
Expand Down Expand Up @@ -282,25 +271,12 @@ export function buildThunks<
)
}
if (result.error) throw new HandledError(result.error, result.meta)
if (endpointDefinition.onSuccess)
endpointDefinition.onSuccess(
arg.originalArgs,
queryApi,
result.data,
result.meta
)

return {
fulfilledTimeStamp: Date.now(),
result: await transformResponse(result.data, result.meta),
}
} catch (error) {
if (endpointDefinition.onError)
endpointDefinition.onError(
arg.originalArgs,
queryApi,
error instanceof HandledError ? error.value : error,
error instanceof HandledError ? error.meta : undefined
)
if (error instanceof HandledError) {
return rejectWithValue(error.value)
}
Expand Down
40 changes: 40 additions & 0 deletions src/query/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
}
x.providesTags ??= x.provides
}
if (x.onStart || x.onSuccess || x.onError) {

Choose a reason for hiding this comment

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

Nice, glad u added a message here!

if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'development'
) {
console.warn(
'`onStart`, `onSuccess` and `onError` have been replaced by `onQuery`, please change your code accordingly'
)
}
x.onQuery ??= async (arg, api, { resultPromise }) => {
const queryApi = { ...api, context: {} }
x.onStart?.(arg, queryApi)
try {
const result = await resultPromise
x.onSuccess?.(arg, queryApi, result, undefined)
} catch (error) {
x.onError?.(arg, queryApi, error, undefined)
}
}
}
return { ...x, type: DefinitionType.query } as any
},
mutation: (x) => {
Expand All @@ -292,6 +312,26 @@ export function buildCreateApi<Modules extends [Module<any>, ...Module<any>[]]>(
}
x.invalidatesTags ??= x.invalidates
}
if (x.onStart || x.onSuccess || x.onError) {
if (
typeof process !== 'undefined' &&
process.env.NODE_ENV === 'development'
) {
console.warn(
'`onStart`, `onSuccess` and `onError` have been replaced by `onQuery`, please change your code accordingly'
)
}
x.onQuery ??= async (arg, api, { resultPromise }) => {
const queryApi = { ...api, context: {} }
x.onStart?.(arg, queryApi)
try {
const result = await resultPromise
x.onSuccess?.(arg, queryApi, result, undefined)
} catch (error) {
x.onError?.(arg, queryApi, error, undefined)
}
}
}
return { ...x, type: DefinitionType.mutation } as any
},
})
Expand Down
Loading