Skip to content
Myr

es5.js

This file

Path

src/es5.js

198 lines · 5.6 KiB · JavaScript

Last changed by

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

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

line 10why, one step back
10 shallowCopy,

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

line 11why, one step back
11 RETURNED_AND_MODIFIED_ERROR,

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

lines 12–13why, one step back
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–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–51why, one step back
49function set(state, prop, value) {
50 assertUnfinished(state)51 if (!state.modified) {

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

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

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

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

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

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

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 58why, one step back
58

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

lines 59–64why, one step back
59function markChanged(state) {
60 if (!state.modified) {61 state.modified = true62 if (state.parent) markChanged(state.parent)63 }64}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 65why, one step back
65

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

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

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

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

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

line 70why, one step back
70}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 71why, one step back
71

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

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

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

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

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 83why, one step back
83

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 95why, one step back
95 }

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 99why, one step back
99

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

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

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

line 105why, one step back
105}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 106why, one step back
106

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

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

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

line 122why, one step back
122}

transpile es5 codeMichel Weststratebefore adoptionf012154

line 123why, one step back
123

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

line 129why, one step back
129

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

lines 130–131why, one step back
130function hasArrayChanges(state) {
131 return state.proxy.length !== state.base.length

transpile es5 codeMichel Weststratebefore adoptionf012154

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

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

lines 134–138why, one step back
134export function produceEs5(baseState, producer) {
135 const prevStates = states136 states = []137 try {138 // create proxy for root

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

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

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

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

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

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

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

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

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

lines 143–145why, one step back
143 each(states, (_, state) => {
144 state.finalizing = true145 })

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

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

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

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

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

line 148why, one step back
148 markChanges()

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

lines 149–156why, one step back
149 let result = finalize(rootProxy)
150 // check whether the draft was modified and/or a value was returned151 if (returnValue !== undefined && returnValue !== rootProxy) {152 // something was returned, and it wasn't the proxy itself153 if (rootProxy[PROXY_STATE].modified)154 throw new Error(RETURNED_AND_MODIFIED_ERROR)155 result = returnValue156 }

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

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

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

lines 158–160why, one step back
158 each(states, (_, state) => {
159 state.finished = true160 })

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

line 161why, one step back
161 return result

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

lines 162–163why, one step back
162 } finally {
163 states = prevStates

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 164–168why, one step back
164 }
165}166167function shallowEqual(objA, objB) {168 //From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 170–183why, one step back
170 if (
171 typeof objA !== "object" ||172 objA === null ||173 typeof objB !== "object" ||174 objB === null175 ) {176 return false177 }178 const keysA = Object.keys(objA)179 const keysB = Object.keys(objB)180 if (keysA.length !== keysB.length) return false181 for (let i = 0; i < keysA.length; i++) {182 if (183 !hasOwnProperty.call(objB, keysA[i]) ||

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

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

transpile es5 codeMichel Weststratebefore adoptionf012154

lines 185–191why, one step back
185 ) {
186 return false187 }188 }189 return true190}191

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

lines 192–197why, one step back
192function createHiddenProperty(target, prop, value) {
193 Object.defineProperty(target, prop, {194 value: value,195 enumerable: false,196 writable: true197 })

transpile es5 codeMichel Weststratebefore adoptionf012154

line 198why, one step back
198}