Skip to content
Myr

es5.js

This file

Path

src/es5.js

220 lines · 5.8 KiB · JavaScript

Last changed by

remove unused varsiruca3before adoption9d54d64

History of this file

Take it

transpile es5 codeMichel Weststratebefore adoptionf012154

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

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

lines 4–8why, one step back
4import {
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 back
9 shallowCopy,
10 verifyReturnValue,11 each,12 finalize

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 14why, one step back
14

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 15–16why, one step back
15const descriptors = {}
16let states = null

transpile es5 codeMichel Weststratebefore adoptionf012154

line 17why, one step back
17

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 18–26why, one step back
18function 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 back
27 finalizing: false,
28 finalized: false

transpile es5 codeMichel Weststratebefore adoptionf012154

line 29why, one step back
29 }

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 30why, one step back
30}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 31why, one step back
31

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 32–41why, one step back
32function 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 back
42 }

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 43–44why, one step back
43 return value
44}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 45why, one step back
45

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 52–54why, one step back
52 }
53 state.copy[prop] = value54}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 55why, one step back
55

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 56–61why, one step back
56function 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 back
62

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 63–65why, one step back
63function prepareCopy(state) {
64 if (state.hasCopy) return65 state.hasCopy = true

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 67why, one step back
67}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 68why, one step back
68

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 69–70why, one step back
69// creates a proxy for plain objects / arrays
70function createProxy(parent, base) {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 71–73why, one step back
71 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 back
74 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 back
79

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 80–90why, one step back
80function 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 back
91 }

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 92–94why, one step back
92 })
93 )94}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 95why, one step back
95

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 96–97why, one step back
96function createObjectProxy(base) {
97 const proxy = Object.assign({}, base)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 98why, one step back
98 each(base, prop => {

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

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

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 100why, one step back
100 })

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 101–102why, one step back
101 return proxy
102}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 103why, one step back
103

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 104–105why, one step back
104function createArrayProxy(base) {
105 const proxy = new Array(base.length)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

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

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 108why, one step back
108 })

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 109–110why, one step back
109 return proxy
110}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 111why, one step back
111

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 112–115why, one step back
112function 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 back
116 )

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 117why, one step back
117}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 118why, one step back
118

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 119–131why, one step back
119// this sounds very expensive, but actually it is not that extensive in practice
120// 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 back
132 }
133 }

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 134why, one step back
134}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 135why, one step back
135

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 136–140why, one step back
136function 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 back
141

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 142–144why, one step back
142function hasArrayChanges(state) {
143 return state.proxy.length !== state.base.length144}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 145why, one step back
145

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 146–147why, one step back
146export 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 back
148 const base = state.base

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 149–151why, one step back
149 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 back
152 return freeze(res)

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 153–154why, one step back
153}
154

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 155–157why, one step back
155export function finalizeArray(proxy, state) {
156 const res = (state.copy = shallowCopy(proxy))157 each(res, (i, value) => {

remove extra conditioniruca3before adoptionac122b7

line 158why, one step back
158 res[i] = finalize(value)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 159why, one step back
159 })

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 160why, one step back
160 return freeze(res)

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 161–162why, one step back
161}
162

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 163–169why, one step back
163export function produceEs5(baseState, producer) {
164 const prevStates = states165 states = []166 try {167 // create proxy for root168 const rootClone = createProxy(undefined, baseState)169 // execute the thunk

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

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

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 172–174why, one step back
172 each(states, (_, state) => {
173 state.finalizing = true174 })

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 175why, one step back
175 // 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 back
176 // TODO: store states by depth, to be able guarantee processing leaves first

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 177–179why, one step back
177 markChanges()
178 const res = finalize(rootClone)179 // make sure all proxies become unusable

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 180–182why, one step back
180 each(states, (_, state) => {
181 state.finished = true182 })

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 183–185why, one step back
183 return res
184 } finally {185 states = prevStates

transpile es5 codeMichel Weststratebefore adoptionf012154

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 214–219why, one step back
214function 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 back
220}