Skip to content
Myr

es5.js

This file

Path

src/es5.js

270 lines · 8.7 KiB · JavaScript

Last changed by

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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–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–169why, one step back
126function markChangesRecursively(object) {
127 if (!object || typeof object !== "object") return128 const state = object[PROXY_STATE]129 if (!state) return130 const {proxy, base} = state131 if (Array.isArray(object)) {132 if (hasArrayChanges(state)) {133 markChanged(state)134 state.assigned.length = true135 if (proxy.length < base.length)136 for (let i = proxy.length; i < base.length; i++)137 state.assigned[i] = false138 else139 for (let i = base.length; i < proxy.length; i++)140 state.assigned[i] = true141 each(proxy, (index, child) => {142 if (!state.assigned[index]) markChangesRecursively(child)143 })144 }145 } else {146 const {added, removed} = diffKeys(base, proxy)147 if (added.length > 0 || removed.length > 0) markChanged(state)148 each(added, (_, key) => {149 state.assigned[key] = true150 })151 each(removed, (_, key) => {152 state.assigned[key] = false153 })154 each(proxy, (key, child) => {155 if (!state.assigned[key]) markChangesRecursively(child)156 })157 }158}159160function diffKeys(from, to) {161 // TODO: optimize162 const a = Object.keys(from)163 const b = Object.keys(to)164 return {165 added: b.filter(key => a.indexOf(key) === -1),166 removed: a.filter(key => b.indexOf(key) === -1)167 }168}169

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

lines 170–174why, one step back
170function hasObjectChanges(state) {
171 const baseKeys = Object.keys(state.base)172 const keys = Object.keys(state.proxy)173 return !shallowEqual(baseKeys, keys)174}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 175why, one step back
175

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

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

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

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

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

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

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 191–192why, one step back
191}
192

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

lines 194–198why, one step back
194 if (isProxy(baseState)) {
195 // See #100, don't nest producers196 const returnValue = producer.call(baseState, baseState)197 return returnValue === undefined ? baseState : returnValue198 }

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

lines 199–200why, one step back
199 const prevStates = states
200 states = []

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 201–202why, one step back
201 const patches = patchListener && []
202 const inversePatches = patchListener && []

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

lines 203–204why, one step back
203 try {
204 // create proxy for root

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

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

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

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

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

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

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

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

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

lines 209–211why, one step back
209 each(states, (_, state) => {
210 state.finalizing = true211 })

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

line 212why, one step back
212 let result

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

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

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

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

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

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

lines 229–231why, one step back
229 each(states, (_, state) => {
230 state.finished = true231 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

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

line 233why, one step back
233 return result

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

lines 234–235why, one step back
234 } finally {
235 states = prevStates

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 236–240why, one step back
236 }
237}238239function shallowEqual(objA, objB) {240 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 242–255why, one step back
242 if (
243 typeof objA !== "object" ||244 objA === null ||245 typeof objB !== "object" ||246 objB === null247 ) {248 return false249 }250 const keysA = Object.keys(objA)251 const keysB = Object.keys(objB)252 if (keysA.length !== keysB.length) return false253 for (let i = 0; i < keysA.length; i++) {254 if (255 !hasOwnProperty.call(objB, keysA[i]) ||

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 257–263why, one step back
257 ) {
258 return false259 }260 }261 return true262}263

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

lines 264–269why, one step back
264function createHiddenProperty(target, prop, value) {
265 Object.defineProperty(target, prop, {266 value: value,267 enumerable: false,268 writable: true269 })

transpile es5 codeMichel Weststratebefore adoptionf012154

line 270why, one step back
270}