One module to rule them all WIP
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 9b3bceb · Browse files · 9 files·+129 added−181 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+129 added−181 removed
- tests:+24 added−22 removed
- other:+105 added−159 removed
Provenance
- Commit
b7647e4- Tree
641f3f6- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
9b3bceb
9 files changed+129 added−181 removed
modified:__performance_tests__/todo.js+14 added-12 removed
123234567133 unmodified linesExpand all1411421431441451451461461471481491501511511521531541551551561561571581591601611621631641651641651661671681691701711701721731741751741751761771781791801812 unmodified lines"use strict"import produceProxy, {setAutoFreeze as setAutoFreezeProxy} from "../proxy"import produceEs5, {setAutoFreeze as setAutoFreezeEs5} from "../es5"// TODO: import from rolled up source:import produce, {setAutoFreeze, setUseProxies} from "../dist"import cloneDeep from "lodash.clonedeep"import {List, Record} from "immutable"import deepFreeze from "deep-freeze"
133 unmodified linesExpand all .toJS() })
measure("immer (proxy) - without autofreeze", () => { setUseProxies(true) setAutoFreezeProxy(false) produceProxy(baseState, draft => { setAutoFreeze(false) produce(baseState, draft => { for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { draft[i].done = true } }) setAutoFreezeProxy(true) })
measure("immer (proxy) - with autofreeze", () => { setUseProxies(true) setAutoFreezeProxy(true) produceProxy(frozenBazeState, draft => { setAutoFreeze(true) produce(frozenBazeState, draft => { for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { draft[i].done = true } }) })
measure("immer (es5) - without autofreeze", () => { setUseProxies(false) setAutoFreezeEs5(false) produceEs5(baseState, draft => { setAutoFreeze(false) produce(baseState, draft => { for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { draft[i].done = true } }) setAutoFreezeEs5(true) })
measure("immer (es5) - with autofreeze", () => { setUseProxies(false) setAutoFreezeEs5(true) produceEs5(frozenBazeState, draft => { setAutoFreeze(true) produce(frozenBazeState, draft => { for (let i = 0; i < MAX * MODIFY_FACTOR; i++) { draft[i].done = true } })2 unmodified linesmodified:__tests__/base.js+12 added-11 removed
1 "use strict"2-import * as immerProxy from "../src/immer"3-import * as immerEs5 from "../src/es5"2+import produce, {setAutoFreeze, setUseProxies} from "../src"3 import deepFreeze from "deep-freeze"4 5 jest.setTimeout(1000)6 8-runBaseTest("proxy (no freeze)", immerProxy, false)9-runBaseTest("proxy (autofreeze)", immerProxy, true)10-runBaseTest("es5 (no freeze)", immerEs5, false)11-runBaseTest("es5 (autofreeze)", immerEs5, true)7+runBaseTest("proxy (no freeze)", true, false)8+runBaseTest("proxy (autofreeze)", true, true)9+runBaseTest("es5 (no freeze)", false, false)10+runBaseTest("es5 (autofreeze)", false, true)11 13-function runBaseTest(name, lib, freeze) {12+function runBaseTest(name, useProxies, freeze) {13 describe(`base functionality - ${name}`, () => {15- const produce = lib.default14 let baseState15 let origBaseState16 17 beforeEach(() => {20- lib.setAutoFreeze(freeze)18+ setAutoFreeze(freeze)19+ setUseProxies(useProxies)20 origBaseState = baseState = createBaseState()21 })22 23 it("should return the original without modifications", () => {250 unmodified lines274 expect(nextState.aProp).toBe(undefined)275 })276 277 // ES implementation does't protect against all outside modifications, just some..279- if (lib === immerProxy) {278+ if (useProxies) {279 it("should revoke the proxy of the baseState after immer function is executed", () => {280 let proxy281 const nextState = produce(baseState, s => {282 proxy = s132 unmodified lines415 ).toEqual(["0", "1", "length"])416 })417 })418 420- if (lib === immerProxy)419+ if (useProxies === true) {420 it("should not be possible to set property descriptors", () => {421 expect(() => {422+ debugger423 produce({}, draft => {424 Object.defineProperty(draft, "xx", {425 enumerable: true,426 writeable: true,1 unmodified line428 })429 })430 }).toThrowError(/not support/)431 })432+ }433 434 it("should not throw error, see #53 - 1", () => {435 const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}436 const result = produce(base, draft => {168 unmodified linesmodified:__tests__/curry.js+5 added-6 removed
1 "use strict"2-import * as immerProxy from "../src/immer"3-import * as immerEs5 from "../src/es5"2+import produce, {setUseProxies} from "../src/"3 5-runTests("proxy", immerProxy)6-runTests("es5", immerEs5)4+runTests("proxy", true)5+runTests("es5", false)6 8-function runTests(name, lib) {7+function runTests(name, useProxies) {8 describe("curry - " + name, () => {10- const produce = lib.default9+ setUseProxies(useProxies)10 11 it("should check arguments", () => {12 expect(() => produce()).toThrow(/produce expects 1 or 2 arguments/)13 expect(() => produce(() => {}, {})).toThrow(26 unmodified linesmodified:__tests__/frozen.js+6 added-4 removed
1 "use strict"2+import produce, {setUseProxies} from "../src/"3+4 import * as immerProxy from "../src/immer"5 import * as immerEs5 from "../src/es5"6 import deepFreeze from "deep-freeze"7 6-runTests("proxy", immerProxy)7-runTests("es5", immerEs5)8+runTests("proxy", true)9+runTests("es5", false)10 9-function runTests(name, lib) {11+function runTests(name, useProxies) {12 describe("auto freeze - " + name, () => {11- const produce = lib.default13+ setUseProxies(useProxies)14 const baseState = {15 object: {a: 1},16 array: [1, 2]17 }42 unmodified linesmodified:__tests__/readme.js+1 added-1 removed
1 "use strict"2-import produce from "../src/immer"2+import produce from "../src/"3 4 describe("readme example", () => {5 it("works", () => {6 const baseState = [30 unmodified linesadded:src/common.js+33 added
123456789101112131415161718192021222324252627282930313233export const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer versionexport let autoFreeze = true
export function isProxy(value) { return !!value && !!value[PROXY_STATE]}
export function isProxyable(value) { if (!value) return false if (typeof value !== "object") return false if (Array.isArray(value)) return true const proto = Object.getPrototypeOf(value) return proto === null || proto === Object.prototype}
export function freeze(value) { if (autoFreeze) { Object.freeze(value) } return value}
/** * Automatically freezes any state trees generated by immer. * This protects against accidental modifications of the state tree outside of an immer function. * This comes with a performance impact, so it is recommended to disable this option in production. * It is by default enabled. * * @returns {void} */export function setAutoFreeze(enableAutoFreeze) { autoFreeze = enableAutoFreeze}modified:src/es5.js+9 added-72 removed
1 "use strict"2 // @ts-check3 4-const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version5-const descriptors = {}4+import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"5 7-let autoFreeze = true6+const descriptors = {}7 let states = null8 9 function createState(parent, proxy, base) {10 return {164 unmodified lines175 }176 return freeze(res)177 }178 180-/**181- * Immer takes a state, and runs a function against it.182- * That function can freely mutate the state, as it will create copies-on-write.183- * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned184- *185- * @export186- * @param {any} baseState - the state to start with187- * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified188- * @returns {any} a new state, or the base state if nothing was modified189- */190-export default function produce(baseState, producer) {179+export function produceEs5(baseState, producer) {191- // curried invocation192- if (arguments.length === 1) {193- const producer = baseState194- // prettier-ignore195- if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")196- return function() {197- const args = arguments198- return produce(args[0], draft => {199- args[0] = draft // blegh!200- baseState.apply(null, args)201- })202- }203- }204-205- // prettier-ignore206- {207- if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)208- if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))209- if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")210- }211-180 const prevStates = states181 states = []182 try {183 // create proxy for root17 unmodified lines201 states = prevStates202 }203 }204 237-function isProxy(value) {238- return !!value && !!value[PROXY_STATE]239-}240-241-function isProxyable(value) {242- if (!value) return false243- if (typeof value !== "object") return false244- if (Array.isArray(value)) return true245- const proto = Object.getPrototypeOf(value)246- return proto === null || proto === Object.prototype247-}248-249-function freeze(value) {250- if (autoFreeze) {251- Object.freeze(value)252- }253- return value254-}255-256-function createHiddenProperty(target, prop, value) {257- Object.defineProperty(target, prop, {258- value: value,259- enumerable: false,260- writable: true261- })262-}263-205 function shallowEqual(objA, objB) {206 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js207 if (Object.is(objA, objB)) return true208 if (17 unmodified lines226 }227 return true228 }229 289-/**290- * Automatically freezes any state trees generated by immer.291- * This protects against accidental modifications of the state tree outside of an immer function.292- * This comes with a performance impact, so it is recommended to disable this option in production.293- * It is by default enabled.294- *295- * @returns {void}296- */297-export function setAutoFreeze(enableAutoFreeze) {298- autoFreeze = enableAutoFreeze230+function createHiddenProperty(target, prop, value) {231+ Object.defineProperty(target, prop, {232+ value: value,233+ enumerable: false,234+ writable: true235+ })236 }modified:src/immer.js+2 added-73 removed
1 "use strict"2 // @ts-check3 4-if (typeof Proxy === "undefined")5- throw new Error(6- "Immer requires `Proxy` to be available, but it seems to be not available on your platform. Consider requiring immer '\"immer/es5\"' instead."7- )4+import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"5 9-const PROXY_STATE = Symbol("immer-proxy-state")10-let autoFreeze = true6 let revocableProxies = null7 8 const objectTraps = {9 get,154 unmodified lines164 }165 return freeze(copy)166 }167 173-/**174- * produce takes a state, and runs a function against it.175- * That function can freely mutate the state, as it will create copies-on-write.176- * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned177- *178- * @export179- * @param {any} baseState - the state to start with180- * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified181- * @returns {any} a new state, or the base state if nothing was modified182- */183-export default function produce(baseState, producer) {168+export function produceProxy(baseState, producer) {184- // curried invocation185- if (arguments.length === 1) {186- const producer = baseState187- // prettier-ignore188- if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")189- return function() {190- const args = arguments191- return produce(args[0], draft => {192- args[0] = draft // blegh!193- baseState.apply(null, args)194- })195- }196- }197-198- // prettier-ignore199- {200- if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)201- if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))202- if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")203- }204-169 const previousProxies = revocableProxies170 revocableProxies = []171 try {172 // create proxy for root13 unmodified lines186 } finally {187 revocableProxies = previousProxies188 }189 }226-227-function isProxy(value) {228- return !!value && !!value[PROXY_STATE]229-}230-231-function isProxyable(value) {232- if (!value) return false233- if (typeof value !== "object") return false234- if (Array.isArray(value)) return true235- const proto = Object.getPrototypeOf(value)236- return proto === null || proto === Object.prototype237-}238-239-function freeze(value) {240- // short circuit to achieve 100% code coverage instead of 98%241- /*242- if(autoFreeze) {243- Object.freeze(value)244- }245- * */246- autoFreeze && Object.freeze(value)247- return value248-}249-250-/**251- * Automatically freezes any state trees generated by immer.252- * This protects against accidental modifications of the state tree outside of an immer function.253- * This comes with a performance impact, so it is recommended to disable this option in production.254- * It is by default enabled.255- *256- * @returns {void}257- */258-export function setAutoFreeze(enableAutoFreeze) {259- autoFreeze = enableAutoFreeze260-}modified:src/index.js+47 added-2 removed
1-module.exports =2- typeof Proxy === "undefined" ? require("./es5") : require("./immer")1+export {setAutoFreeze} from "./common"2+import {isProxyable} from "./common"3+import {produceProxy} from "./immer"4+import {produceEs5} from "./es5"5+6+let useProxies = typeof Proxy !== "undefined"7+8+/**9+ * produce takes a state, and runs a function against it.10+ * That function can freely mutate the state, as it will create copies-on-write.11+ * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned12+ *13+ * @export14+ * @param {any} baseState - the state to start with15+ * @param {Function} producer - function that receives a proxy of the base state as first argument and which can be freely modified16+ * @returns {any} a new state, or the base state if nothing was modified17+ */18+export default function produce(baseState, producer) {19+ // curried invocation20+ if (arguments.length === 1) {21+ const producer = baseState22+ // prettier-ignore23+ if (typeof producer !== "function") throw new Error("if produce is called with 1 argument, the first argument should be a function")24+ return function() {25+ const args = arguments26+ return produce(args[0], draft => {27+ args[0] = draft // blegh!28+ baseState.apply(null, args)29+ })30+ }31+ }32+33+ // prettier-ignore34+ {35+ if (arguments.length !== 2) throw new Error("produce expects 1 or 2 arguments, got " + arguments.length)36+ if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))37+ if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")38+ }39+40+ return useProxies41+ ? produceProxy(baseState, producer)42+ : produceEs5(baseState, producer)43+}44+45+export function setUseProxies(value) {46+ useProxies = value47+}