es5.js
This file
Path
src/es5.js
198 lines · 5.6 KiB · JavaScript
Last changed by
Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
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
line 10why, one step back10 shallowCopy,Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 11why, one step back11 RETURNED_AND_MODIFIED_ERROR,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 12–13why, one step back12 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–138why, one step back134export function produceEs5(baseState, producer) {135 const prevStates = states136 states = []137 try {138 // create proxy for rootImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 139why, one step back139 const rootProxy = createProxy(undefined, baseState)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 140why, one step back140 // execute the thunkImplemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 141why, one step back141 const returnValue = producer.call(rootProxy, rootProxy)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
line 148why, one step back148 markChanges()Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
lines 149–156why, one step back149 let result = finalize(rootProxy)150 // check whether the draft was modified and/or a value was returned151 if (returnValue !== undefined && returnValue !== rootProxy) {152 // something was returned, and it wasn't the proxy itself153 if (rootProxy[PROXY_STATE].modified)154 throw new Error(RETURNED_AND_MODIFIED_ERROR)155 result = returnValue156 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 157why, one step back157 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 158–160why, one step back158 each(states, (_, state) => {159 state.finished = true160 })Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd
line 161why, one step back161 return resultMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 162–163why, one step back162 } finally {163 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 164–168why, one step back164 }165}166167function shallowEqual(objA, objB) {168 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.jsAdded polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 169why, one step back169 if (is(objA, objB)) return truetranspile es5 codeMichel Weststratebefore adoptionf012154
lines 170–183why, one step back170 if (171 typeof objA !== "object" ||172 objA === null ||173 typeof objB !== "object" ||174 objB === null175 ) {176 return false177 }178 const keysA = Object.keys(objA)179 const keysB = Object.keys(objB)180 if (keysA.length !== keysB.length) return false181 for (let i = 0; i < keysA.length; i++) {182 if (183 !hasOwnProperty.call(objB, keysA[i]) ||Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1
line 184why, one step back184 !is(objA[keysA[i]], objB[keysA[i]])transpile es5 codeMichel Weststratebefore adoptionf012154
lines 185–191why, one step back185 ) {186 return false187 }188 }189 return true190}191Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 192–197why, one step back192function createHiddenProperty(target, prop, value) {193 Object.defineProperty(target, prop, {194 value: value,195 enumerable: false,196 writable: true197 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 198why, one step back198}