Skip to content

Commit 46cc6a1

Browse files
committed
fix(es5): onDelete hook
The `assigned` map cannot be trusted in es5, because of perf optimizations when a patch listener is not used.
1 parent 5ff645c commit 46cc6a1

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/immer.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {generatePatches} from "./patches"
44
import {
55
assign,
66
each,
7+
has,
78
is,
89
isDraft,
910
isDraftable,
@@ -135,9 +136,18 @@ export class Immer {
135136
state.finalized = true
136137
this.finalizeTree(state.draft, path, patches, inversePatches)
137138
if (this.onDelete) {
138-
const {assigned} = state
139-
for (const prop in assigned)
140-
assigned[prop] || this.onDelete(state, prop)
139+
// The `assigned` object is unreliable with ES5 drafts.
140+
if (this.useProxies) {
141+
const {assigned} = state
142+
for (const prop in assigned) {
143+
if (!assigned[prop]) this.onDelete(state, prop)
144+
}
145+
} else {
146+
const {base, copy} = state
147+
eachOwn(base, prop => {
148+
if (!has(copy, prop)) this.onDelete(state, prop)
149+
})
150+
}
141151
}
142152
if (this.onCopy) this.onCopy(state)
143153

0 commit comments

Comments
 (0)