Fixed #66: Incorrectly trying to modify a frozen object
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 d5de885 · Browse files · 5 files·+104 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+104 added−25 removed
- tests:+56 added−0 removed
- other:+48 added−25 removed
Provenance
- Commit
0834d8a- Tree
dd23f95- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
d5de885
5 files changed+104 added−25 removed
added:__tests__/test-data.json+56 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 { "_id": "5a5e59f1bc9132a90101f521", "index": 0, "guid": "8dc96fdb-0526-4bc4-a0ef-fb08e6c06e77", "isActive": true, "balance": "$3,359.66", "picture": "http://placehold.it/32x32", "age": 40, "eyeColor": "blue", "name": "Samantha Richardson", "gender": "female", "company": "COWTOWN", "email": "samantharichardson@cowtown.com", "phone": "+1 (847) 556-2336", "address": "419 Bergen Street, Emison, Nebraska, 554", "about": "Enim magna ut do esse voluptate et commodo anim laborum proident aute. Nulla non sit in incididunt minim velit. Laboris et tempor enim cillum ex adipisicing excepteur. Dolore consectetur eiusmod eu ad eu. Veniam qui sunt est reprehenderit et ut occaecat eiusmod pariatur aliquip officia ipsum.\r\n", "registered": "2017-07-09T02:30:48 -02:00", "latitude": -38.375287, "longitude": -27.090184, "tags": [ "ex", "cillum", "cillum", "Lorem", "fugiat", "dolore", "nulla", "anim", "aliqua", "sint" ], "friends": [ { "id": 0, "name": "Adrian Dunlap" }, { "id": 1, "name": "Salinas Copeland" }, { "id": 2, "name": "Doreen Emerson" }, { "id": 3, "name": "Willis Long" }, { "id": 4, "name": "Josefina Garza" } ], "greeting": "Hello, Samantha Richardson! You have 8 unread messages.", "favoriteFruit": "banana" }No newline at end of filemodified:src/common.js+26 added
29 unmodified lines30313233343536373839404142434445464748495051525354555657585929 unmodified lines */export function setAutoFreeze(enableAutoFreeze) { autoFreeze = enableAutoFreeze}
export function finalizeNonProxiedObject(parent, finalizer) { // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well if (!isProxyable(parent)) return if (Object.isFrozen(parent)) return let modified = false if (Array.isArray(parent)) { for (let i = 0; i < parent.length; i++) { const child = parent[i] if (isProxy(child)) { parent[i] = finalizer(child) modified = true } else finalizeNonProxiedObject(child, finalizer) } } else { for (let key in parent) { const child = parent[key] if (isProxy(child)) { parent[key] = finalizer(child) modified = true } else finalizeNonProxiedObject(child, finalizer) } } if (modified) freeze(parent)}modified:src/es5.js+9 added-12 removed
1 "use strict"2 // @ts-check3 4-import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"4+import {5+ autoFreeze,6+ isProxyable,7+ isProxy,8+ freeze,9+ PROXY_STATE,10+ finalizeNonProxiedObject11+} from "./common"12 13 const descriptors = {}14 let states = null15 133 unmodified lines149 if (Array.isArray(state.base)) return finalizeArray(proxy, state)150 return finalizeObject(proxy, state)151 } else return state.base152 } else if (proxy !== null && typeof proxy === "object") {146- // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original147- // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well148- if (Array.isArray(proxy)) {149- for (let i = 0; i < proxy.length; i++) proxy[i] = finalize(proxy[i])150- return freeze(proxy)151- }152- const proto = Object.getPrototypeOf(proxy)153- if (proto === null || proto === Object.prototype) {154- for (let key in proxy) proxy[key] = finalize(proxy[key])155- return freeze(proxy)156- }153+ finalizeNonProxiedObject(proxy, finalize)154 }155 return proxy156 }157 76 unmodified linesmodified:src/proxy.js+9 added-13 removed
1 "use strict"2 // @ts-check3 4-import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"4+import {5+ autoFreeze,6+ isProxyable,7+ isProxy,8+ freeze,9+ PROXY_STATE,10+ finalizeNonProxiedObject11+} from "./common"12 13 let revocableProxies = null14 15 const objectTraps = {121 unmodified lines137 if (Array.isArray(state.base)) return finalizeArray(state)138 return finalizeObject(state)139 } else return state.base140 } else if (base !== null && typeof base === "object") {134- // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original135- // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well136- // TODO: optimize this; walk the tree without writing to find proxies137- if (Array.isArray(base)) {138- for (let i = 0; i < base.length; i++) base[i] = finalize(base[i])139- return freeze(base)140- }141- const proto = Object.getPrototypeOf(base)142- if (proto === null || proto === Object.prototype) {143- for (let key in base) base[key] = finalize(base[key])144- return freeze(base)145- }141+ finalizeNonProxiedObject(base, finalize)142 }143 return base144 }145 40 unmodified linesmodified:changelog.md+4 added
1234567891075 unmodified lines# Changelog
### 0.8.1
* Fixed [#66](https://github.com/mweststrate/immer/pull/66), assigning an already frozen object to a state threw resulting in exceptions being thrown as Immer unnecessarily tried to rewrite them.
### 0.8.0
* The built is now being rolled up [#64](https://github.com/mweststrate/immer/pull/64) by [Arthur Denner](https://github.com/arthurdenner). A minified gzipped built is only 2kb!* There are no longer separate builds available for the proxy and es5 implementation. The sources where merged to allow for more code reuse.75 unmodified lines