es5.js
This file
Path
src/es5.js
220 lines · 5.8 KiB · JavaScript
Last changed by
remove unused varsiruca3before adoption9d54d64
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
lines 4–8why, one step back4import {5 isProxyable,6 isProxy,7 freeze,8 PROXY_STATE,Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 9–12why, one step back9 shallowCopy,10 verifyReturnValue,11 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–41why, 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]39 if (!state.finalizing && !isProxy(value) && isProxyable(value)) {40 prepareCopy(state)41 return (state.copy[prop] = createProxy(state, value))transpile es5 codeMichel Weststratebefore adoptionf012154
line 42why, one step back42 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 43–44why, one step back43 return value44}transpile es5 codeMichel Weststratebefore adoptionf012154
line 45why, one step back45Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 46–50why, one step back46function set(state, prop, value) {47 assertUnfinished(state)48 if (!state.modified) {49 if (Object.is(source(state)[prop], value)) return50 markChanged(state)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 51why, one step back51 prepareCopy(state)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 52–54why, one step back52 }53 state.copy[prop] = value54}transpile es5 codeMichel Weststratebefore adoptionf012154
line 55why, one step back55Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 56–61why, one step back56function markChanged(state) {57 if (!state.modified) {58 state.modified = true59 if (state.parent) markChanged(state.parent)60 }61}transpile es5 codeMichel Weststratebefore adoptionf012154
line 62why, one step back62Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 63–65why, one step back63function prepareCopy(state) {64 if (state.hasCopy) return65 state.hasCopy = trueMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 66why, one step back66 state.copy = shallowCopy(state.base)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 67why, one step back67}transpile es5 codeMichel Weststratebefore adoptionf012154
line 68why, one step back68Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 69–70why, one step back69// creates a proxy for plain objects / arrays70function createProxy(parent, base) {Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 71–73why, one step back71 const proxy = Array.isArray(base)72 ? createArrayProxy(base)73 : createObjectProxy(base)Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 74–78why, one step back74 const state = createState(parent, proxy, base)75 createHiddenProperty(proxy, PROXY_STATE, state)76 states.push(state)77 return proxy78}transpile es5 codeMichel Weststratebefore adoptionf012154
line 79why, one step back79Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 80–90why, one step back80function createPropertyProxy(prop) {81 return (82 descriptors[prop] ||83 (descriptors[prop] = {84 configurable: true,85 enumerable: true,86 get() {87 return get(this[PROXY_STATE], prop)88 },89 set(value) {90 set(this[PROXY_STATE], prop, value)transpile es5 codeMichel Weststratebefore adoptionf012154
line 91why, one step back91 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 92–94why, one step back92 })93 )94}transpile es5 codeMichel Weststratebefore adoptionf012154
line 95why, one step back95Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 96–97why, one step back96function createObjectProxy(base) {97 const proxy = Object.assign({}, base)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 98why, one step back98 each(base, prop => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 99why, one step back99 Object.defineProperty(proxy, prop, createPropertyProxy(prop))Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 100why, one step back100 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 101–102why, one step back101 return proxy102}transpile es5 codeMichel Weststratebefore adoptionf012154
line 103why, one step back103Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 104–105why, one step back104function createArrayProxy(base) {105 const proxy = new Array(base.length)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 106why, one step back106 each(base, i => {Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 107why, one step back107 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 108why, one step back108 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 109–110why, one step back109 return proxy110}transpile es5 codeMichel Weststratebefore adoptionf012154
line 111why, one step back111Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 112–115why, one step back112function assertUnfinished(state) {113 if (state.finished === true)114 throw new Error(115 "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 116why, one step back116 )Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 117why, one step back117}transpile es5 codeMichel Weststratebefore adoptionf012154
line 118why, one step back118Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 119–131why, one step back119// this sounds very expensive, but actually it is not that extensive in practice120// as it will only visit proxies, and only do key-based change detection for objects for121// which it is not already know that they are changed (that is, only object for which no known key was changed)122function markChanges() {123 // intentionally we process the proxies in reverse order;124 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore125 // reverse order of proxy creation approximates this126 for (let i = states.length - 1; i >= 0; i--) {127 const state = states[i]128 if (state.modified === false) {129 if (Array.isArray(state.base)) {130 if (hasArrayChanges(state)) markChanged(state)131 } else if (hasObjectChanges(state)) markChanged(state)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 132–133why, one step back132 }133 }Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 134why, one step back134}transpile es5 codeMichel Weststratebefore adoptionf012154
line 135why, one step back135Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 136–140why, one step back136function hasObjectChanges(state) {137 const baseKeys = Object.keys(state.base)138 const keys = Object.keys(state.proxy)139 return !shallowEqual(baseKeys, keys)140}transpile es5 codeMichel Weststratebefore adoptionf012154
line 141why, one step back141Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 142–144why, one step back142function hasArrayChanges(state) {143 return state.proxy.length !== state.base.length144}transpile es5 codeMichel Weststratebefore adoptionf012154
line 145why, one step back145Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 146–147why, one step back146export function finalizeObject(proxy, state) {147 const res = (state.copy = shallowCopy(proxy))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 148why, one step back148 const base = state.baseMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 149–151why, one step back149 each(res, (prop, value) => {150 if (value !== base[prop]) res[prop] = finalize(value)151 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 152why, one step back152 return freeze(res)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 153–154why, one step back153}154Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 155–157why, one step back155export function finalizeArray(proxy, state) {156 const res = (state.copy = shallowCopy(proxy))157 each(res, (i, value) => {remove extra conditioniruca3before adoptionac122b7
line 158why, one step back158 res[i] = finalize(value)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 159why, one step back159 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 160why, one step back160 return freeze(res)transpile es5 codeMichel Weststratebefore adoptionf012154
lines 161–162why, one step back161}162Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 163–169why, one step back163export function produceEs5(baseState, producer) {164 const prevStates = states165 states = []166 try {167 // create proxy for root168 const rootClone = createProxy(undefined, baseState)169 // execute the thunkMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 170why, one step back170 verifyReturnValue(producer(rootClone))Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 171why, one step back171 // and finalize the modified proxyMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 172–174why, one step back172 each(states, (_, state) => {173 state.finalizing = true174 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
line 175why, one step back175 // find and mark all changes (for parts not done yet)Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
line 176why, one step back176 // TODO: store states by depth, to be able guarantee processing leaves firstMerge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 177–179why, one step back177 markChanges()178 const res = finalize(rootClone)179 // make sure all proxies become unusableMerge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a
lines 180–182why, one step back180 each(states, (_, state) => {181 state.finished = true182 })Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 183–185why, one step back183 return res184 } finally {185 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 186–213why, one step back186 }187}188189function shallowEqual(objA, objB) {190 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js191 if (Object.is(objA, objB)) return true192 if (193 typeof objA !== "object" ||194 objA === null ||195 typeof objB !== "object" ||196 objB === null197 ) {198 return false199 }200 const keysA = Object.keys(objA)201 const keysB = Object.keys(objB)202 if (keysA.length !== keysB.length) return false203 for (let i = 0; i < keysA.length; i++) {204 if (205 !hasOwnProperty.call(objB, keysA[i]) ||206 !Object.is(objA[keysA[i]], objB[keysA[i]])207 ) {208 return false209 }210 }211 return true212}213Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0
lines 214–219why, one step back214function createHiddenProperty(target, prop, value) {215 Object.defineProperty(target, prop, {216 value: value,217 enumerable: false,218 writable: true219 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 220why, one step back220}