es5.js
This file
Path
src/es5.js
270 lines · 8.7 KiB · JavaScript
Last changed by
Merge branch 'patches'Michel Weststratebefore adoption2513fb8
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–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
lines 170–174why, one step back170function hasObjectChanges(state) {171 const baseKeys = Object.keys(state.base)172 const keys = Object.keys(state.proxy)173 return !shallowEqual(baseKeys, keys)174}transpile es5 codeMichel Weststratebefore adoptionf012154
line 175why, one step back175Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 176why, one step back176function hasArrayChanges(state) {Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
lines 177–183why, one step back177 const {proxy} = state178 if (proxy.length !== state.base.length) return true179 // See #116180 // If we first shorten the length, our array interceptors will be removed.181 // If after that new items are added, result in the same original length,182 // those last items will have no intercepting property.183 // 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 184–185why, one step back184 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check185 // the last oneFixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df
line 186why, one step back186 const descriptor = Object.getOwnPropertyDescriptor(proxy, proxy.length - 1)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 187why, one step back187 // 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 188–190why, one step back188 if (descriptor && !descriptor.get) return true189 // For all other cases, we don't have to compare, as they would have been picked up by the index setters190 return falsetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 191–192why, one step back191}192Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 193why, one step back193export function produceEs5(baseState, producer, patchListener) {Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
lines 194–198why, one step back194 if (isProxy(baseState)) {195 // See #100, don't nest producers196 const returnValue = producer.call(baseState, baseState)197 return returnValue === undefined ? baseState : returnValue198 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 199–200why, one step back199 const prevStates = states200 states = []Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 201–202why, one step back201 const patches = patchListener && []202 const inversePatches = patchListener && []Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 203–204why, one step back203 try {204 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 205why, one step back205 const rootProxy = createProxy(undefined, baseState)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 206why, one step back206 // execute the thunkImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 207why, one step back207 const returnValue = producer.call(rootProxy, rootProxy)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 208why, one step back208 // and finalize the modified proxyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 209–211why, one step back209 each(states, (_, state) => {210 state.finalizing = true211 })Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 212why, one step back212 let resultImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 213–217why, one step back213 // check whether the draft was modified and/or a value was returned214 if (returnValue !== undefined && returnValue !== rootProxy) {215 // something was returned, and it wasn't the proxy itself216 if (rootProxy[PROXY_STATE].modified)217 throw new Error(RETURNED_AND_MODIFIED_ERROR)Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145
line 218why, one step back218 result = finalize(returnValue)Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 219–227why, one step back219 if (patches) {220 patches.push({op: "replace", path: [], value: result})221 inversePatches.push({op: "replace", path: [], value: baseState})222 }223 } else {224 if (patchListener) markChangesRecursively(rootProxy)225 markChangesSweep() // this one is more efficient if we don't need to know which attributes have changed226 result = finalize(rootProxy, [], patches, inversePatches)227 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 228why, one step back228 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 229–231why, one step back229 each(states, (_, state) => {230 state.finished = true231 })Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 232why, one step back232 patchListener && patchListener(patches, inversePatches)Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 233why, one step back233 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 234–235why, one step back234 } finally {235 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 236–240why, one step back236 }237}238239function shallowEqual(objA, objB) {240 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.jsAdded polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 241why, one step back241 if (is(objA, objB)) return truetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 242–255why, one step back242 if (243 typeof objA !== "object" ||244 objA === null ||245 typeof objB !== "object" ||246 objB === null247 ) {248 return false249 }250 const keysA = Object.keys(objA)251 const keysB = Object.keys(objB)252 if (keysA.length !== keysB.length) return false253 for (let i = 0; i < keysA.length; i++) {254 if (255 !hasOwnProperty.call(objB, keysA[i]) ||Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 256why, one step back256 !is(objA[keysA[i]], objB[keysA[i]])transpile es5 codeMichel Weststratebefore adoptionf012154
lines 257–263why, one step back257 ) {258 return false259 }260 }261 return true262}263Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 264–269why, one step back264function createHiddenProperty(target, prop, value) {265 Object.defineProperty(target, prop, {266 value: value,267 enumerable: false,268 writable: true269 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 270why, one step back270}