From 46cc6a1511fa707f55f183f840524a969f198310 Mon Sep 17 00:00:00 2001 From: aleclarson Date: Sun, 20 Jan 2019 14:49:19 -0500 Subject: [PATCH] fix(es5): onDelete hook The `assigned` map cannot be trusted in es5, because of perf optimizations when a patch listener is not used. --- src/immer.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/immer.js b/src/immer.js index 46129204..bca73181 100644 --- a/src/immer.js +++ b/src/immer.js @@ -4,6 +4,7 @@ import {generatePatches} from "./patches" import { assign, each, + has, is, isDraft, isDraftable, @@ -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)