Skip to content

Commit cd6a5a3

Browse files
phryneasmarkerikson
authored andcommitted
re-add faulty getRunningOperationPromises
but have it throw in development
1 parent a5e8905 commit cd6a5a3

File tree

4 files changed

+114
-17
lines changed

4 files changed

+114
-17
lines changed

packages/toolkit/src/query/core/buildInitiate.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type { ApiEndpointQuery } from './module'
1515
import type { BaseQueryError, QueryReturnValue } from '../baseQueryTypes'
1616
import type { QueryResultSelectorResult } from './buildSelectors'
1717
import type { Dispatch } from 'redux'
18+
import { isNotNullish } from '../utils/isNotNullish'
1819

1920
declare module './module' {
2021
export interface ApiEndpointQuery<
@@ -218,6 +219,37 @@ export function buildInitiate({
218219
getRunningMutationThunk,
219220
getRunningQueriesThunk,
220221
getRunningMutationsThunk,
222+
getRunningOperationPromises,
223+
removalWarning,
224+
}
225+
226+
/** @deprecated to be removed in 2.0 */
227+
function removalWarning(): never {
228+
throw new Error(
229+
`This method had to be removed due to a conceptual bug in RTK.
230+
Please see https://github.com/reduxjs/redux-toolkit/pull/2481 for details.
231+
See https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering for new guidance on SSR.`
232+
)
233+
}
234+
235+
/** @deprecated to be removed in 2.0 */
236+
function getRunningOperationPromises() {
237+
if (
238+
typeof process !== 'undefined' &&
239+
process.env.NODE_ENV === 'development'
240+
) {
241+
removalWarning()
242+
} else {
243+
const extract = <T>(
244+
v: Map<Dispatch<AnyAction>, Record<string, T | undefined>>
245+
) =>
246+
Array.from(v.values()).flatMap((queriesForStore) =>
247+
queriesForStore ? Object.values(queriesForStore) : []
248+
)
249+
return [...extract(runningQueries), ...extract(runningMutations)].filter(
250+
isNotNullish
251+
)
252+
}
221253
}
222254

223255
function getRunningQueryThunk(endpointName: string, queryArgs: any) {
@@ -251,16 +283,12 @@ export function buildInitiate({
251283

252284
function getRunningQueriesThunk() {
253285
return (dispatch: Dispatch) =>
254-
Object.values(runningQueries.get(dispatch) || {}).filter(
255-
<T>(t: T | undefined): t is T => !!t
256-
)
286+
Object.values(runningQueries.get(dispatch) || {}).filter(isNotNullish)
257287
}
258288

259289
function getRunningMutationsThunk() {
260290
return (dispatch: Dispatch) =>
261-
Object.values(runningMutations.get(dispatch) || {}).filter(
262-
<T>(t: T | undefined): t is T => !!t
263-
)
291+
Object.values(runningMutations.get(dispatch) || {}).filter(isNotNullish)
264292
}
265293

266294
function middlewareWarning(getState: () => RootState<{}, string, string>) {

packages/toolkit/src/query/core/module.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,22 @@ declare module '../apiTypes' {
142142
util: {
143143
/**
144144
* This method had to be removed due to a conceptual bug in RTK.
145-
* Please see https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering for details.
145+
*
146+
* Despite TypeScript errors, it will continue working in the "buggy" way it did
147+
* before in production builds and will be removed in the next major release.
148+
*
149+
* Nonetheless, you should immediately replace it with the new recommended approach.
150+
* See https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering for new guidance on SSR.
151+
*
152+
* Please see https://github.com/reduxjs/redux-toolkit/pull/2481 for details.
146153
* @deprecated
147154
*/
148155
getRunningOperationPromises: never // this is now types as `never` to immediately throw TS errors on use, but still allow for a comment
149156

150157
/**
151158
* This method had to be removed due to a conceptual bug in RTK.
152-
* Please see https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering for details.
159+
* It has been replaced by `api.util.getRunningQueryThunk` and `api.util.getRunningMutationThunk`.
160+
* Please see https://github.com/reduxjs/redux-toolkit/pull/2481 for details.
153161
* @deprecated
154162
*/
155163
getRunningOperationPromise: never // this is now types as `never` to immediately throw TS errors on use, but still allow for a comment
@@ -541,6 +549,8 @@ export const coreModule = (): Module<CoreModule> => ({
541549
getRunningMutationsThunk,
542550
getRunningQueriesThunk,
543551
getRunningQueryThunk,
552+
getRunningOperationPromises,
553+
removalWarning,
544554
} = buildInitiate({
545555
queryThunk,
546556
mutationThunk,
@@ -549,16 +559,9 @@ export const coreModule = (): Module<CoreModule> => ({
549559
context,
550560
})
551561

552-
function removedSSRHelper(): never {
553-
throw new Error(
554-
`This method had to be removed due to a conceptual bug in RTK.
555-
Please see https://redux-toolkit.js.org/rtk-query/usage/server-side-rendering for details.`
556-
)
557-
}
558-
559562
safeAssign(api.util, {
560-
getRunningOperationPromises: removedSSRHelper as any,
561-
getRunningOperationPromise: removedSSRHelper as any,
563+
getRunningOperationPromises: getRunningOperationPromises as any,
564+
getRunningOperationPromise: removalWarning as any,
562565
getRunningMutationThunk,
563566
getRunningMutationsThunk,
564567
getRunningQueryThunk,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isNotNullish<T>(v: T | null | undefined): v is T {
2+
return v != null
3+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-disable import/first */
2+
// @ts-nocheck
3+
4+
// reducer.ts or whatever
5+
6+
import { combineSlices } from '@reduxjs/toolkit'
7+
8+
import { sliceA } from 'fileA'
9+
import { sliceB } from 'fileB'
10+
import { lazySliceC } from 'fileC'
11+
import type { lazySliceD } from 'fileD'
12+
13+
import { anotherReducer } from 'somewhere'
14+
15+
export interface LazyLoadedSlices {}
16+
17+
export const rootReducer = combineSlices(sliceA, sliceB, {
18+
another: anotherReducer,
19+
}).withLazyLoadedSlices<LazyLoadedSlices>()
20+
/*
21+
results in a return type of
22+
{
23+
[sliceA.name]: SliceAState,
24+
[sliceB.name]: SliceBState,
25+
another: AnotherState,
26+
[lazySliceC.name]?: SliceCState, // see fileC.ts to understand why this appears here
27+
[lazySliceD.name]?: SliceDState, // see fileD.ts to understand why this appears here
28+
}
29+
*/
30+
31+
// fileC.ts
32+
// "naive" approach
33+
34+
import { rootReducer, RootState } from './reducer'
35+
import { createSlice } from '@reduxjs/toolkit'
36+
37+
interface SliceCState {
38+
foo: string
39+
}
40+
41+
declare module './reducer' {
42+
export interface LazyLoadedSlices {
43+
[lazySliceC.name]: SliceCState
44+
}
45+
}
46+
47+
export const lazySliceC = createSlice({
48+
/* ... */
49+
})
50+
/**
51+
* Synchronously call `injectSlice` in file.
52+
*/
53+
rootReducer.injectSlice(lazySliceC)
54+
55+
// might want to add code for HMR as well here
56+
57+
// this will still error - `lazySliceC` is optional here
58+
const naiveSelectFoo = (state: RootState) => state.lazySliceC.foo
59+
60+
const selectFoo = rootReducer.withSlice(lazySliceC).selector((state) => {
61+
// `lazySlice` is guaranteed to not be `undefined` here.
62+
return state.lazySlice.foo
63+
})

0 commit comments

Comments
 (0)