Skip to content
Myr

es5.js

This file

Path

src/es5.js

248 lines · 8.4 KiB · JavaScript

Last changed by

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

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

line 8why, one step back
8 shallowCopy,

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

line 9why, one step back
9 RETURNED_AND_MODIFIED_ERROR,

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

line 10why, one step back
10 has,

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–20why, one step back
18function createState(parent, proxy, base) {
19 return {20 modified: false,

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 21why, one step back
21 assigned: {}, // true: value was assigned to these props, false: was removed

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

lines 22–27why, one step back
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–39why, 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]

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

lines 40–42why, one step back
40 if (!state.finalizing && value === state.base[prop] && isProxyable(value)) {
41 // only create a proxy if the value is proxyable, and the value was in the base state42 // 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 43–44why, one step back
43 prepareCopy(state)
44 return (state.copy[prop] = createProxy(state, value))

transpile es5 codeMichel Weststratebefore adoptionf012154

line 45why, one step back
45 }

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 48why, one step back
48

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

lines 49–50why, one step back
49function set(state, prop, value) {
50 assertUnfinished(state)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 51why, one step back
51 state.assigned[prop] = true // optimization; skip this if there is no listener

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

line 52why, one step back
52 if (!state.modified) {

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

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

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

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

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

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

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

lines 56–58why, one step back
56 }
57 state.copy[prop] = value58}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 59why, one step back
59

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

lines 60–65why, one step back
60function markChanged(state) {
61 if (!state.modified) {62 state.modified = true63 if (state.parent) markChanged(state.parent)64 }65}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 66why, one step back
66

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

lines 67–69why, one step back
67function prepareCopy(state) {
68 if (state.hasCopy) return69 state.hasCopy = true

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

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

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

line 71why, one step back
71}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 72why, one step back
72

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

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

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

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

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 84why, one step back
84

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 96why, one step back
96 }

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

lines 97–99why, one step back
97 })
98 )99}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 100why, one step back
100

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

lines 101–103why, one step back
101function assertUnfinished(state) {
102 if (state.finished === true)103 throw new Error(

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 104–105why, one step back
104 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
105 JSON.stringify(state.copy || state.base)

transpile es5 codeMichel Weststratebefore adoptionf012154

line 106why, one step back
106 )

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

line 107why, one step back
107}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 108why, one step back
108

Merge pull request #135 from btnwtn/patch-1benbraoubefore adoption6e60eb5

line 109why, one step back
109// this sounds very expensive, but actually it is not that expensive in practice

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

lines 110–111why, one step back
110// as it will only visit proxies, and only do key-based change detection for objects for
111// which it is not already know that they are changed (that is, only object for which no known key was changed)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 112why, one step back
112function markChangesSweep() {

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

lines 113–121why, one step back
113 // intentionally we process the proxies in reverse order;
114 // ideally we start by processing leafs in the tree, because if a child has changed, we don't have to check the parent anymore115 // reverse order of proxy creation approximates this116 for (let i = states.length - 1; i >= 0; i--) {117 const state = states[i]118 if (state.modified === false) {119 if (Array.isArray(state.base)) {120 if (hasArrayChanges(state)) markChanged(state)121 } else if (hasObjectChanges(state)) markChanged(state)

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 122–123why, one step back
122 }
123 }

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

line 124why, one step back
124}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 125why, one step back
125

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 126–129why, one step back
126function markChangesRecursively(object) {
127 if (!object || typeof object !== "object") return128 const state = object[PROXY_STATE]129 if (!state) return

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 130–141why, one step back
130 const {proxy, base, assigned} = state
131 if (!Array.isArray(object)) {132 // Look for added keys.133 Object.keys(proxy).forEach(key => {134 // The `undefined` check is a fast path for pre-existing keys.135 if (base[key] === undefined && !has(base, key)) {136 assigned[key] = true137 markChanged(state)138 } else if (!assigned[key]) {139 // Only untouched properties trigger recursion.140 markChangesRecursively(proxy[key])141 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 142why, one step back
142 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 143–149why, one step back
143 // Look for removed keys.
144 Object.keys(base).forEach(key => {145 // The `undefined` check is a fast path for pre-existing keys.146 if (proxy[key] === undefined && !has(proxy, key)) {147 assigned[key] = false148 markChanged(state)149 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 150why, one step back
150 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 151–162why, one step back
151 } else if (hasArrayChanges(state)) {
152 markChanged(state)153 assigned.length = true154 if (proxy.length < base.length) {155 for (let i = proxy.length; i < base.length; i++) assigned[i] = false156 } else {157 for (let i = base.length; i < proxy.length; i++) assigned[i] = true158 }159 for (let i = 0; i < proxy.length; i++) {160 // Only untouched indices trigger recursion.161 if (assigned[i] === undefined) markChangesRecursively(proxy[i])162 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

line 166why, one step back
166function hasObjectChanges(state) {

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 167–171why, one step back
167 const {base, proxy} = state
168169 // Search for added keys. Start at the back, because non-numeric keys170 // are ordered by time of definition on the object.171 const keys = Object.keys(proxy)

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

line 172why, one step back
172 for (let i = keys.length - 1; i >= 0; i--) {

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

line 173why, one step back
173 // The `undefined` check is a fast path for pre-existing keys.

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

line 174why, one step back
174 if (base[keys[i]] === undefined && !has(base, keys[i])) {

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 175–181why, one step back
175 return true
176 }177 }178179 // Since no keys have been added, we can compare lengths to know if an180 // object has been deleted.181 return keys.length !== Object.keys(base).length

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

line 182why, one step back
182}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 183why, one step back
183

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

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

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

lines 185–191why, one step back
185 const {proxy} = state
186 if (proxy.length !== state.base.length) return true187 // See #116188 // If we first shorten the length, our array interceptors will be removed.189 // If after that new items are added, result in the same original length,190 // those last items will have no intercepting property.191 // 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 192–193why, one step back
192 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
193 // the last one

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

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

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

line 195why, one step back
195 // 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 196–198why, one step back
196 if (descriptor && !descriptor.get) return true
197 // For all other cases, we don't have to compare, as they would have been picked up by the index setters198 return false

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 199–200why, one step back
199}
200

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

lines 202–203why, one step back
202 const prevStates = states
203 states = []

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 204–205why, one step back
204 const patches = patchListener && []
205 const inversePatches = patchListener && []

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

lines 206–207why, one step back
206 try {
207 // create proxy for root

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

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

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

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

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

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

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

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

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

lines 212–214why, one step back
212 each(states, (_, state) => {
213 state.finalizing = true214 })

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

line 215why, one step back
215 let result

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

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

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

line 221why, one step back
221 result = finalize(returnValue)

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 222–230why, one step back
222 if (patches) {
223 patches.push({op: "replace", path: [], value: result})224 inversePatches.push({op: "replace", path: [], value: baseState})225 }226 } else {227 if (patchListener) markChangesRecursively(rootProxy)228 markChangesSweep() // this one is more efficient if we don't need to know which attributes have changed229 result = finalize(rootProxy, [], patches, inversePatches)230 }

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

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

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

lines 232–234why, one step back
232 each(states, (_, state) => {
233 state.finished = true234 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 235why, one step back
235 patchListener && patchListener(patches, inversePatches)

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

line 236why, one step back
236 return result

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

lines 237–238why, one step back
237 } finally {
238 states = prevStates

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 239–241why, one step back
239 }
240}241

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

lines 242–247why, one step back
242function createHiddenProperty(target, prop, value) {
243 Object.defineProperty(target, prop, {244 value: value,245 enumerable: false,246 writable: true247 })

transpile es5 codeMichel Weststratebefore adoptionf012154

line 248why, one step back
248}