Fix gnDownload reducer mutating previous state on DOWNLOAD_METADATA_COMPLETE#2565
Fix gnDownload reducer mutating previous state on DOWNLOAD_METADATA_COMPLETE#2565Valyrian-Code wants to merge 2 commits into
Conversation
…OMPLETE
{ ...state } is shallow, so newState.downloads[linkType] is the same
reference as the incoming state; delete downloads[action.pk] then mutates
the previous state (reducers must be pure). Copy the link bucket before
deleting so the previous state is untouched.
Adds a test in gndownload-test.js asserting the previous state is unchanged
after dispatch; fails on master, passes with the fix.
There was a problem hiding this comment.
Code Review
This pull request fixes a state mutation bug in the gndownload reducer during the DOWNLOAD_METADATA_COMPLETE action by copying the target downloads object before deleting the action's primary key. A corresponding unit test was also added to verify that the previous state is not mutated. The reviewer suggested a cleaner, more idiomatic approach using object destructuring with rest syntax to immutably omit the key instead of copying and using the delete operator.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Per review: replace the copy-then-delete with object rest destructuring
({ [action.pk]: removed, ...remaining }) to omit the key without the delete
operator. Behaviour unchanged; non-mutation test still passes.
|
Done — switched to object rest destructuring ( |
Problem
In
reducers/gnDownload, theDOWNLOAD_METADATA_COMPLETEcase mutates the previous state:{ ...state }is shallow, sonewState.downloads[linkType]is the same object as the incomingstate.downloads[linkType]. Thedeleteremoves the key from the previous state before the immutable spread runs. Reducers must be pure — mutating prior state can break referential-equality checks, memoized selectors, and time-travel debugging.Fix
Copy the link bucket before deleting, so the previous state is untouched:
Test
Added to
reducers/__tests__/gndownload-test.js: captures the previous link bucket and asserts it is unchanged after dispatch. Fails onmaster(Expected {} to equal { 1: true }), passes with the fix. Output value is unchanged. Full suite green.