Skip to content

Commit

Permalink
fix(es5): onDelete hook
Browse files Browse the repository at this point in the history
The `assigned` map cannot be trusted in es5, because of perf optimizations when a patch listener is not used.
  • Loading branch information
aleclarson committed Jan 21, 2019
1 parent 5ff645c commit 46cc6a1
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/immer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {generatePatches} from "./patches"
import {
assign,
each,
has,
is,
isDraft,
isDraftable,
Expand Down Expand Up @@ -135,9 +136,18 @@ export class Immer {
state.finalized = true
this.finalizeTree(state.draft, path, patches, inversePatches)
if (this.onDelete) {
const {assigned} = state
for (const prop in assigned)
assigned[prop] || this.onDelete(state, prop)
// The `assigned` object is unreliable with ES5 drafts.
if (this.useProxies) {
const {assigned} = state
for (const prop in assigned) {
if (!assigned[prop]) this.onDelete(state, prop)
}
} else {
const {base, copy} = state
eachOwn(base, prop => {
if (!has(copy, prop)) this.onDelete(state, prop)
})
}
}
if (this.onCopy) this.onCopy(state)

Expand Down

0 comments on commit 46cc6a1

Please sign in to comment.