Skip to content
Myr

common.js

This file

Path

src/common.js

168 lines · 5.0 KiB · JavaScript

Last changed by

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

History of this file

Take it

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 1–2why, one step back
1import {generatePatches} from "./patches"
2

Fixed #75, don't use Symbols if not availableMichel Weststratebefore adoption7290670

lines 3–6why, one step back
3export const PROXY_STATE =
4 typeof Symbol !== "undefined"5 ? Symbol("immer-proxy-state")6 : "__$immer_state"

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 7why, one step back
7

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 8–10why, one step back
8export const RETURNED_AND_MODIFIED_ERROR =
9 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."10

Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909

lines 11–14why, one step back
11function verifyMinified() {}
1213const inProduction =14 (typeof process !== "undefined" && process.env.NODE_ENV === "production") ||

Fixed #95: incorrect detection of minified sourcesMichel Weststratebefore adoptiona091002

line 15why, one step back
15 verifyMinified.name !== "verifyMinified"

Disable auto freeze in production, fixes #46, also see #76Michel Weststratebefore adoption71d5909

line 16why, one step back
16

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 17why, one step back
17let autoFreeze = !inProduction

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 18–39why, one step back
18let useProxies = typeof Proxy !== "undefined"
1920/**21 * Automatically freezes any state trees generated by immer.22 * This protects against accidental modifications of the state tree outside of an immer function.23 * This comes with a performance impact, so it is recommended to disable this option in production.24 * It is by default enabled.25 *26 * @returns {void}27 */28export function setAutoFreeze(enableAutoFreeze) {29 autoFreeze = enableAutoFreeze30}3132export function setUseProxies(value) {33 useProxies = value34}3536export function getUseProxies() {37 return useProxies38}39

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 40–58why, one step back
40export function isProxy(value) {
41 return !!value && !!value[PROXY_STATE]42}4344export function isProxyable(value) {45 if (!value) return false46 if (typeof value !== "object") return false47 if (Array.isArray(value)) return true48 const proto = Object.getPrototypeOf(value)49 return proto === null || proto === Object.prototype50}5152export function freeze(value) {53 if (autoFreeze) {54 Object.freeze(value)55 }56 return value57}58

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 59–69why, one step back
59const assign =
60 Object.assign ||61 function assign(target, value) {62 for (let key in value) {63 if (has(value, key)) {64 target[key] = value[key]65 }66 }67 return target68 }69

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 70why, one step back
70export function shallowCopy(value) {

Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85

line 71why, one step back
71 if (Array.isArray(value)) return value.slice()

Merge pull request #139 from celebro/fix/remove-object-assign-for-IEMichel Weststratebefore adoption427308e

line 72why, one step back
72 const target = value.__proto__ === undefined ? Object.create(null) : {}

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 73why, one step back
73 return assign(target, value)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 74why, one step back
74}

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 75why, one step back
75

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 76–83why, one step back
76export function each(value, cb) {
77 if (Array.isArray(value)) {78 for (let i = 0; i < value.length; i++) cb(i, value[i])79 } else {80 for (let key in value) cb(key, value[key])81 }82}83

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 84why, one step back
84export function has(thing, prop) {

Merge branch 'iruca3-fix/creating_array_proxy'Michel Weststratebefore adoption374ad85

line 85why, one step back
85 return Object.prototype.hasOwnProperty.call(thing, prop)

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

lines 86–87why, one step back
86}
87

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 88why, one step back
88// given a base object, returns it if unmodified, or return the changed cloned if modified

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 89why, one step back
89export function finalize(base, path, patches, inversePatches) {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 90–94why, one step back
90 if (isProxy(base)) {
91 const state = base[PROXY_STATE]92 if (state.modified === true) {93 if (state.finalized === true) return state.copy94 state.finalized = true

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 95why, one step back
95 const result = finalizeObject(

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

line 96why, one step back
96 useProxies ? state.copy : (state.copy = shallowCopy(base)),

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 97–100why, one step back
97 state,
98 path,99 patches,100 inversePatches

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

line 101why, one step back
101 )

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 102–110why, one step back
102 generatePatches(
103 state,104 path,105 patches,106 inversePatches,107 state.base,108 result109 )110 return result

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 111–113why, one step back
111 } else {
112 return state.base113 }

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 114why, one step back
114 }

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

line 115why, one step back
115 finalizeNonProxiedObject(base)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 116–118why, one step back
116 return base
117}118

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 119why, one step back
119function finalizeObject(copy, state, path, patches, inversePatches) {

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 120–121why, one step back
120 const base = state.base
121 each(copy, (prop, value) => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 122–132why, one step back
122 if (value !== base[prop]) {
123 // if there was an assignment on this property, we don't need to generate124 // patches for the subtree125 const generatePatches = patches && !has(state.assigned, prop)126 copy[prop] = finalize(127 value,128 generatePatches && path.concat(prop),129 generatePatches && patches,130 inversePatches131 )132 }

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 133–136why, one step back
133 })
134 return freeze(copy)135}136

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 137why, one step back
137function finalizeNonProxiedObject(parent) {

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

lines 138–141why, one step back
138 // 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
139 // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well140 if (!isProxyable(parent)) return141 if (Object.isFrozen(parent)) return

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 142–146why, one step back
142 each(parent, (i, child) => {
143 if (isProxy(child)) {144 parent[i] = finalize(child)145 } else finalizeNonProxiedObject(child)146 })

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 147–150why, one step back
147 // always freeze completely new data
148 freeze(parent)149}150

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 151–158why, one step back
151export function verifyReturnValue(returnedValue, proxy, isProxyModified) {
152 if (returnedValue !== undefined && returnedValue !== proxy) {153 // something was returned, and it wasn't the proxy itself154 if (isProxyModified)155 throw new Error(156 "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft."157 )158 }

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 159why, one step back
159}

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

lines 160–168why, one step back
160
161export function is(x, y) {162 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js163 if (x === y) {164 return x !== 0 || 1 / x === 1 / y165 } else {166 return x !== x && y !== y167 }168}