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
Provenance
- Commit
8e61145- Tree
d349b61- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
76d33df
4 files changed+62 added−5 removed
modified:__tests__/base.js+47 added
856 unmodified lines857 })858 expect(nextState).toEqual([1, 2, 100])859 })860 861+ 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+ })883+884+ 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+ })907+908 afterEach(() => {909 expect(baseState).toBe(origBaseState)910 expect(baseState).toEqual(createBaseState())911 })21 unmodified linesmodified:src/es5.js+6 added-3 removed
132 unmodified lines133 // 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 lines157 })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 linesmodified:src/proxy.js+8 added-2 removed
135 unmodified linesExpand all1361371381391401401411421431441451461461471481491501511521531541551564 unmodified lines135 unmodified linesExpand all const rootProxy = createProxy(undefined, baseState) // execute the thunk const returnValue = producer.call(rootProxy, rootProxy) // and finalize the modified proxy let result = finalize(rootProxy) let result // check whether the draft was modified and/or a value was returned if (returnValue !== undefined && returnValue !== rootProxy) { // something was returned, and it wasn't the proxy itself if (rootProxy[PROXY_STATE].modified) throw new Error(RETURNED_AND_MODIFIED_ERROR) result = returnValue
// See #117 // Should we just throw when returning a proxy which is not the root, but a subset of the original state? // Looks like a wrongly modeled reducer result = finalize(returnValue) } else { result = finalize(rootProxy) } // revoke all proxies each(proxies, (_, p) => p.revoke()) return result4 unmodified linesmodified:changelog.md+1 added
1 # Changelog2 3 # Next4 5+* 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.7 8 ### 1.1.19 133 unmodified lines