Skip to content
Myr

es5.js

This file

Path

src/es5.js

222 lines · 5.9 KiB · JavaScript

Last changed by

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

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

line 4why, one step back
4import {

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

line 5why, one step back
5 is,

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

lines 6–9why, one step back
6 isProxyable,
7 isProxy,8 freeze,9 PROXY_STATE,

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

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

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 15why, one step back
15

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 18why, one step back
18

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

lines 19–27why, 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,

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

lines 28–29why, one step back
28 finalizing: false,
29 finalized: false

transpile es5 codeMichel Weststratebefore adoptionf012154

line 30why, one step back
30 }

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

line 31why, one step back
31}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 32why, one step back
32

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

lines 33–42why, one step back
33function source(state) {
34 return state.hasCopy ? state.copy : state.base35}3637function get(state, prop) {38 assertUnfinished(state)39 const value = source(state)[prop]40 if (!state.finalizing && !isProxy(value) && isProxyable(value)) {41 prepareCopy(state)42 return (state.copy[prop] = createProxy(state, value))

transpile es5 codeMichel Weststratebefore adoptionf012154

line 43why, one step back
43 }

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 46why, one step back
46

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

lines 47–49why, one step back
47function set(state, prop, value) {
48 assertUnfinished(state)49 if (!state.modified) {

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

line 50why, one step back
50 if (is(source(state)[prop], value)) return

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

line 51why, one step back
51 markChanged(state)

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

line 52why, one step back
52 prepareCopy(state)

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 56why, one step back
56

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

lines 57–62why, one step back
57function markChanged(state) {
58 if (!state.modified) {59 state.modified = true60 if (state.parent) markChanged(state.parent)61 }62}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 63why, one step back
63

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

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

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

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

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

line 68why, one step back
68}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 69why, one step back
69

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

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

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

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

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

lines 75–79why, one step back
75 const state = createState(parent, proxy, base)
76 createHiddenProperty(proxy, PROXY_STATE, state)77 states.push(state)78 return proxy79}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 80why, one step back
80

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

lines 81–91why, one step back
81function createPropertyProxy(prop) {
82 return (83 descriptors[prop] ||84 (descriptors[prop] = {85 configurable: true,86 enumerable: true,87 get() {88 return get(this[PROXY_STATE], prop)89 },90 set(value) {91 set(this[PROXY_STATE], prop, value)

transpile es5 codeMichel Weststratebefore adoptionf012154

line 92why, one step back
92 }

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 96why, one step back
96

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

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

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

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

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

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

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

line 101why, one step back
101 })

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 104why, one step back
104

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

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

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

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

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

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

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

line 109why, one step back
109 })

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 112why, one step back
112

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

lines 113–116why, one step back
113function assertUnfinished(state) {
114 if (state.finished === true)115 throw new Error(116 "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 117why, one step back
117 )

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

line 118why, one step back
118}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 119why, one step back
119

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

lines 120–132why, one step back
120// this sounds very expensive, but actually it is not that extensive in practice
121// as it will only visit proxies, and only do key-based change detection for objects for122// which it is not already know that they are changed (that is, only object for which no known key was changed)123function markChanges() {124 // intentionally we process the proxies in reverse order;125 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore126 // reverse order of proxy creation approximates this127 for (let i = states.length - 1; i >= 0; i--) {128 const state = states[i]129 if (state.modified === false) {130 if (Array.isArray(state.base)) {131 if (hasArrayChanges(state)) markChanged(state)132 } else if (hasObjectChanges(state)) markChanged(state)

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 133–134why, one step back
133 }
134 }

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

line 135why, one step back
135}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 136why, one step back
136

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

lines 137–141why, one step back
137function hasObjectChanges(state) {
138 const baseKeys = Object.keys(state.base)139 const keys = Object.keys(state.proxy)140 return !shallowEqual(baseKeys, keys)141}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 142why, one step back
142

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 146why, one step back
146

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

lines 147–148why, one step back
147export function finalizeObject(proxy, state) {
148 const res = (state.copy = shallowCopy(proxy))

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

line 149why, one step back
149 const base = state.base

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

lines 150–152why, one step back
150 each(res, (prop, value) => {
151 if (value !== base[prop]) res[prop] = finalize(value)152 })

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

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

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

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

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

line 158why, one step back
158 const base = state.base

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

lines 159–161why, one step back
159 each(res, (i, value) => {
160 if (value !== base[i]) res[i] = finalize(value)161 })

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 163–164why, one step back
163}
164

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

lines 165–171why, one step back
165export function produceEs5(baseState, producer) {
166 const prevStates = states167 states = []168 try {169 // create proxy for root170 const rootClone = createProxy(undefined, baseState)171 // execute the thunk

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

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

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

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

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

lines 174–176why, one step back
174 each(states, (_, state) => {
175 state.finalizing = true176 })

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

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

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

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

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

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

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

lines 182–184why, one step back
182 each(states, (_, state) => {
183 state.finished = true184 })

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 188–192why, one step back
188 }
189}190191function shallowEqual(objA, objB) {192 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

line 193why, one step back
193 if (is(objA, objB)) return true

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 194–207why, one step back
194 if (
195 typeof objA !== "object" ||196 objA === null ||197 typeof objB !== "object" ||198 objB === null199 ) {200 return false201 }202 const keysA = Object.keys(objA)203 const keysB = Object.keys(objB)204 if (keysA.length !== keysB.length) return false205 for (let i = 0; i < keysA.length; i++) {206 if (207 !hasOwnProperty.call(objB, keysA[i]) ||

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

line 208why, one step back
208 !is(objA[keysA[i]], objB[keysA[i]])

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 209–215why, one step back
209 ) {
210 return false211 }212 }213 return true214}215

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

lines 216–221why, one step back
216function createHiddenProperty(target, prop, value) {
217 Object.defineProperty(target, prop, {218 value: value,219 enumerable: false,220 writable: true221 })

transpile es5 codeMichel Weststratebefore adoptionf012154

line 222why, one step back
222}