Skip to content

Commit a631974

Browse files
committed
Rewrite immutable middleware to enable error extraction
1 parent 436834f commit a631974

File tree

1 file changed

+11
-32
lines changed

1 file changed

+11
-32
lines changed

packages/toolkit/src/immutableStateInvariantMiddleware.ts

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,6 @@ import { getTimeMeasureUtils } from './utils'
33

44
type EntryProcessor = (key: string, value: any) => any
55

6-
const isProduction: boolean = process.env.NODE_ENV === 'production'
7-
const prefix: string = 'Invariant failed'
8-
9-
// Throw an error if the condition fails
10-
// Strip out error messages for production
11-
// > Not providing an inline default argument for message as the result is smaller
12-
function invariant(condition: any, message?: string) {
13-
if (condition) {
14-
return
15-
}
16-
// Condition not passed
17-
18-
// In production we strip the message but still throw
19-
if (isProduction) {
20-
throw new Error(prefix)
21-
}
22-
23-
// When not in production we allow the message to pass through
24-
// *This block will be removed in production builds*
25-
throw new Error(`${prefix}: ${message || ''}`)
26-
}
27-
286
/**
297
* The default `isImmutable` function.
308
*
@@ -62,7 +40,7 @@ function trackProperties(
6240
const tracked: Partial<TrackedProperty> = { value: obj }
6341

6442
if (!isImmutable(obj) && !checkedObjects.has(obj)) {
65-
checkedObjects.add(obj);
43+
checkedObjects.add(obj)
6644
tracked.children = {}
6745

6846
for (const key in obj) {
@@ -248,12 +226,13 @@ export function createImmutableStateInvariantMiddleware(
248226
// Track before potentially not meeting the invariant
249227
tracker = track(state)
250228

251-
invariant(
252-
!result.wasMutated,
253-
`A state mutation was detected between dispatches, in the path '${
254-
result.path || ''
255-
}'. This may cause incorrect behavior. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)`
256-
)
229+
if (result.wasMutated) {
230+
throw new Error(
231+
`A state mutation was detected between dispatches, in the path '${
232+
result.path || ''
233+
}'. This may cause incorrect behavior. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)`
234+
)
235+
}
257236
})
258237

259238
const dispatchedAction = next(action)
@@ -265,15 +244,15 @@ export function createImmutableStateInvariantMiddleware(
265244
// Track before potentially not meeting the invariant
266245
tracker = track(state)
267246

268-
result.wasMutated &&
269-
invariant(
270-
!result.wasMutated,
247+
if (result.wasMutated) {
248+
throw new Error(
271249
`A state mutation was detected inside a dispatch, in the path: ${
272250
result.path || ''
273251
}. Take a look at the reducer(s) handling the action ${stringify(
274252
action
275253
)}. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)`
276254
)
255+
}
277256
})
278257

279258
measureUtils.warnIfExceeded()

0 commit comments

Comments
 (0)