Skip to content
Myr

es5.js

This file

Path

src/es5.js

235 lines · 6.4 KiB · JavaScript

Last changed by

cleanupMichel Weststratebefore adoptiond536bc9

History of this file

Take it

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 1–3why, one step back
1"use strict"
2// @ts-check3

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

lines 4–9why, one step back
4import {
5 autoFreeze,6 isProxyable,7 isProxy,8 freeze,9 PROXY_STATE,

small improvementsMichel Weststratebefore adoption7d0b7e8

line 10why, one step back
10 finalizeNonProxiedObject,

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 11–13why, one step back
11 shallowCopy,
12 verifyReturnValue,13 each

Merge branch 'master' into feature/fast-pruneMichel Weststratebefore adoptionf8f402c

line 14why, one step back
14} from "./common"

transpile es5 codeMichel Weststratebefore adoptionf012154

line 15why, one step back
15

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

line 16why, one step back
16const descriptors = {}

Both implementation statelessMichel Weststratebefore adoption9b3bceb

line 17why, one step back
17let states = null

transpile es5 codeMichel Weststratebefore adoptionf012154

line 18why, one step back
18

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 19–50why, one step back
19function 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 back
51 prepareCopy(state)

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 52–65why, one step back
52 }
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 = true

small improvementsMichel Weststratebefore adoption7d0b7e8

line 66why, one step back
66 state.copy = shallowCopy(state.base)

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 67–70why, one step back
67}
6869// creates a proxy for plain objects / arrays70function createProxy(parent, base) {

cleanupMichel Weststratebefore adoptiond536bc9

lines 71–73why, one step back
71 const proxy = Array.isArray(base)
72 ? createArrayProxy(base)73 : createObjectProxy(base)

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 74–97why, one step back
74 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 back
98 each(base, prop => {

Both implementation statelessMichel Weststratebefore adoption9b3bceb

line 99why, one step back
99 Object.defineProperty(proxy, prop, createPropertyProxy(prop))

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 100why, one step back
100 })

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 101–105why, one step back
101 return proxy
102}103104function createArrayProxy(base) {105 const proxy = new Array(base.length)

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 106why, one step back
106 each(base, i => {

Both implementation statelessMichel Weststratebefore adoption9b3bceb

line 107why, one step back
107 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 108why, one step back
108 })

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 109–154why, one step back
109 return proxy
110}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 back
155 finalizeNonProxiedObject(proxy, finalize)

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 156–162why, one step back
156 }
157 return proxy158}159160function finalizeObject(proxy, state) {161 const res = Object.assign({}, proxy)162 const base = state.base

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 163–165why, one step back
163 each(res, (prop, value) => {
164 if (value !== base[prop]) res[prop] = finalize(value)165 })

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 166–171why, one step back
166 return freeze(res)
167}168169function finalizeArray(proxy, state) {170 const res = proxy.slice()171 const base = state.base

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 172–174why, one step back
172 each(res, (i, value) => {
173 if (value !== base[i]) res[i] = finalize(value)174 })

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 175–177why, one step back
175 return freeze(res)
176}177

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

line 178why, one step back
178export function produceEs5(baseState, producer) {

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 179–184why, one step back
179 const prevStates = states
180 states = []181 try {182 // create proxy for root183 const rootClone = createProxy(undefined, baseState)184 // execute the thunk

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

line 185why, one step back
185 verifyReturnValue(producer(rootClone))

Both implementation statelessMichel Weststratebefore adoption9b3bceb

line 186why, one step back
186 // and finalize the modified proxy

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 187–189why, one step back
187 each(states, (_, state) => {
188 state.finalizing = true189 })

Both implementation statelessMichel Weststratebefore adoption9b3bceb

line 190why, one step back
190 // find and mark all changes (for parts not done yet)

cleanupMichel Weststratebefore adoptiond536bc9

line 191why, one step back
191 // TODO: store states by depth, to be able guarantee processing leaves first

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 192–194why, one step back
192 markChanges()
193 const res = finalize(rootClone)194 // make sure all proxies become unusable

some code cleanup / more reuseMichel Weststratebefore adoption9d393e3

lines 195–197why, one step back
195 each(states, (_, state) => {
196 state.finished = true197 })

Both implementation statelessMichel Weststratebefore adoption9b3bceb

lines 198–200why, one step back
198 return res
199 } finally {200 states = prevStates

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 201–228why, one step back
201 }
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}228

One module to rule them all WIPMichel Weststratebefore adoptionb7647e4

lines 229–234why, one step back
229function 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 back
235}