Skip to content

Commit

Permalink
fix(es5): throw if a modified child draft is returned
Browse files Browse the repository at this point in the history
To be more consistent with the native Proxy implementation.
  • Loading branch information
aleclarson committed Feb 2, 2019
1 parent 271cd1b commit eabe9db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 12 additions & 1 deletion __tests__/base.js
Expand Up @@ -794,14 +794,25 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
expect(res).toEqual({x: 4})
})

it("can return a child draft", () => {
it("can return an unmodified child draft", () => {
const base = {a: {}}
const res = produce(base, d => {
return d.a
})
expect(res).toBe(base.a)
})

// TODO: Avoid throwing if only the child draft was modified.
it("cannot return a modified child draft", () => {
const base = {a: {}}
expect(() => {
produce(base, d => {
d.a.b = 1
return d.a
})
}).toThrow(/^An immer producer returned/)
})

it("can return a frozen object", () => {
const res = deepFreeze([{x: 3}])
expect(produce({}, () => res)).toBe(res)
Expand Down
4 changes: 4 additions & 0 deletions src/es5.js
Expand Up @@ -26,6 +26,10 @@ export function willFinalize(result, baseDraft, needPatches) {
// This is faster when we don't care about which attributes changed.
markChangesSweep(scope)
}
// When a child draft is returned, look for changes.
else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {
markChangesSweep(scope.drafts)
}
}

export function createDraft(base, parent) {
Expand Down

0 comments on commit eabe9db

Please sign in to comment.