es5.js
This file
Path
src/es5.js
252 lines · 8.3 KiB · JavaScript
Last changed by
nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
History of this fileTake it
transpile es5 codeMichel Weststratebefore adoptionf012154
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"transpile es5 codeMichel Weststratebefore adoptionf012154
line 14why, one step back14Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 15–16why, one step back15const descriptors = {}16let states = nulltranspile es5 codeMichel Weststratebefore adoptionf012154
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: falsetranspile es5 codeMichel Weststratebefore adoptionf012154
line 30why, one step back30 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 31why, one step back31}transpile es5 codeMichel Weststratebefore adoptionf012154
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))transpile es5 codeMichel Weststratebefore adoptionf012154
line 45why, one step back45 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 46–47why, one step back46 return value47}transpile es5 codeMichel Weststratebefore adoptionf012154
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}transpile es5 codeMichel Weststratebefore adoptionf012154
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}transpile es5 codeMichel Weststratebefore adoptionf012154
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}transpile es5 codeMichel Weststratebefore adoptionf012154
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}transpile es5 codeMichel Weststratebefore adoptionf012154
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)transpile es5 codeMichel Weststratebefore adoptionf012154
line 96why, one step back96 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 97–99why, one step back97 })98 )99}transpile es5 codeMichel Weststratebefore adoptionf012154
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)transpile es5 codeMichel Weststratebefore adoptionf012154
line 106why, one step back106 )Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 107why, one step back107}transpile es5 codeMichel Weststratebefore adoptionf012154
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)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 122–123why, one step back122 }123 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 124why, one step back124}transpile es5 codeMichel Weststratebefore adoptionf012154
line 125why, one step back125Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 126–169why, one step back126function markChangesRecursively(object) {127 if (!object || typeof object !== "object") return128 const state = object[PROXY_STATE]129 if (!state) return130 const {proxy, base} = state131 if (Array.isArray(object)) {132 if (hasArrayChanges(state)) {133 markChanged(state)134 state.assigned.length = true135 if (proxy.length < base.length)136 for (let i = proxy.length; i < base.length; i++)137 state.assigned[i] = false138 else139 for (let i = base.length; i < proxy.length; i++)140 state.assigned[i] = true141 each(proxy, (index, child) => {142 if (!state.assigned[index]) markChangesRecursively(child)143 })144 }145 } else {146 const {added, removed} = diffKeys(base, proxy)147 if (added.length > 0 || removed.length > 0) markChanged(state)148 each(added, (_, key) => {149 state.assigned[key] = true150 })151 each(removed, (_, key) => {152 state.assigned[key] = false153 })154 each(proxy, (key, child) => {155 if (!state.assigned[key]) markChangesRecursively(child)156 })157 }158}159160function diffKeys(from, to) {161 // TODO: optimize162 const a = Object.keys(from)163 const b = Object.keys(to)164 return {165 added: b.filter(key => a.indexOf(key) === -1),166 removed: a.filter(key => b.indexOf(key) === -1)167 }168}169Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 170why, one step back170function hasObjectChanges(state) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 171–175why, one step back171 const {base, proxy} = state172173 // Search for added keys. Start at the back, because non-numeric keys174 // are ordered by time of definition on the object.175 const keys = Object.keys(proxy)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 176why, one step back176 for (let i = keys.length - 1; i >= 0; i--) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 177why, one step back177 // The `undefined` check is a fast path for pre-existing keys.nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 178why, one step back178 if (base[keys[i]] === undefined && !has(base, keys[i])) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 179–185why, one step back179 return true180 }181 }182183 // Since no keys have been added, we can compare lengths to know if an184 // object has been deleted.185 return keys.length !== Object.keys(base).lengthMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 186why, one step back186}transpile es5 codeMichel Weststratebefore adoptionf012154
line 187why, one step back187Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 188why, one step back188function hasArrayChanges(state) {Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 189–195why, one step back189 const {proxy} = state190 if (proxy.length !== state.base.length) return true191 // See #116192 // If we first shorten the length, our array interceptors will be removed.193 // If after that new items are added, result in the same original length,194 // those last items will have no intercepting property.195 // 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 196–197why, one step back196 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check197 // the last oneFixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
line 198why, one step back198 const descriptor = Object.getOwnPropertyDescriptor(proxy, proxy.length - 1)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 199why, one step back199 // 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 200–202why, one step back200 if (descriptor && !descriptor.get) return true201 // For all other cases, we don't have to compare, as they would have been picked up by the index setters202 return falsetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 203–204why, one step back203}204Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 205why, one step back205export function produceEs5(baseState, producer, patchListener) {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 206–207why, one step back206 const prevStates = states207 states = []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 208–209why, one step back208 const patches = patchListener && []209 const inversePatches = patchListener && []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 210–211why, one step back210 try {211 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 212why, one step back212 const rootProxy = createProxy(undefined, baseState)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 213why, one step back213 // execute the thunkImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 214why, one step back214 const returnValue = producer.call(rootProxy, rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 215why, one step back215 // and finalize the modified proxyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 216–218why, one step back216 each(states, (_, state) => {217 state.finalizing = true218 })Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 219why, one step back219 let resultImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 220–224why, one step back220 // check whether the draft was modified and/or a value was returned221 if (returnValue !== undefined && returnValue !== rootProxy) {222 // something was returned, and it wasn't the proxy itself223 if (rootProxy[PROXY_STATE].modified)224 throw new Error(RETURNED_AND_MODIFIED_ERROR)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 225why, one step back225 result = finalize(returnValue)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 226–234why, one step back226 if (patches) {227 patches.push({op: "replace", path: [], value: result})228 inversePatches.push({op: "replace", path: [], value: baseState})229 }230 } else {231 if (patchListener) markChangesRecursively(rootProxy)232 markChangesSweep() // this one is more efficient if we don't need to know which attributes have changed233 result = finalize(rootProxy, [], patches, inversePatches)234 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 235why, one step back235 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 236–238why, one step back236 each(states, (_, state) => {237 state.finished = true238 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 239why, one step back239 patchListener && patchListener(patches, inversePatches)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 240why, one step back240 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 241–242why, one step back241 } finally {242 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 243–245why, one step back243 }244}245Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 246–251why, one step back246function createHiddenProperty(target, prop, value) {247 Object.defineProperty(target, prop, {248 value: value,249 enumerable: false,250 writable: true251 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 252why, one step back252}