es5.js
This file
Path
src/es5.js
216 lines · 6.7 KiB · JavaScript
Last changed by
Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
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–8why, one step back6 isProxyable,7 isProxy,8 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 9why, one step back9 shallowCopy,Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 10why, one step back10 RETURNED_AND_MODIFIED_ERROR,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–26why, one step back18function createState(parent, proxy, base) {19 return {20 modified: false,21 hasCopy: false,22 parent,23 base,24 proxy,25 copy: undefined,26 finished: false,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 27–28why, one step back27 finalizing: false,28 finalized: falsetranspile es5 codeMichel Weststratebefore adoptionf012154
line 29why, one step back29 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 30why, one step back30}transpile es5 codeMichel Weststratebefore adoptionf012154
line 31why, one step back31Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 32–38why, one step back32function source(state) {33 return state.hasCopy ? state.copy : state.base34}3536function get(state, prop) {37 assertUnfinished(state)38 const value = source(state)[prop]Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 39–41why, one step back39 if (!state.finalizing && value === state.base[prop] && isProxyable(value)) {40 // only create a proxy if the value is proxyable, and the value was in the base state41 // 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 42–43why, one step back42 prepareCopy(state)43 return (state.copy[prop] = createProxy(state, value))transpile es5 codeMichel Weststratebefore adoptionf012154
line 44why, one step back44 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 45–46why, one step back45 return value46}transpile es5 codeMichel Weststratebefore adoptionf012154
line 47why, one step back47Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 48–50why, one step back48function set(state, prop, value) {49 assertUnfinished(state)50 if (!state.modified) {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 51why, one step back51 if (is(source(state)[prop], value)) returnMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 52why, one step back52 markChanged(state)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 53why, one step back53 prepareCopy(state)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 54–56why, one step back54 }55 state.copy[prop] = value56}transpile es5 codeMichel Weststratebefore adoptionf012154
line 57why, one step back57Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 58–63why, one step back58function markChanged(state) {59 if (!state.modified) {60 state.modified = true61 if (state.parent) markChanged(state.parent)62 }63}transpile es5 codeMichel Weststratebefore adoptionf012154
line 64why, one step back64Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 65–67why, one step back65function prepareCopy(state) {66 if (state.hasCopy) return67 state.hasCopy = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 68why, one step back68 state.copy = shallowCopy(state.base)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 69why, one step back69}transpile es5 codeMichel Weststratebefore adoptionf012154
line 70why, one step back70Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 71–72why, one step back71// creates a proxy for plain objects / arrays72function createProxy(parent, base) {Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 73–76why, one step back73 const proxy = shallowCopy(base)74 each(base, i => {75 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))76 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 77–81why, one step back77 const state = createState(parent, proxy, base)78 createHiddenProperty(proxy, PROXY_STATE, state)79 states.push(state)80 return proxy81}transpile es5 codeMichel Weststratebefore adoptionf012154
line 82why, one step back82Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 83–93why, one step back83function createPropertyProxy(prop) {84 return (85 descriptors[prop] ||86 (descriptors[prop] = {87 configurable: true,88 enumerable: true,89 get() {90 return get(this[PROXY_STATE], prop)91 },92 set(value) {93 set(this[PROXY_STATE], prop, value)transpile es5 codeMichel Weststratebefore adoptionf012154
line 94why, one step back94 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 95–97why, one step back95 })96 )97}transpile es5 codeMichel Weststratebefore adoptionf012154
line 98why, one step back98Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 99–101why, one step back99function assertUnfinished(state) {100 if (state.finished === true)101 throw new Error(Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 102–103why, one step back102 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +103 JSON.stringify(state.copy || state.base)transpile es5 codeMichel Weststratebefore adoptionf012154
line 104why, one step back104 )Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 105why, one step back105}transpile es5 codeMichel Weststratebefore adoptionf012154
line 106why, one step back106Merge pull request #135 from btnwtn/patch-1benbraoubefore adoption6e60eb5
line 107why, one step back107// this sounds very expensive, but actually it is not that expensive in practiceMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 108–119why, one step back108// as it will only visit proxies, and only do key-based change detection for objects for109// which it is not already know that they are changed (that is, only object for which no known key was changed)110function markChanges() {111 // intentionally we process the proxies in reverse order;112 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore113 // reverse order of proxy creation approximates this114 for (let i = states.length - 1; i >= 0; i--) {115 const state = states[i]116 if (state.modified === false) {117 if (Array.isArray(state.base)) {118 if (hasArrayChanges(state)) markChanged(state)119 } else if (hasObjectChanges(state)) markChanged(state)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 120–121why, one step back120 }121 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 122why, one step back122}transpile es5 codeMichel Weststratebefore adoptionf012154
line 123why, one step back123Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 124–128why, one step back124function hasObjectChanges(state) {125 const baseKeys = Object.keys(state.base)126 const keys = Object.keys(state.proxy)127 return !shallowEqual(baseKeys, keys)128}transpile es5 codeMichel Weststratebefore adoptionf012154
line 129why, one step back129Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 130why, one step back130function hasArrayChanges(state) {Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 131–137why, one step back131 const {proxy} = state132 if (proxy.length !== state.base.length) return true133 // See #116134 // If we first shorten the length, our array interceptors will be removed.135 // If after that new items are added, result in the same original length,136 // those last items will have no intercepting property.137 // 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 138–139why, one step back138 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check139 // the last oneFixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
line 140why, one step back140 const descriptor = Object.getOwnPropertyDescriptor(proxy, proxy.length - 1)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 141why, one step back141 // 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 142–144why, one step back142 if (descriptor && !descriptor.get) return true143 // For all other cases, we don't have to compare, as they would have been picked up by the index setters144 return falsetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 145–146why, one step back145}146Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 147why, one step back147export function produceEs5(baseState, producer) {Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 148–152why, one step back148 if (isProxy(baseState)) {149 // See #100, don't nest producers150 const returnValue = producer.call(baseState, baseState)151 return returnValue === undefined ? baseState : returnValue152 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 153–156why, one step back153 const prevStates = states154 states = []155 try {156 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 157why, one step back157 const rootProxy = createProxy(undefined, baseState)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 158why, one step back158 // execute the thunkImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 159why, one step back159 const returnValue = producer.call(rootProxy, rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 160why, one step back160 // and finalize the modified proxyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 161–163why, one step back161 each(states, (_, state) => {162 state.finalizing = true163 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 164why, one step back164 // find and mark all changes (for parts not done yet)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 165why, one step back165 // TODO: store states by depth, to be able guarantee processing leaves firstMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 166why, one step back166 markChanges()Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 167why, one step back167 let resultImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 168–172why, one step back168 // check whether the draft was modified and/or a value was returned169 if (returnValue !== undefined && returnValue !== rootProxy) {170 // something was returned, and it wasn't the proxy itself171 if (rootProxy[PROXY_STATE].modified)172 throw new Error(RETURNED_AND_MODIFIED_ERROR)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
lines 173–174why, one step back173 result = finalize(returnValue)174 } else result = finalize(rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 175why, one step back175 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 176–178why, one step back176 each(states, (_, state) => {177 state.finished = true178 })Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 179why, one step back179 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 180–181why, one step back180 } finally {181 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 182–186why, one step back182 }183}184185function shallowEqual(objA, objB) {186 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.jsAdded polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 187why, one step back187 if (is(objA, objB)) return truetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 188–201why, one step back188 if (189 typeof objA !== "object" ||190 objA === null ||191 typeof objB !== "object" ||192 objB === null193 ) {194 return false195 }196 const keysA = Object.keys(objA)197 const keysB = Object.keys(objB)198 if (keysA.length !== keysB.length) return false199 for (let i = 0; i < keysA.length; i++) {200 if (201 !hasOwnProperty.call(objB, keysA[i]) ||Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 202why, one step back202 !is(objA[keysA[i]], objB[keysA[i]])transpile es5 codeMichel Weststratebefore adoptionf012154
lines 203–209why, one step back203 ) {204 return false205 }206 }207 return true208}209Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 210–215why, one step back210function createHiddenProperty(target, prop, value) {211 Object.defineProperty(target, prop, {212 value: value,213 enumerable: false,214 writable: true215 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 216why, one step back216}