es5.js
This file
Path
src/es5.js
235 lines · 6.4 KiB · JavaScript
Last changed by
cleanupMichel Weststratebefore adoptiond536bc9
History of this fileTake it
transpile es5 codeMichel Weststratebefore adoptionf012154
lines 1–3why, one step back1"use strict"2// @ts-check3Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
lines 4–9why, one step back4import {5 autoFreeze,6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,small improvementsMichel Weststratebefore adoption7d0b7e8
line 10why, one step back10 finalizeNonProxiedObject,some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 11–13why, one step back11 shallowCopy,12 verifyReturnValue,13 eachMerge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
line 14why, one step back14} from "./common"transpile es5 codeMichel Weststratebefore adoptionf012154
line 15why, one step back15One module to rule them all WIPMichel Weststratebefore adoptionb7647e4
line 16why, one step back16const descriptors = {}Both implementation statelessMichel Weststratebefore adoption9b3bceb
line 17why, one step back17let states = nulltranspile es5 codeMichel Weststratebefore adoptionf012154
line 18why, one step back18Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 19–50why, 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,28 finalizing: false29 }30}3132function 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))42 }43 return value44}4546function set(state, prop, value) {47 assertUnfinished(state)48 if (!state.modified) {49 if (Object.is(source(state)[prop], value)) return50 markChanged(state)small improvementsMichel Weststratebefore adoption7d0b7e8
line 51why, one step back51 prepareCopy(state)Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 52–65why, one step back52 }53 state.copy[prop] = value54}5556function markChanged(state) {57 if (!state.modified) {58 state.modified = true59 if (state.parent) markChanged(state.parent)60 }61}6263function prepareCopy(state) {64 if (state.hasCopy) return65 state.hasCopy = truesmall improvementsMichel Weststratebefore adoption7d0b7e8
line 66why, one step back66 state.copy = shallowCopy(state.base)Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 67–70why, one step back67}6869// creates a proxy for plain objects / arrays70function createProxy(parent, base) {cleanupMichel Weststratebefore adoptiond536bc9
lines 71–73why, one step back71 const proxy = Array.isArray(base)72 ? createArrayProxy(base)73 : createObjectProxy(base)Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 74–97why, one step back74 const state = createState(parent, proxy, base)75 createHiddenProperty(proxy, PROXY_STATE, state)76 states.push(state)77 return proxy78}7980function 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)91 }92 })93 )94}9596function createObjectProxy(base) {97 const proxy = Object.assign({}, base)some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 98why, one step back98 each(base, prop => {Both implementation statelessMichel Weststratebefore adoption9b3bceb
line 99why, one step back99 Object.defineProperty(proxy, prop, createPropertyProxy(prop))some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 100why, one step back100 })Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 101–105why, one step back101 return proxy102}103104function createArrayProxy(base) {105 const proxy = new Array(base.length)some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 106why, one step back106 each(base, i => {Both implementation statelessMichel Weststratebefore adoption9b3bceb
line 107why, one step back107 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))some code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 108why, one step back108 })Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 109–154why, one step back109 return proxy110}111112function 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?"116 )117}118119// 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)132 }133 }134}135136function hasObjectChanges(state) {137 const baseKeys = Object.keys(state.base)138 const keys = Object.keys(state.proxy)139 return !shallowEqual(baseKeys, keys)140}141142function hasArrayChanges(state) {143 return state.proxy.length !== state.base.length144}145146function finalize(proxy) {147 // TODO: almost litterally same as Proxy impl; let's reduce code duplication and rollup148 if (isProxy(proxy)) {149 const state = proxy[PROXY_STATE]150 if (state.modified === true) {151 if (Array.isArray(state.base)) return finalizeArray(proxy, state)152 return finalizeObject(proxy, state)153 } else return state.base154 } else if (proxy !== null && typeof proxy === "object") {Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c
line 155why, one step back155 finalizeNonProxiedObject(proxy, finalize)Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 156–162why, one step back156 }157 return proxy158}159160function finalizeObject(proxy, state) {161 const res = Object.assign({}, proxy)162 const base = state.basesome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 163–165why, one step back163 each(res, (prop, value) => {164 if (value !== base[prop]) res[prop] = finalize(value)165 })Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 166–171why, one step back166 return freeze(res)167}168169function finalizeArray(proxy, state) {170 const res = proxy.slice()171 const base = state.basesome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 172–174why, one step back172 each(res, (i, value) => {173 if (value !== base[i]) res[i] = finalize(value)174 })Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 175–177why, one step back175 return freeze(res)176}177One module to rule them all WIPMichel Weststratebefore adoptionb7647e4
line 178why, one step back178export function produceEs5(baseState, producer) {Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 179–184why, one step back179 const prevStates = states180 states = []181 try {182 // create proxy for root183 const rootClone = createProxy(undefined, baseState)184 // execute the thunksome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
line 185why, one step back185 verifyReturnValue(producer(rootClone))Both implementation statelessMichel Weststratebefore adoption9b3bceb
line 186why, one step back186 // and finalize the modified proxysome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 187–189why, one step back187 each(states, (_, state) => {188 state.finalizing = true189 })Both implementation statelessMichel Weststratebefore adoption9b3bceb
line 190why, one step back190 // find and mark all changes (for parts not done yet)cleanupMichel Weststratebefore adoptiond536bc9
line 191why, one step back191 // TODO: store states by depth, to be able guarantee processing leaves firstBoth implementation statelessMichel Weststratebefore adoption9b3bceb
lines 192–194why, one step back192 markChanges()193 const res = finalize(rootClone)194 // make sure all proxies become unusablesome code cleanup / more reuseMichel Weststratebefore adoption9d393e3
lines 195–197why, one step back195 each(states, (_, state) => {196 state.finished = true197 })Both implementation statelessMichel Weststratebefore adoption9b3bceb
lines 198–200why, one step back198 return res199 } finally {200 states = prevStatestranspile es5 codeMichel Weststratebefore adoptionf012154
lines 201–228why, one step back201 }202}203204function shallowEqual(objA, objB) {205 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js206 if (Object.is(objA, objB)) return true207 if (208 typeof objA !== "object" ||209 objA === null ||210 typeof objB !== "object" ||211 objB === null212 ) {213 return false214 }215 const keysA = Object.keys(objA)216 const keysB = Object.keys(objB)217 if (keysA.length !== keysB.length) return false218 for (let i = 0; i < keysA.length; i++) {219 if (220 !hasOwnProperty.call(objB, keysA[i]) ||221 !Object.is(objA[keysA[i]], objB[keysA[i]])222 ) {223 return false224 }225 }226 return true227}228One module to rule them all WIPMichel Weststratebefore adoptionb7647e4
lines 229–234why, one step back229function createHiddenProperty(target, prop, value) {230 Object.defineProperty(target, prop, {231 value: value,232 enumerable: false,233 writable: true234 })transpile es5 codeMichel Weststratebefore adoptionf012154
line 235why, one step back235}