Skip to content
Myr

Fixed #117 returned proxies not being finalized when only a subset was returned

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 76d33df · Browse files · 4 files+62 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+62 added−5 removed

  • tests:+47 added−0 removed
  • other:+15 added−5 removed

4 files+62 added−5 removed

modified:__tests__/base.js+47 added
856 unmodified lines
857 })858 expect(nextState).toEqual([1, 2, 100])859 })860861 it("should fix #117", () => {862 const reducer = (state, action) =>863 produce(state, draft => {864 switch (action.type) {865 case "SET_STARTING_DOTS":866 return draft.availableStartingDots.map(a => a)867 default:868 break869 }870 })871 const base = {872 availableStartingDots: [873 {dots: 4, count: 1},874 {dots: 3, count: 2},875 {dots: 2, count: 3},876 {dots: 1, count: 4}877 ]878 }879 const next = reducer(base, {type: "SET_STARTING_DOTS"})880 expect(next).toEqual(base.availableStartingDots)881 expect(next).not.toBe(base.availableStartingDots)882 })883884 it("should fix #117 - 2", () => {885 const reducer = (state, action) =>886 produce(state, draft => {887 switch (action.type) {888 case "SET_STARTING_DOTS":889 return {890 dots: draft.availableStartingDots.map(a => a)891 }892 default:893 break894 }895 })896 const base = {897 availableStartingDots: [898 {dots: 4, count: 1},899 {dots: 3, count: 2},900 {dots: 2, count: 3},901 {dots: 1, count: 4}902 ]903 }904 const next = reducer(base, {type: "SET_STARTING_DOTS"})905 expect(next).toEqual({dots: base.availableStartingDots})906 })907908 afterEach(() => {909 expect(baseState).toBe(origBaseState)910 expect(baseState).toEqual(createBaseState())911 })
21 unmodified lines
modified:src/es5.js+6 added-3 removed
132 unmodified lines
133 // If we first shorten the length, our array interceptors will be removed.134 // If after that new items are added, result in the same original length,135 // those last items will have no intercepting property.136 // So if there is no own descriptor on the last position, we know that items were removed and added137 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check138 // the last one139 const descriptor = Object.getOwnPropertyDescriptor(proxy, proxy.length - 1)140 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)141 if (descriptor && !descriptor.get) return true142 // For all other cases, we don't have to compare, as they would have been picked up by the index setters143 return false144}
12 unmodified lines
157 })158 // find and mark all changes (for parts not done yet)159 // TODO: store states by depth, to be able guarantee processing leaves first160 markChanges()158 let result = finalize(rootProxy)161 let result162 // check whether the draft was modified and/or a value was returned163 if (returnValue !== undefined && returnValue !== rootProxy) {164 // something was returned, and it wasn't the proxy itself165 if (rootProxy[PROXY_STATE].modified)166 throw new Error(RETURNED_AND_MODIFIED_ERROR)164 result = returnValue165 }167 result = finalize(returnValue)168 } else result = finalize(rootProxy)169 // make sure all proxies become unusable170 each(states, (_, state) => {171 state.finished = true172 })
38 unmodified lines
modified:src/proxy.js+8 added-2 removed
modified:changelog.md+1 added
1# Changelog23# Next45* Fixed [#117](https://github.com/mweststrate/immer/issues/117), proxy was not finalized when returning a subset of the state6* Fixed [#116](https://github.com/mweststrate/immer/issues/116), changes to arrays that ended up with the same length were not properly detected.78### 1.1.19
133 unmodified lines