es5.js
This file
Path
src/es5.js
191 lines · 5.3 KiB · JavaScript
Last changed by
Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
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–9why, one step back6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 10–13why, one step back10 shallowCopy,11 verifyReturnValue,12 each,13 finalizeFixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a
line 14why, one step back14} from "./common"transpile es5 codeMichel Weststratebefore adoptionf012154
line 15why, one step back15Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 16–17why, one step back16const descriptors = {}17let states = nulltranspile es5 codeMichel Weststratebefore adoptionf012154
line 18why, one step back18Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 19–27why, one step back19function createState(parent, proxy, base) {20 return {21 modified: false,22 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–51why, one step back49function set(state, prop, value) {50 assertUnfinished(state)51 if (!state.modified) {Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 52why, one step back52 if (is(source(state)[prop], value)) returnMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 53why, one step back53 markChanged(state)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 54why, one step back54 prepareCopy(state)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 55–57why, one step back55 }56 state.copy[prop] = value57}transpile es5 codeMichel Weststratebefore adoptionf012154
line 58why, one step back58Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 59–64why, one step back59function markChanged(state) {60 if (!state.modified) {61 state.modified = true62 if (state.parent) markChanged(state.parent)63 }64}transpile es5 codeMichel Weststratebefore adoptionf012154
line 65why, one step back65Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 66–68why, one step back66function prepareCopy(state) {67 if (state.hasCopy) return68 state.hasCopy = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 69why, one step back69 state.copy = shallowCopy(state.base)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 70why, one step back70}transpile es5 codeMichel Weststratebefore adoptionf012154
line 71why, one step back71Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 72–73why, one step back72// creates a proxy for plain objects / arrays73function createProxy(parent, base) {Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092
lines 74–77why, one step back74 const proxy = shallowCopy(base)75 each(base, i => {76 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))77 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 78–82why, one step back78 const state = createState(parent, proxy, base)79 createHiddenProperty(proxy, PROXY_STATE, state)80 states.push(state)81 return proxy82}transpile es5 codeMichel Weststratebefore adoptionf012154
line 83why, one step back83Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 84–94why, one step back84function createPropertyProxy(prop) {85 return (86 descriptors[prop] ||87 (descriptors[prop] = {88 configurable: true,89 enumerable: true,90 get() {91 return get(this[PROXY_STATE], prop)92 },93 set(value) {94 set(this[PROXY_STATE], prop, value)transpile es5 codeMichel Weststratebefore adoptionf012154
line 95why, one step back95 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 96–98why, one step back96 })97 )98}transpile es5 codeMichel Weststratebefore adoptionf012154
line 99why, one step back99Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 100–103why, one step back100function assertUnfinished(state) {101 if (state.finished === true)102 throw new Error(103 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process?"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 #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 107–119why, one step back107// this sounds very expensive, but actually it is not that extensive in practice108// 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
lines 130–131why, one step back130function hasArrayChanges(state) {131 return state.proxy.length !== state.base.lengthtranspile es5 codeMichel Weststratebefore adoptionf012154
lines 132–133why, one step back132}133Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 134–140why, one step back134export function produceEs5(baseState, producer) {135 const prevStates = states136 states = []137 try {138 // create proxy for root139 const rootClone = createProxy(undefined, baseState)140 // execute the thunkImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 141why, one step back141 verifyReturnValue(producer.call(rootClone, rootClone))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 142why, one step back142 // and finalize the modified proxyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 143–145why, one step back143 each(states, (_, state) => {144 state.finalizing = true145 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 146why, one step back146 // find and mark all changes (for parts not done yet)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 147why, one step back147 // TODO: store states by depth, to be able guarantee processing leaves firstMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 148–150why, one step back148 markChanges()149 const res = finalize(rootClone)150 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 151–153why, one step back151 each(states, (_, state) => {152 state.finished = true153 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 154–156why, one step back154 return res155 } finally {156 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 157–161why, one step back157 }158}159160function shallowEqual(objA, objB) {161 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.jsAdded polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 162why, one step back162 if (is(objA, objB)) return truetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 163–176why, one step back163 if (164 typeof objA !== "object" ||165 objA === null ||166 typeof objB !== "object" ||167 objB === null168 ) {169 return false170 }171 const keysA = Object.keys(objA)172 const keysB = Object.keys(objB)173 if (keysA.length !== keysB.length) return false174 for (let i = 0; i < keysA.length; i++) {175 if (176 !hasOwnProperty.call(objB, keysA[i]) ||Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 177why, one step back177 !is(objA[keysA[i]], objB[keysA[i]])transpile es5 codeMichel Weststratebefore adoptionf012154
lines 178–184why, one step back178 ) {179 return false180 }181 }182 return true183}184Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 185–190why, one step back185function createHiddenProperty(target, prop, value) {186 Object.defineProperty(target, prop, {187 value: value,188 enumerable: false,189 writable: true190 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 191why, one step back191}