Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear Map produces incorrect patches #663

Closed
2 of 3 tasks
LYF1999 opened this issue Sep 2, 2020 · 3 comments 路 Fixed by #664
Closed
2 of 3 tasks

clear Map produces incorrect patches #663

LYF1999 opened this issue Sep 2, 2020 · 3 comments 路 Fixed by #664

Comments

@LYF1999
Copy link
Contributor

LYF1999 commented Sep 2, 2020

馃悰 Bug Report

clear Map produce incorrect patches

Link to repro

codebox link: https://codesandbox.io/s/immer-sandbox-forked-zz8le

immer source code

state.assigned_ = new Map()

this line clears the assigned_, and Patch Plugin gen patches depends on each(state.assigned_)

function generatePatchesFromAssigned(
state: MapState | ES5ObjectState | ProxyObjectState,
basePath: PatchPath,
patches: Patch[],
inversePatches: Patch[]
) {
const {base_, copy_} = state
each(state.assigned_!, (key, assignedValue) => {
const origValue = get(base_, key)
const value = get(copy_!, key)
const op = !assignedValue ? REMOVE : has(base_, key) ? REPLACE : ADD
if (origValue === value && op === REPLACE) return
const path = basePath.concat(key as any)
patches.push(op === REMOVE ? {op, path} : {op, path, value})
inversePatches.push(
op === ADD
? {op: REMOVE, path}
: op === REMOVE
? {op: ADD, path, value: clonePatchValueIfNeeded(origValue)}
: {op: REPLACE, path, value: clonePatchValueIfNeeded(origValue)}
)
})
}

is this by design? or a bug.

To Reproduce

just run the codesanbox link above

Observed behavior

changes is a empty array.

Expected behavior

changes is [{ type: 'Remove', path: ['t2', 't'] }]

Environment

We only accept bug reports against the latest Immer version.

  • Immer version:
  • I filed this report against the latest version of Immer
  • Occurs with setUseProxies(true)
  • Occurs with setUseProxies(false) (ES5 only)
@LYF1999 LYF1999 changed the title clear Map produce incorrect patches clear Map produces incorrect patches Sep 2, 2020
@mweststrate mweststrate added the bug label Sep 2, 2020
@mweststrate
Copy link
Collaborator

Looks like a bug indeed, PR welcome!

@LYF1999
Copy link
Contributor Author

LYF1999 commented Sep 2, 2020

OK, I will submit a PR later.

@aleclarson
Copy link
Member

馃帀 This issue has been resolved in version 7.0.9 馃帀

The release is available on:

Your semantic-release bot 馃摝馃殌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants