Skip to content

Commit ba9ed75

Browse files
committed
Allow keepUnusedDataFor: Infinity
1 parent e964498 commit ba9ed75

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/toolkit/src/query/core/buildMiddleware/cacheCollection.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ export const build: SubMiddlewareBuilder = ({ reducerPath, api, context }) => {
113113
] as QueryDefinition<any, any, any, any>
114114
const keepUnusedDataFor =
115115
endpointDefinition?.keepUnusedDataFor ?? config.keepUnusedDataFor
116+
117+
if (keepUnusedDataFor === Infinity) {
118+
// Hey, user said keep this forever!
119+
return
120+
}
116121
// Prevent `setTimeout` timers from overflowing a 32-bit internal int, by
117122
// clamping the max value to be at most 1000ms less than the 32-bit max.
118123
// Look, a 24.8-day keepalive ought to be enough for anybody, right? :)

packages/toolkit/src/query/tests/cacheCollection.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => {
101101
query: () => '/success',
102102
keepUnusedDataFor: 0,
103103
}),
104+
query4: build.query<unknown, string>({
105+
query: () => '/success',
106+
keepUnusedDataFor: Infinity,
107+
}),
104108
}),
105109
keepUnusedDataFor: 29,
106110
})
@@ -126,9 +130,18 @@ describe(`query: await cleanup, keepUnusedDataFor set`, () => {
126130
expect(onCleanup).not.toHaveBeenCalled()
127131
store.dispatch(api.endpoints.query3.initiate('arg')).unsubscribe()
128132
expect(onCleanup).not.toHaveBeenCalled()
129-
jest.advanceTimersByTime(1), await waitMs()
133+
jest.advanceTimersByTime(1)
134+
await waitMs()
130135
expect(onCleanup).toHaveBeenCalled()
131136
})
137+
138+
test('endpoint keepUnusedDataFor: Infinity', async () => {
139+
expect(onCleanup).not.toHaveBeenCalled()
140+
store.dispatch(api.endpoints.query4.initiate('arg')).unsubscribe()
141+
expect(onCleanup).not.toHaveBeenCalled()
142+
jest.advanceTimersByTime(THIRTY_TWO_BIT_MAX_INT)
143+
expect(onCleanup).not.toHaveBeenCalled()
144+
})
132145
})
133146

134147
function storeForApi<

0 commit comments

Comments
 (0)