Skip to content

Commit

Permalink
move lifecycle callbacks type definitions into their respective middl…
Browse files Browse the repository at this point in the history
…eware
  • Loading branch information
phryneas committed May 6, 2021
1 parent d9d7bd4 commit e2e1fbc
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 123 deletions.
108 changes: 107 additions & 1 deletion src/query/core/buildMiddleware/cacheLifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,113 @@
import { isAsyncThunkAction, isFulfilled } from '@reduxjs/toolkit'
import { toOptionalPromise } from '../../utils/toOptionalPromise'
import { AnyAction } from 'redux'
import { ThunkDispatch } from 'redux-thunk'
import { BaseQueryFn } from '../../baseQueryTypes'
import {
OptionalPromise,
toOptionalPromise,
} from '../../utils/toOptionalPromise'
import { RootState } from '../apiState'
import {
MutationResultSelectorResult,
QueryResultSelectorResult,
} from '../buildSelectors'
import { SubMiddlewareApi, SubMiddlewareBuilder } from './types'

declare module '../../endpointDefinitions' {
export type LifecycleApi<
ReducerPath extends string = string,
CacheEntry = unknown
> = {
/**
* The dispatch method for the store
*/
dispatch: ThunkDispatch<any, any, AnyAction>
/**
* A method to get the current state
*/
getState(): RootState<any, any, ReducerPath>
/**
* `extra` as provided as `thunk.extraArgument` to the `configureStore` `getDefaultMiddleware` option.
*/
extra: unknown
/**
* A unique ID generated for the mutation
*/
requestId: string
/**
* Gets the current value of this cache entry.
*/
getCacheEntry: () => CacheEntry
}

export interface CacheLifecyclePromises<ResultType = unknown> {
/**
* Promise that will resolve with the first value for this cache key.
* This allows you to `await` until an actual value is in cache.
*
* If the cache entry is removed from the cache before any value has ever
* been resolved, this Promise will reject with
* `new Error('Promise never resolved before cleanup.')`
* to prevent memory leaks.
* You can just re-throw that error (or not handle it at all) -
* it will be caught outside of `cacheEntryAdded`.
*/
firstValueResolved: OptionalPromise<ResultType>
/**
* Promise that allows you to wait for the point in time when the cache entry
* has been removed from the cache, by not being used/subscribed to any more
* in the application for too long or by dispatching `api.util.resetApiState`.
*/
cleanup: Promise<void>
}

interface QueryExtraOptions<
TagTypes extends string,
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn,
ReducerPath extends string = string
> {
onCacheEntryAdded?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
QueryResultSelectorResult<
{ type: DefinitionType.query } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: CacheLifecyclePromises<ResultType>
): Promise<void> | void
}

interface MutationExtraOptions<
TagTypes extends string,
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn,
ReducerPath extends string = string
> {
onCacheEntryAdded?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
MutationResultSelectorResult<
{ type: DefinitionType.mutation } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: CacheLifecyclePromises<ResultType>
): Promise<void> | void
}
}

export const build: SubMiddlewareBuilder = ({
api,
reducerPath,
Expand Down
69 changes: 68 additions & 1 deletion src/query/core/buildMiddleware/queryLifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
import { isPending, isRejected, isFulfilled } from '@reduxjs/toolkit'
import { toOptionalPromise } from '../../utils/toOptionalPromise'
import { BaseQueryFn } from '../../baseQueryTypes'
import {
OptionalPromise,
toOptionalPromise,
} from '../../utils/toOptionalPromise'
import {
MutationResultSelectorResult,
QueryResultSelectorResult,
} from '../buildSelectors'
import { SubMiddlewareBuilder } from './types'

declare module '../../endpointDefinitions' {
export interface QueryLifecyclePromises<ResultType> {
/**
* Promise that will resolve with the (transformed) query result.
* If the query fails, this promise will reject with the error.
*
* This allows you to `await` for the query to finish.
*/
resultPromise: OptionalPromise<ResultType>
}

interface QueryExtraOptions<
TagTypes extends string,
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn,
ReducerPath extends string = string
> {
onQuery?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
QueryResultSelectorResult<
{ type: DefinitionType.query } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: QueryLifecyclePromises<ResultType>
): Promise<void> | void
}

interface MutationExtraOptions<
TagTypes extends string,
ResultType,
QueryArg,
BaseQuery extends BaseQueryFn,
ReducerPath extends string = string
> {
onQuery?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
MutationResultSelectorResult<
{ type: DefinitionType.mutation } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: QueryLifecyclePromises<ResultType>
): Promise<void> | void
}
}

export const build: SubMiddlewareBuilder = ({
api,
context,
Expand Down
1 change: 0 additions & 1 deletion src/query/core/buildSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
RootState as _RootState,
getRequestStatusFlags,
RequestStatusFlags,
CombinedState,
} from './apiState'
import {
EndpointDefinitions,
Expand Down
122 changes: 2 additions & 120 deletions src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { AnyAction, ThunkDispatch } from '@reduxjs/toolkit'
import { RootState } from './core/apiState'
import {
QueryResultSelectorResult,
MutationResultSelectorResult,
} from './core/buildSelectors'
import {
BaseQueryExtraOptions,
BaseQueryFn,
Expand Down Expand Up @@ -60,64 +56,6 @@ interface EndpointDefinitionWithQueryFn<
transformResponse?: never
}

export type LifecycleApi<
ReducerPath extends string = string,
CacheEntry = unknown
> = {
/**
* The dispatch method for the store
*/
dispatch: ThunkDispatch<any, any, AnyAction>
/**
* A method to get the current state
*/
getState(): RootState<any, any, ReducerPath>
/**
* `extra` as provided as `thunk.extraArgument` to the `configureStore` `getDefaultMiddleware` option.
*/
extra: unknown
/**
* A unique ID generated for the mutation
*/
requestId: string
/**
* Gets the current value of this cache entry.
*/
getCacheEntry: () => CacheEntry
}

export interface CacheLifecyclePromises<ResultType = unknown> {
/**
* Promise that will resolve with the first value for this cache key.
* This allows you to `await` until an actual value is in cache.
*
* If the cache entry is removed from the cache before any value has ever
* been resolved, this Promise will reject with
* `new Error('Promise never resolved before cleanup.')`
* to prevent memory leaks.
* You can just re-throw that error (or not handle it at all) -
* it will be caught outside of `cacheEntryAdded`.
*/
firstValueResolved: OptionalPromise<ResultType>
/**
* Promise that allows you to wait for the point in time when the cache entry
* has been removed from the cache, by not being used/subscribed to any more
* in the application for too long or by dispatching `api.util.resetApiState`.
*/
cleanup: Promise<void>
}

interface QueryLifecyclePromises<ResultType> {
/**
* Promise that will resolve with the (transformed) query result.
* If the query fails, this promise will reject with the error.
*
* This allows you to `await` for the query to finish.
*/
resultPromise: OptionalPromise<ResultType>
}

export type BaseEndpointDefinition<
QueryArg,
BaseQuery extends BaseQueryFn,
Expand Down Expand Up @@ -182,7 +120,7 @@ export interface QueryApi<ReducerPath extends string, Context extends {}> {
context: Context
}

interface QueryExtraOptions<
export interface QueryExtraOptions<
TagTypes extends string,
ResultType,
QueryArg,
Expand Down Expand Up @@ -232,34 +170,6 @@ interface QueryExtraOptions<
result: ResultType,
meta: undefined
): void
onCacheEntryAdded?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
QueryResultSelectorResult<
{ type: DefinitionType.query } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: CacheLifecyclePromises<ResultType>
): Promise<void> | void
onQuery?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
QueryResultSelectorResult<
{ type: DefinitionType.query } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: QueryLifecyclePromises<ResultType>
): Promise<void> | void
}

export type QueryDefinition<
Expand Down Expand Up @@ -295,7 +205,7 @@ export interface MutationApi<ReducerPath extends string, Context extends {}> {
context: Context
}

interface MutationExtraOptions<
export interface MutationExtraOptions<
TagTypes extends string,
ResultType,
QueryArg,
Expand Down Expand Up @@ -342,34 +252,6 @@ interface MutationExtraOptions<
result: ResultType,
meta: undefined
): void
onCacheEntryAdded?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
MutationResultSelectorResult<
{ type: DefinitionType.mutation } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: CacheLifecyclePromises<ResultType>
): Promise<void> | void
onQuery?(
arg: QueryArg,
api: LifecycleApi<
ReducerPath,
MutationResultSelectorResult<
{ type: DefinitionType.mutation } & BaseEndpointDefinition<
QueryArg,
BaseQuery,
ResultType
>
>
>,
promises: QueryLifecyclePromises<ResultType>
): Promise<void> | void
}

export type MutationDefinition<
Expand Down

0 comments on commit e2e1fbc

Please sign in to comment.