fix: Fixed issue where drafts from nested producers were frozen. Fixes #522, #524
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 9d8d995 · Browse files · 2 files·+97 added−3 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−3 removed
- tests:+94 added−0 removed
- other:+3 added−3 removed
Provenance
- Commit
acc5e54- Tree
4f65612- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
9d8d995
2 files changed+97 added−3 removed
modified:__tests__/base.js+94 added
1771 unmodified lines1772 return autoFreeze ? deepFreeze(data) : data1773 }1774 })1775 1776+ it(`works with spread #524 - ${name}`, () => {1777+ const state = {1778+ level1: {1779+ level2: {1780+ level3: "data"1781+ }1782+ }1783+ }1784+1785+ const nextState = produce(state, draft => {1786+ return {...draft}1787+ })1788+ const nextState1 = produce(state, draft => {1789+ const newLevel1 = produce(draft.level1, level1Draft => {1790+ return {...level1Draft}1791+ })1792+ draft.level1 = newLevel11793+ })1794+1795+ const nextState2 = produce(state, draft => {1796+ const newLevel1 = produce(draft.level1, level1Draft => {1797+ return {...level1Draft}1798+ })1799+ return {1800+ level1: newLevel11801+ }1802+ })1803+1804+ const nextState3 = produce(state, draft => {1805+ const newLevel1 = produce(draft.level1, level1Draft => {1806+ return Object.assign({}, level1Draft)1807+ })1808+ return Object.assign(draft, {1809+ level1: newLevel11810+ })1811+ })1812+1813+ const expected = {level1: {level2: {level3: "data"}}}1814+ expect(nextState1).toEqual(expected)1815+ expect(nextState2).toEqual(expected)1816+ expect(nextState3).toEqual(expected)1817+ })1818+1819+ it(`Something with nested producers #522 ${name}`, () => {1820+ function initialStateFactory() {1821+ return {1822+ substate: {1823+ array: [1824+ {id: "id1", value: 0},1825+ {id: "id2", value: 0}1826+ ]1827+ },1828+ array: [1829+ {id: "id1", value: 0},1830+ {id: "id2", value: 0}1831+ ]1832+ }1833+ }1834+1835+ const globalProducer = produce(draft => {1836+ draft.substate = subProducer(draft.substate)1837+ draft.array = arrayProducer(draft.array)1838+ }, initialStateFactory())1839+1840+ const subProducer = produce(draftSubState => {1841+ draftSubState.array = arrayProducer(draftSubState.array)1842+ })1843+1844+ const arrayProducer = produce(draftArray => {1845+ draftArray[0].value += 51846+ })1847+1848+ {1849+ const state = globalProducer(undefined)1850+ expect(state.array[0].value).toBe(5)1851+ expect(state.array.length).toBe(2)1852+ expect(state.array[1]).toMatchObject({1853+ id: "id2",1854+ value: 01855+ })1856+ }1857+1858+ {1859+ const state = globalProducer(undefined)1860+ expect(state.substate.array[0].value).toBe(5)1861+ expect(state.substate.array.length).toBe(2)1862+ expect(state.substate.array[1]).toMatchObject({1863+ id: "id2",1864+ value: 01865+ })1866+ expect(state.substate.array).toMatchObject(state.array)1867+ }1868+ })1869+1870 describe(`isDraft - ${name}`, () => {1871 it("returns true for object drafts", () => {1872 produce({}, state => {1873 expect(isDraft(state)).toBeTruthy()217 unmodified linesmodified:src/finalize.ts+3 added-3 removed
31 unmodified lines32 }33 if (isDraftable(result)) {34 // Finalize the result in case it contains (or is) a subset of the draft.35 result = finalize(immer, result, scope)36- maybeFreeze(immer, result)36+ if (!scope.parent) maybeFreeze(immer, result)37 }38 if (scope.patches) {39 scope.patches.push({40 op: "replace",137 unmodified lines178 }179 // Search new objects for unfinalized drafts. Frozen objects should never contain drafts.180 // TODO: the recursion over here looks weird, shouldn't non-draft stuff have it's own recursion?181 // especially the passing on of root and rootState doesn't make sense...182- else if (isDraftable(childValue) && !Object.isFrozen(childValue)) {182+ else if (isDraftable(childValue)) {183 each(childValue, (key, grandChild) =>184 finalizeProperty(185 immer,186 scope,4 unmodified lines191 grandChild,192 rootPath193 )194 )195- maybeFreeze(immer, childValue)195+ if (!scope.parent) maybeFreeze(immer, childValue)196 }197 198 if (isDraftProp && immer.onAssign && !isSetMember) {199 immer.onAssign(rootState, prop, childValue)8 unmodified lines