Skip to content
Myr

es5.js

This file

Path

src/es5.js

210 lines · 6.5 KiB · JavaScript

Last changed by

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

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–8why, one step back
6 isProxyable,
7 isProxy,8 PROXY_STATE,

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

line 9why, one step back
9 shallowCopy,

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 10why, one step back
10 RETURNED_AND_MODIFIED_ERROR,

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

lines 11–12why, one step back
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–38why, 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]

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 39–41why, one step back
39 if (!state.finalizing && value === state.base[prop] && isProxyable(value)) {
40 // only create a proxy if the value is proxyable, and the value was in the base state41 // if it wasn't in the base state, the object is already modified and we will process it in finalize

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

lines 42–43why, one step back
42 prepareCopy(state)
43 return (state.copy[prop] = createProxy(state, value))

transpile es5 codeMichel Weststratebefore adoptionf012154

line 44why, one step back
44 }

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 47why, one step back
47

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

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

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

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

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

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

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

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

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 57why, one step back
57

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

lines 58–63why, one step back
58function markChanged(state) {
59 if (!state.modified) {60 state.modified = true61 if (state.parent) markChanged(state.parent)62 }63}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 64why, one step back
64

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

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

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

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

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

line 69why, one step back
69}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 70why, one step back
70

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

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

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 73–76why, one step back
73 const proxy = shallowCopy(base)
74 each(base, i => {75 Object.defineProperty(proxy, "" + i, createPropertyProxy("" + i))76 })

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 82why, one step back
82

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 94why, one step back
94 }

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

lines 95–97why, one step back
95 })
96 )97}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 98why, one step back
98

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

lines 99–102why, one step back
99function assertUnfinished(state) {
100 if (state.finished === true)101 throw new Error(102 "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 103why, one step back
103 )

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

line 104why, one step back
104}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 105why, one step back
105

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

lines 106–118why, one step back
106// this sounds very expensive, but actually it is not that extensive in practice
107// as it will only visit proxies, and only do key-based change detection for objects for108// which it is not already know that they are changed (that is, only object for which no known key was changed)109function markChanges() {110 // intentionally we process the proxies in reverse order;111 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore112 // reverse order of proxy creation approximates this113 for (let i = states.length - 1; i >= 0; i--) {114 const state = states[i]115 if (state.modified === false) {116 if (Array.isArray(state.base)) {117 if (hasArrayChanges(state)) markChanged(state)118 } else if (hasObjectChanges(state)) markChanged(state)

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 119–120why, one step back
119 }
120 }

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

line 121why, one step back
121}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 122why, one step back
122

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

lines 123–127why, one step back
123function hasObjectChanges(state) {
124 const baseKeys = Object.keys(state.base)125 const keys = Object.keys(state.proxy)126 return !shallowEqual(baseKeys, keys)127}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 128why, one step back
128

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

line 129why, one step back
129function hasArrayChanges(state) {

Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df

lines 130–136why, one step back
130 const {proxy} = state
131 if (proxy.length !== state.base.length) return true132 // See #116133 // If we first shorten the length, our array interceptors will be removed.134 // If after that new items are added, result in the same original length,135 // those last items will have no intercepting property.136 // So if there is no own descriptor on the last position, we know that items were removed and added

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

lines 137–138why, one step back
137 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
138 // the last one

Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df

line 139why, one step back
139 const descriptor = Object.getOwnPropertyDescriptor(proxy, proxy.length - 1)

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 140why, one step back
140 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)

Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df

lines 141–143why, one step back
141 if (descriptor && !descriptor.get) return true
142 // For all other cases, we don't have to compare, as they would have been picked up by the index setters143 return false

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 144–145why, one step back
144}
145

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

lines 146–150why, one step back
146export function produceEs5(baseState, producer) {
147 const prevStates = states148 states = []149 try {150 // create proxy for root

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 151why, one step back
151 const rootProxy = createProxy(undefined, baseState)

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

line 152why, one step back
152 // execute the thunk

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 153why, one step back
153 const returnValue = producer.call(rootProxy, rootProxy)

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

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

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

lines 155–157why, one step back
155 each(states, (_, state) => {
156 state.finalizing = true157 })

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

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

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

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

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

line 160why, one step back
160 markChanges()

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 161why, one step back
161 let result

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 162–166why, one step back
162 // check whether the draft was modified and/or a value was returned
163 if (returnValue !== undefined && returnValue !== rootProxy) {164 // something was returned, and it wasn't the proxy itself165 if (rootProxy[PROXY_STATE].modified)166 throw new Error(RETURNED_AND_MODIFIED_ERROR)

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

lines 167–168why, one step back
167 result = finalize(returnValue)
168 } else result = finalize(rootProxy)

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

line 169why, one step back
169 // make sure all proxies become unusable

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

lines 170–172why, one step back
170 each(states, (_, state) => {
171 state.finished = true172 })

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 173why, one step back
173 return result

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

lines 174–175why, one step back
174 } finally {
175 states = prevStates

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 176–180why, one step back
176 }
177}178179function shallowEqual(objA, objB) {180 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 182–195why, one step back
182 if (
183 typeof objA !== "object" ||184 objA === null ||185 typeof objB !== "object" ||186 objB === null187 ) {188 return false189 }190 const keysA = Object.keys(objA)191 const keysB = Object.keys(objB)192 if (keysA.length !== keysB.length) return false193 for (let i = 0; i < keysA.length; i++) {194 if (195 !hasOwnProperty.call(objB, keysA[i]) ||

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 197–203why, one step back
197 ) {
198 return false199 }200 }201 return true202}203

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

lines 204–209why, one step back
204function createHiddenProperty(target, prop, value) {
205 Object.defineProperty(target, prop, {206 value: value,207 enumerable: false,208 writable: true209 })

transpile es5 codeMichel Weststratebefore adoptionf012154

line 210why, one step back
210}