Skip to content
Myr

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

9 files+129 added−181 removed

modified:__performance_tests__/todo.js+14 added-12 removed
modified:__tests__/base.js+12 added-11 removed
1"use strict"2import * as immerProxy from "../src/immer"3import * as immerEs5 from "../src/es5"2import produce, {setAutoFreeze, setUseProxies} from "../src"3import deepFreeze from "deep-freeze"45jest.setTimeout(1000)68runBaseTest("proxy (no freeze)", immerProxy, false)9runBaseTest("proxy (autofreeze)", immerProxy, true)10runBaseTest("es5 (no freeze)", immerEs5, false)11runBaseTest("es5 (autofreeze)", immerEs5, true)7runBaseTest("proxy (no freeze)", true, false)8runBaseTest("proxy (autofreeze)", true, true)9runBaseTest("es5 (no freeze)", false, false)10runBaseTest("es5 (autofreeze)", false, true)1113function runBaseTest(name, lib, freeze) {12function runBaseTest(name, useProxies, freeze) {13 describe(`base functionality - ${name}`, () => {15 const produce = lib.default14 let baseState15 let origBaseState1617 beforeEach(() => {20 lib.setAutoFreeze(freeze)18 setAutoFreeze(freeze)19 setUseProxies(useProxies)20 origBaseState = baseState = createBaseState()21 })2223 it("should return the original without modifications", () => {
250 unmodified lines
274 expect(nextState.aProp).toBe(undefined)275 })276277 // 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 = s
132 unmodified lines
415 ).toEqual(["0", "1", "length"])416 })417 })418420 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 line
428 })429 })430 }).toThrowError(/not support/)431 })432 }433434 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 lines
modified:__tests__/curry.js+5 added-6 removed
1"use strict"2import * as immerProxy from "../src/immer"3import * as immerEs5 from "../src/es5"2import produce, {setUseProxies} from "../src/"35runTests("proxy", immerProxy)6runTests("es5", immerEs5)4runTests("proxy", true)5runTests("es5", false)68function runTests(name, lib) {7function runTests(name, useProxies) {8 describe("curry - " + name, () => {10 const produce = lib.default9 setUseProxies(useProxies)1011 it("should check arguments", () => {12 expect(() => produce()).toThrow(/produce expects 1 or 2 arguments/)13 expect(() => produce(() => {}, {})).toThrow(
26 unmodified lines
modified:__tests__/frozen.js+6 added-4 removed
1"use strict"2import produce, {setUseProxies} from "../src/"34import * as immerProxy from "../src/immer"5import * as immerEs5 from "../src/es5"6import deepFreeze from "deep-freeze"76runTests("proxy", immerProxy)7runTests("es5", immerEs5)8runTests("proxy", true)9runTests("es5", false)109function runTests(name, lib) {11function 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 lines
modified:__tests__/readme.js+1 added-1 removed
1"use strict"2import produce from "../src/immer"2import produce from "../src/"34describe("readme example", () => {5 it("works", () => {6 const baseState = [
30 unmodified lines
added:src/common.js+33 added
modified:src/es5.js+9 added-72 removed
1"use strict"2// @ts-check34const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version5const descriptors = {}4import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"57let autoFreeze = true6const descriptors = {}7let states = null89function createState(parent, proxy, base) {10 return {
164 unmodified lines
175 }176 return freeze(res)177}178180/**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 */190export default function produce(baseState, producer) {179export 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 }204205 // 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 }211180 const prevStates = states181 states = []182 try {183 // create proxy for root
17 unmodified lines
201 states = prevStates202 }203}204237function isProxy(value) {238 return !!value && !!value[PROXY_STATE]239}240241function 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}248249function freeze(value) {250 if (autoFreeze) {251 Object.freeze(value)252 }253 return value254}255256function createHiddenProperty(target, prop, value) {257 Object.defineProperty(target, prop, {258 value: value,259 enumerable: false,260 writable: true261 })262}263205function 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 lines
226 }227 return true228}229289/**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 */297export function setAutoFreeze(enableAutoFreeze) {298 autoFreeze = enableAutoFreeze230function 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-check34if (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 )4import {autoFreeze, isProxyable, isProxy, freeze, PROXY_STATE} from "./common"59const PROXY_STATE = Symbol("immer-proxy-state")10let autoFreeze = true6let revocableProxies = null78const objectTraps = {9 get,
154 unmodified lines
164 }165 return freeze(copy)166}167173/**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 */183export default function produce(baseState, producer) {168export 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 }197198 // 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 }204169 const previousProxies = revocableProxies170 revocableProxies = []171 try {172 // create proxy for root
13 unmodified lines
186 } finally {187 revocableProxies = previousProxies188 }189}226227function isProxy(value) {228 return !!value && !!value[PROXY_STATE]229}230231function 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}238239function 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}249250/**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 */258export function setAutoFreeze(enableAutoFreeze) {259 autoFreeze = enableAutoFreeze260}
modified:src/index.js+47 added-2 removed
1module.exports =2 typeof Proxy === "undefined" ? require("./es5") : require("./immer")1export {setAutoFreeze} from "./common"2import {isProxyable} from "./common"3import {produceProxy} from "./immer"4import {produceEs5} from "./es5"56let useProxies = typeof Proxy !== "undefined"78/**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 */18export 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 }3233 // 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 }3940 return useProxies41 ? produceProxy(baseState, producer)42 : produceEs5(baseState, producer)43}4445export function setUseProxies(value) {46 useProxies = value47}