-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix trackProperties recursion #4844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 6a17ea9:
|
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Same problem here, which can be solved with this PR |
passdown the checkedObjects to detect circularity
The stack trace here is particularly odd, given that line 138 seems to be in the middle of a comment?
redux-toolkit/packages/toolkit/src/immutableStateInvariantMiddleware.ts Lines 134 to 150 in 6a17ea9
|
I can reproduce the brokenness from this change locally, although the line numbers are different:
|
It looks like the " state.foo.bar = new Array(10).fill({ value: 'more' }) rather than the Honestly I'm not sure what's going on here, and this is low enough priority that I don't want to get bogged down debugging it atm. |
Just solve it @paztis. Can you try it and modify #4844 ? Problem: I was encountering false-positive mutation detections because my Solution: I fixed the issue by introducing memoization in
This change ensures that each object reference is tracked only once, completely eliminating the false-positive mutation detections. This is the complete code for trackProperties with the problem fixed. @paztis , only have replace and update the PR. function trackProperties(
isImmutable: IsImmutableFunc,
ignorePaths: IgnorePaths = [],
obj: Record<string, any>,
path: string = '',
checkedObjects: WeakMap<any, TrackedProperty> = new WeakMap(),
) {
if (typeof obj !== 'object' || obj === null || isImmutable(obj)) {
return { value: obj, children: {} }
}
if (checkedObjects.has(obj)) {
return checkedObjects.get(obj)!
}
const tracked: TrackedProperty = { value: obj, children: {} }
checkedObjects.set(obj, tracked)
for (const key in obj) {
const childPath = path ? path + '.' + key : key
if (ignorePaths.length && ignorePaths.indexOf(childPath) !== -1) {
continue
}
tracked.children[key] = trackProperties(
isImmutable,
ignorePaths,
obj[key],
childPath,
checkedObjects,
)
}
return tracked as TrackedProperty
} |
I just tried out your suggested changes, and I don't think it actually fixes the issue. I think the first issue is that our existing tests don't correctly capture this behavior. So, I tried changing one of our existing tests to try to force a failure: it('works correctly with circular references', () => {
const next: MWNext = (action) => action
const dispatch = middleware()(next)
let x: any = {}
let y: any = {}
x.y = y
y.x = x
y.items = new Array(5).fill({ data: 'testing' })
expect(() => {
dispatch({ type: 'SOME_ACTION', x })
}).not.toThrow()
// ADDED
const tracker = trackForMutations(isImmutableDefault, [], x)
expect(() => tracker.detectMutations()).not.toThrow()
}) That does induce an error:
However, when I tried using your suggested I'd been hoping to include this in 2.7.0, which I'm going to publish shortly. But, given that, I'm going to set this aside again. Could you please file a new PR that includes both the code changes and a test that fails with the existing implementation and passes with the new one? |
Of course @markerikson. I'm going to start working on a new PR that addresses these issues and will create it as soon as possible so it can be incorporated into the next release. Maybe you can close this one, and I will reference this in the new PR. |
passdown the checkedObjects to detect circularity