Skip to content
Myr

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

7 files+130 added−26 removed

modified:__tests__/base.js+25 added
1"use strict"2import produce, {setAutoFreeze, setUseProxies} from "../src"3import deepFreeze from "deep-freeze"4import cloneDeep from "lodash.clonedeep"56jest.setTimeout(1000)78runBaseTest("proxy (no freeze)", true, false)
566 unmodified lines
575 expect(next.paw.honey).toBe(false)576 expect(next).not.toBe(bear)577 })578579 it("should not try to change immutable data, see #66", () => {580 const user = require("./test-data")581582 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 })590591 it("should not try to change immutable data, see #66 - 2", () => {592 const user = deepFreeze(cloneDeep(require("./test-data")))593594 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 })602603 afterEach(() => {604 expect(baseState).toBe(origBaseState)605 expect(baseState).toEqual(createBaseState())606 })
21 unmodified lines
added:__tests__/test-data.json+56 added
modified:src/common.js+26 added
modified:src/es5.js+9 added-12 removed
1"use strict"2// @ts-check34import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"4import {5 autoFreeze,6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,10 finalizeNonProxiedObject11} from "./common"1213const descriptors = {}14let states = null15
133 unmodified lines
149 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 lines
modified:src/proxy.js+9 added-13 removed
1"use strict"2// @ts-check34import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"4import {5 autoFreeze,6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,10 finalizeNonProxiedObject11} from "./common"1213let revocableProxies = null1415const objectTraps = {
121 unmodified lines
137 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 lines
modified:changelog.md+4 added
modified: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