es5.js
This file
Path
src/es5.js
248 lines · 8.4 KiB · JavaScript
Last changed by
perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
History of this fileTake it
The walk stopped after 200 commits: lines older than 804703b point at it.
Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
lines 1–3why, one step back1"use strict"2// @ts-check3Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 4why, one step back4import {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 5why, one step back5 is,Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
lines 6–7why, one step back6 isProxyable,7 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 8why, one step back8 shallowCopy,Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 9why, one step back9 RETURNED_AND_MODIFIED_ERROR,Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 10why, one step back10 has,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 11–12why, one step back11 each,12 finalizeFixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 13why, one step back13} from "./common"Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 14why, one step back14Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 15–16why, one step back15const descriptors = {}16let states = nullMerge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 17why, one step back17Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 18–20why, one step back18function createState(parent, proxy, base) {19 return {20 modified: false,Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 21why, one step back21 assigned: {}, // true: value was assigned to these props, false: was removedMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 22–27why, one step back22 hasCopy: false,23 parent,24 base,25 proxy,26 copy: undefined,27 finished: false,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 28–29why, one step back28 finalizing: false,29 finalized: falseMerge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 30why, one step back30 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 31why, one step back31}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 32why, one step back32Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 33–39why, one step back33function source(state) {34 return state.hasCopy ? state.copy : state.base35}3637function get(state, prop) {38 assertUnfinished(state)39 const value = source(state)[prop]Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 40–42why, one step back40 if (!state.finalizing && value === state.base[prop] && isProxyable(value)) {41 // only create a proxy if the value is proxyable, and the value was in the base state42 // if it wasn't in the base state, the object is already modified and we will process it in finalizeMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 43–44why, one step back43 prepareCopy(state)44 return (state.copy[prop] = createProxy(state, value))Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 45why, one step back45 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 46–47why, one step back46 return value47}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 48why, one step back48Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 49–50why, one step back49function set(state, prop, value) {50 assertUnfinished(state)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 51why, one step back51 state.assigned[prop] = true // optimization; skip this if there is no listenerMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 52why, one step back52 if (!state.modified) {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 53why, one step back53 if (is(source(state)[prop], value)) returnMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 54why, one step back54 markChanged(state)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 55why, one step back55 prepareCopy(state)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 56–58why, one step back56 }57 state.copy[prop] = value58}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 59why, one step back59Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 60–65why, one step back60function markChanged(state) {61 if (!state.modified) {62 state.modified = true63 if (state.parent) markChanged(state.parent)64 }65}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 66why, one step back66Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 67–69why, one step back67function prepareCopy(state) {68 if (state.hasCopy) return69 state.hasCopy = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 70why, one step back70 state.copy = shallowCopy(state.base)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 71why, one step back71}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 72why, one step back72Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 73–74why, one step back73// creates a proxy for plain objects / arrays74function createProxy(parent, base) {Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 75–78why, one step back75 const proxy = shallowCopy(base)76 each(base, i => {77 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))78 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 79–83why, one step back79 const state = createState(parent, proxy, base)80 createHiddenProperty(proxy, PROXY_STATE, state)81 states.push(state)82 return proxy83}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 84why, one step back84Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 85–95why, one step back85function createPropertyProxy(prop) {86 return (87 descriptors[prop] ||88 (descriptors[prop] = {89 configurable: true,90 enumerable: true,91 get() {92 return get(this[PROXY_STATE], prop)93 },94 set(value) {95 set(this[PROXY_STATE], prop, value)Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 96why, one step back96 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 97–99why, one step back97 })98 )99}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 100why, one step back100Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 101–103why, one step back101function assertUnfinished(state) {102 if (state.finished === true)103 throw new Error(Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 104–105why, one step back104 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +105 JSON.stringify(state.copy || state.base)Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 106why, one step back106 )Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 107why, one step back107}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 108why, one step back108Merge pull request #135 from btnwtn/patch-1benbraoubefore adoption6e60eb5
line 109why, one step back109// this sounds very expensive, but actually it is not that expensive in practiceMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 110–111why, one step back110// as it will only visit proxies, and only do key-based change detection for objects for111// which it is not already know that they are changed (that is, only object for which no known key was changed)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 112why, one step back112function markChangesSweep() {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 113–121why, one step back113 // intentionally we process the proxies in reverse order;114 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore115 // reverse order of proxy creation approximates this116 for (let i = states.length - 1; i >= 0; i--) {117 const state = states[i]118 if (state.modified === false) {119 if (Array.isArray(state.base)) {120 if (hasArrayChanges(state)) markChanged(state)121 } else if (hasObjectChanges(state)) markChanged(state)Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
lines 122–123why, one step back122 }123 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 124why, one step back124}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 125why, one step back125Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 126–129why, one step back126function markChangesRecursively(object) {127 if (!object || typeof object !== "object") return128 const state = object[PROXY_STATE]129 if (!state) returnperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 130–141why, one step back130 const {proxy, base, assigned} = state131 if (!Array.isArray(object)) {132 // Look for added keys.133 Object.keys(proxy).forEach(key => {134 // The `undefined` check is a fast path for pre-existing keys.135 if (base[key] === undefined && !has(base, key)) {136 assigned[key] = true137 markChanged(state)138 } else if (!assigned[key]) {139 // Only untouched properties trigger recursion.140 markChangesRecursively(proxy[key])141 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 142why, one step back142 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 143–149why, one step back143 // Look for removed keys.144 Object.keys(base).forEach(key => {145 // The `undefined` check is a fast path for pre-existing keys.146 if (proxy[key] === undefined && !has(proxy, key)) {147 assigned[key] = false148 markChanged(state)149 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 150why, one step back150 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 151–162why, one step back151 } else if (hasArrayChanges(state)) {152 markChanged(state)153 assigned.length = true154 if (proxy.length < base.length) {155 for (let i = proxy.length; i < base.length; i++) assigned[i] = false156 } else {157 for (let i = base.length; i < proxy.length; i++) assigned[i] = true158 }159 for (let i = 0; i < proxy.length; i++) {160 // Only untouched indices trigger recursion.161 if (assigned[i] === undefined) markChangesRecursively(proxy[i])162 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 163–165why, one step back163 }164}165Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 166why, one step back166function hasObjectChanges(state) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 167–171why, one step back167 const {base, proxy} = state168169 // Search for added keys. Start at the back, because non-numeric keys170 // are ordered by time of definition on the object.171 const keys = Object.keys(proxy)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 172why, one step back172 for (let i = keys.length - 1; i >= 0; i--) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 173why, one step back173 // The `undefined` check is a fast path for pre-existing keys.nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 174why, one step back174 if (base[keys[i]] === undefined && !has(base, keys[i])) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 175–181why, one step back175 return true176 }177 }178179 // Since no keys have been added, we can compare lengths to know if an180 // object has been deleted.181 return keys.length !== Object.keys(base).lengthMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 182why, one step back182}Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 183why, one step back183Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 184why, one step back184function hasArrayChanges(state) {Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 185–191why, one step back185 const {proxy} = state186 if (proxy.length !== state.base.length) return true187 // See #116188 // If we first shorten the length, our array interceptors will be removed.189 // If after that new items are added, result in the same original length,190 // those last items will have no intercepting property.191 // So if there is no own descriptor on the last position, we know that items were removed and addedFixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 192–193why, one step back192 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check193 // the last oneFixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
line 194why, one step back194 const descriptor = Object.getOwnPropertyDescriptor(proxy, proxy.length - 1)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 195why, one step back195 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 196–198why, one step back196 if (descriptor && !descriptor.get) return true197 // For all other cases, we don't have to compare, as they would have been picked up by the index setters198 return falseMerge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
lines 199–200why, one step back199}200Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 201why, one step back201export function produceEs5(baseState, producer, patchListener) {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 202–203why, one step back202 const prevStates = states203 states = []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 204–205why, one step back204 const patches = patchListener && []205 const inversePatches = patchListener && []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 206–207why, one step back206 try {207 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 208why, one step back208 const rootProxy = createProxy(undefined, baseState)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 209why, one step back209 // execute the thunkImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 210why, one step back210 const returnValue = producer.call(rootProxy, rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 211why, one step back211 // and finalize the modified proxyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 212–214why, one step back212 each(states, (_, state) => {213 state.finalizing = true214 })Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 215why, one step back215 let resultImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 216–220why, one step back216 // check whether the draft was modified and/or a value was returned217 if (returnValue !== undefined && returnValue !== rootProxy) {218 // something was returned, and it wasn't the proxy itself219 if (rootProxy[PROXY_STATE].modified)220 throw new Error(RETURNED_AND_MODIFIED_ERROR)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 221why, one step back221 result = finalize(returnValue)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 222–230why, one step back222 if (patches) {223 patches.push({op: "replace", path: [], value: result})224 inversePatches.push({op: "replace", path: [], value: baseState})225 }226 } else {227 if (patchListener) markChangesRecursively(rootProxy)228 markChangesSweep() // this one is more efficient if we don't need to know which attributes have changed229 result = finalize(rootProxy, [], patches, inversePatches)230 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 231why, one step back231 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 232–234why, one step back232 each(states, (_, state) => {233 state.finished = true234 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 235why, one step back235 patchListener && patchListener(patches, inversePatches)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 236why, one step back236 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 237–238why, one step back237 } finally {238 states = prevStatesMerge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
lines 239–241why, one step back239 }240}241Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 242–247why, one step back242function createHiddenProperty(target, prop, value) {243 Object.defineProperty(target, prop, {244 value: value,245 enumerable: false,246 writable: true247 })Merge pull request #55 from typicode/patch-1Michel Weststratebefore adoption804703b
line 248why, one step back248}