Skip to content

Commit

Permalink
task(query): Revert making subscription cleanup required
Browse files Browse the repository at this point in the history
  • Loading branch information
bbboydston committed Jan 18, 2022
1 parent c198c1c commit 81d929a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 15 additions & 6 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
const hasMaxAge = (
options: any
): options is { ifOlderThan: false | number } => 'ifOlderThan' in options
const handlePrefetchSubscription = (result: QueryActionCreatorResult<any>) =>
result.unwrap().finally(result.unsubscribe)
const hasCleanup = (options: any): options is { cleanup?: boolean } =>
'cleanup' in options

const prefetch =
<EndpointName extends QueryKeys<Definitions>>(
Expand All @@ -445,33 +445,42 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
(dispatch: ThunkDispatch<any, any, any>, getState: () => any) => {
const force = hasTheForce(options) && options.force
const maxAge = hasMaxAge(options) && options.ifOlderThan
const cleanupSubscription = hasCleanup(options) && options.cleanup

const queryAction = (force: boolean = true) =>
(api.endpoints[endpointName] as ApiEndpointQuery<any, any>).initiate(
arg,
{ forceRefetch: force }
)
const queryCleanup = (
result: QueryActionCreatorResult<any>,
doCleanup: boolean = false
) => {
if (doCleanup) {
result.unwrap().finally(result.unsubscribe)
}
}
const latestStateValue = (
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
).select(arg)(getState())

if (force) {
handlePrefetchSubscription(dispatch(queryAction()))
queryCleanup(dispatch(queryAction()), cleanupSubscription)
} else if (maxAge) {
const lastFulfilledTs = latestStateValue?.fulfilledTimeStamp
if (!lastFulfilledTs) {
handlePrefetchSubscription(dispatch(queryAction()))
queryCleanup(dispatch(queryAction()), cleanupSubscription)
return
}
const shouldRetrigger =
(Number(new Date()) - Number(new Date(lastFulfilledTs))) / 1000 >=
maxAge
if (shouldRetrigger) {
handlePrefetchSubscription(dispatch(queryAction()))
queryCleanup(dispatch(queryAction()), cleanupSubscription)
}
} else {
// If prefetching with no options, just let it try
handlePrefetchSubscription(dispatch(queryAction(false)))
queryCleanup(dispatch(queryAction(false)), cleanupSubscription)
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/toolkit/src/query/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ import { enablePatches } from 'immer'
* `force`
* - If `force: true`, it will ignore the `ifOlderThan` value if it is set and the query will be run even if it exists in the cache.
*/
export type PrefetchOptions =
export type PrefetchOptions = (
| {
ifOlderThan?: false | number
}
| { force?: boolean }
) & {
cleanup?: boolean
}

export const coreModuleName = /* @__PURE__ */ Symbol()
export type CoreModule =
Expand Down

0 comments on commit 81d929a

Please sign in to comment.