Merge branch 'master' into feature/fast-prune
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 · parents bcd4826, 4055768 · Browse files · 7 files·+130 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+130 added−26 removed
- tests:+81 added−0 removed
- other:+49 added−26 removed
Provenance
- Commit
f8f402c- Tree
6c88a46- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent 1
bcd4826- Parent 2
4055768
7 files changed+130 added−26 removed
modified:__tests__/base.js+25 added
1 "use strict"2 import produce, {setAutoFreeze, setUseProxies} from "../src"3 import deepFreeze from "deep-freeze"4+import cloneDeep from "lodash.clonedeep"5 6 jest.setTimeout(1000)7 8 runBaseTest("proxy (no freeze)", true, false)566 unmodified lines575 expect(next.paw.honey).toBe(false)576 expect(next).not.toBe(bear)577 })578 579+ it("should not try to change immutable data, see #66", () => {580+ const user = require("./test-data")581+582+ const base = {}583+ const next = produce(base, draft => {584+ draft.user = user585+ })586+ expect(next.user).toBe(user)587+ expect(next).not.toBe(base)588+ expect(next.user).toEqual(user)589+ })590+591+ it("should not try to change immutable data, see #66 - 2", () => {592+ const user = deepFreeze(cloneDeep(require("./test-data")))593+594+ const base = {}595+ const next = produce(base, draft => {596+ draft.user = user597+ })598+ expect(next.user).toBe(user)599+ expect(next).not.toBe(base)600+ expect(next.user).toEqual(user)601+ })602+603 afterEach(() => {604 expect(baseState).toBe(origBaseState)605 expect(baseState).toEqual(createBaseState())606 })21 unmodified linesadded:__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 linesmodified:package.json+1 added-1 removed
1 {2 "name": "immer",3- "version": "0.7.0",3+ "version": "0.8.1",4 "description": "Create your next immutable state by mutating the current one",5 "main": "dist/immer.js",6 "umd:main": "dist/immer.umd.js",7 "module": "dist/immer.module.js",67 unmodified lines