Skip to content
Myr

fix: reconcile if the original value is assigned after creating a draft. Fixes #659

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent 1b70ad5 · Browse files · 2 files+97 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+97 added−6 removed

  • tests:+81 added−0 removed
  • other:+16 added−6 removed

2 files+97 added−6 removed

modified:__tests__/regressions.js+81 added
156 unmodified lines
157 draft.foo[0].isActive = true158 })159 expect(newData.foo[0].isActive).toBe(true)160 })161162 test("#659 no reconciliation after read", () => {163 const bar = {}164 const foo = {bar}165166 const next = produce(foo, draft => {167 draft.bar168 draft.bar = bar169 })170 expect(next).toBe(foo)171 })172173 test("#659 no reconciliation after read - 2", () => {174 const bar = {}175 const foo = {bar}176177 const next = produce(foo, draft => {178 const subDraft = draft.bar179 draft.bar = bar180 subDraft.x = 3 // this subDraft is not part of the end result, so ignore181 })182183 expect(next).toEqual(foo)184 })185186 test("#659 no reconciliation after read - 3", () => {187 const bar = {}188 const foo = {bar}189190 const next = produce(foo, draft => {191 const subDraft = draft.bar192 subDraft.x = 3 // this subDraft is not part of the end result, so ignore193 draft.bar = bar194 })195 expect(next).toEqual(foo)196 })197198 // Disabled: these are optimizations that would be nice if they199 // could be detected, but don't change the correctness of the result200 test.skip("#659 no reconciliation after read - 4", () => {201 const bar = {}202 const foo = {bar}203204 const next = produce(foo, draft => {205 const subDraft = draft.bar206 draft.bar = bar207 subDraft.x = 3 // this subDraft is not part of the end result, so ignore208 })209210 expect(next).toBe(foo)211 })212213 // Disabled: these are optimizations that would be nice if they214 // could be detected, but don't change the correctness of the result215 test.skip("#659 no reconciliation after read - 5", () => {216 const bar = {}217 const foo = {bar}218219 const next = produce(foo, draft => {220 const subDraft = draft.bar221 subDraft.x = 3 // this subDraft is not part of the end result, so ignore222 draft.bar = bar223 })224 expect(next).toBe(foo)225 })226227 test("#659 no reconciliation after read - 6", () => {228 const bar = {}229 const foo = {bar}230231 const next = produce(foo, draft => {232 const subDraft = draft.bar233 subDraft.x = 3 // this subDraft is not part of the end result, so ignore234 draft.bar = bar235 draft.bar = subDraft236 })237 expect(next).not.toBe(foo)238 expect(next).toEqual({239 bar: {x: 3}240 })241 })242 })243}
modified:src/core/proxy.ts+16 added-6 removed
16 unmodified lines
17 createProxy,18 ProxyTypeProxyObject,19 ProxyTypeProxyArray20} from "../internal"21import {isDraft} from "../utils/common"2223interface ProxyBaseState extends ImmerBaseState {24 assigned_: {25 [property: string]: boolean
103 unmodified lines
129 },130 ownKeys(state) {131 return Reflect.ownKeys(latest(state))132 },132 set(state, prop: string /* strictly not, but helps TS */, value) {133 set(134 state: ProxyObjectState,135 prop: string /* strictly not, but helps TS */,136 value137 ) {138 const desc = getDescriptorFromProto(latest(state), prop)139 if (desc?.set) {140 // special case: if this write is captured by a setter, we have141 // to trigger it with the correct context142 desc.set.call(state.draft_, value)143 return true144 }140 state.assigned_[prop] = true145 if (!state.modified_) {146 // the last check is because we need to be able to distinguish setting a non-existig to undefined (which is a change)147 // from setting an existing property with value undefined to undefined (which is not a change)144 if (145 is(value, peek(latest(state), prop)) &&146 (value !== undefined || has(state.base_, prop))147 )148 const current = peek(latest(state), prop)149 // special case, if we assigning the original value to a draft, we can ignore the assignment150 const currentState: ProxyObjectState = current?.[DRAFT_STATE]151 if (currentState && currentState.base_ === value) {152 state.copy_![prop] = value153 state.assigned_[prop] = false154 return true155 }156 if (is(value, current) && (value !== undefined || has(state.base_, prop)))157 return true158 prepareCopy(state)159 markChanged(state)160 }161 // @ts-ignore162 state.copy_![prop] = value163 state.assigned_[prop] = true164 return true165 },166 deleteProperty(state, prop: string) {167 // The `undefined` check is a fast path for pre-existing keys.
104 unmodified lines