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
Provenance
- Commit
c145775- Tree
52ee715- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
1917549
1 file changed+21 added−39 removed
modified:immer-es5.js+21 added-39 removed
1 import {isObject} from "util"2 ;("use strict")3 // @ts-check4 5-/**6- * @typedef {Object} RevocableProxy7- * @property {any} proxy8- * @property {Function} revoke9- */10-5 const PROXY_TARGET = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version6 const CHANGED_STATE = Symbol("immer-changed-state")7 8 let autoFreeze = true27 unmodified lines36 )37 }38 39 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 lines59 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 lines110 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))111 return proxy112 }113 109- // 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 lines138 }139 return false140 }141 139- // 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 }150 151 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 }158 159 function finalizeArray(proxy) {157- if (!hasChanges(proxy)) return proxy[PROXY_TARGET]160 return freeze(proxy.map(finalize))161 }162 163 // create proxy for root12 unmodified lines176 finished = true177 return res178 }179 178-/**179- * Revoke all the proxies stored in the revocableProxies map180- *181- * @param {Map<Object,RevocableProxy>} revocableProxies182- */183-function revoke(revocableProxies) {184- for (var revocableProxy of revocableProxies.values()) {185- revocableProxy.revoke()186- }187-}188-180 function isPlainObject(value) {181 if (value === null || typeof value !== "object") return false182 const proto = Object.getPrototypeOf(value)183 return proto === Object.prototype || proto === null18 unmodified lines202 })203 }204 205 function shallowEqual(objA, objB) {215- if (Object.is(objA, objB)) {216- return true217- }218-206+ 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 }227-215 const keysA = Object.keys(objA)216 const keysB = Object.keys(objB)230-231- if (keysA.length !== keysB.length) {217+ if (keysA.length !== keysB.length) return false232- return false233- }234-218 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 }243-226 return true227 }228 229 /**14 unmodified lines