Skip to content
Myr

Added some comments

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 1917549 · Browse files · 1 file+21 added−39 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+21 added−39 removed

  • other:+21 added−39 removed

1 file+21 added−39 removed

modified:immer-es5.js+21 added-39 removed
1import {isObject} from "util"2;("use strict")3// @ts-check45/**6 * @typedef {Object} RevocableProxy7 * @property {any} proxy8 * @property {Function} revoke9 */105const PROXY_TARGET = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version6const CHANGED_STATE = Symbol("immer-changed-state")78let autoFreeze = true
27 unmodified lines
36 )37 }3839 function proxySet(target, prop, value) {40 // immer func not ended?41 assertUnfinished()42 // actually a change?43 if (Object.is(target[prop], value)) return44 // mark changed45 target[CHANGED_STATE] = true46 // and stop proxying, we know this object has changed47 Object.defineProperty(target, prop, {48 enumerable: true,49 writable: true,50 configurable: true,
8 unmodified lines
59 configurable: true,60 enumerable: true,61 get() {62 assertUnfinished()63 // find the target object64 const target = this[PROXY_TARGET]66 if (finalizing) return target[prop]65 // find the original value66 const value = target[prop]67 // if we are finalizing, don't bother creating proxies, just return base value68 if (finalizing) return value69 // if not proxy-able, return value70 if (!isPlainObject(value) && !Array.isArray(value))71 return value72 // otherwise, create proxy73 const proxy = createProxy(value)74 // and make sure this proxy is returned from this prop in the future if read75 // (write behavior as is)76 Object.defineProperty(this, prop, {77 configurable: true,78 enumerable: true,79 get() {
30 unmodified lines
110 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))111 return proxy112 }113109 // checks if the given base object has modifications, either because it is modified, or110 // because one of it's children is114 function hasChanges(proxy) {115 if (!isProxy(proxy)) return false116 if (proxy[CHANGED_STATE]) return true // some property was modified117 if (isPlainObject(proxy)) return objectHasChanges(proxy)
20 unmodified lines
138 }139 return false140 }141139 // given a base object, returns it if unmodified, or return the changed cloned if modified140 function finalize(thing) {141 if (!isProxy(thing)) return thing142 if (isPlainObject(thing)) return finalizeObject(thing)143 if (Array.isArray(thing)) return finalizeArray(thing)144 return thing142 function finalize(proxy) {143 // given a base object, returns it if unmodified, or return the changed cloned if modified144 if (!isProxy(proxy)) return proxy145 if (!hasChanges(proxy)) return proxy[PROXY_TARGET] // return the original target146 if (isPlainObject(proxy)) return finalizeObject(proxy)147 if (Array.isArray(proxy)) return finalizeArray(proxy)148 throw new Error("Illegal state")149 }150151 function finalizeObject(proxy) {148 if (!hasChanges(proxy)) return proxy[PROXY_TARGET] // return the original target152 const res = {}153 Object.keys(proxy).forEach(prop => {154 res[prop] = finalize(proxy[prop])155 })156 return freeze(res)157 }158159 function finalizeArray(proxy) {157 if (!hasChanges(proxy)) return proxy[PROXY_TARGET]160 return freeze(proxy.map(finalize))161 }162163 // create proxy for root
12 unmodified lines
176 finished = true177 return res178}179178/**179 * Revoke all the proxies stored in the revocableProxies map180 *181 * @param {Map<Object,RevocableProxy>} revocableProxies182 */183function revoke(revocableProxies) {184 for (var revocableProxy of revocableProxies.values()) {185 revocableProxy.revoke()186 }187}188180function isPlainObject(value) {181 if (value === null || typeof value !== "object") return false182 const proto = Object.getPrototypeOf(value)183 return proto === Object.prototype || proto === null
18 unmodified lines
202 })203}204205function shallowEqual(objA, objB) {215 if (Object.is(objA, objB)) {216 return true217 }218206 if (Object.is(objA, objB)) return true207 if (208 typeof objA !== "object" ||209 objA === null ||210 typeof objB !== "object" ||211 objB === null212 ) {213 return false214 }227215 const keysA = Object.keys(objA)216 const keysB = Object.keys(objB)230231 if (keysA.length !== keysB.length) {217 if (keysA.length !== keysB.length) return false232 return false233 }234218 for (let i = 0; i < keysA.length; i++) {219 if (220 !hasOwnProperty.call(objB, keysA[i]) ||221 !Object.is(objA[keysA[i]], objB[keysA[i]])222 ) {223 return false224 }225 }243226 return true227}228229/**
14 unmodified lines