Skip to content
Myr

es5.js

This file

Path

src/es5.js

250 lines · 8.0 KiB · JavaScript

Last changed by

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

History of this file

Take it

The walk stopped after 200 commits: lines older than 2897e12 point at it.

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 1–2why, one step back
1"use strict"
2import {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 3–4why, one step back
3 each,
4 has,

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 5why, one step back
5 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 6–7why, one step back
6 isDraft,
7 isDraftable,

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 8why, one step back
8 isEnumerable,

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 9why, one step back
9 shallowCopy,

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 10why, one step back
10 DRAFT_STATE

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 12why, one step back
12import {ImmerScope} from "./scope"

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 13why, one step back
13

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 14–15why, one step back
14// property descriptors are recycled to make sure we don't create a get and set closure per property,
15// but share them all instead

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 18–25why, one step back
18export function willFinalize(scope, result, isReplaced) {
19 scope.drafts.forEach(draft => {20 draft[DRAFT_STATE].finalizing = true21 })22 if (!isReplaced) {23 if (scope.patches) {24 markChangesRecursively(scope.drafts[0])25 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 26why, one step back
26 // This is faster when we don't care about which attributes changed.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 27why, one step back
27 markChangesSweep(scope.drafts)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 28why, one step back
28 }

fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db

lines 29–32why, one step back
29 // When a child draft is returned, look for changes.
30 else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {31 markChangesSweep(scope.drafts)32 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 33–34why, one step back
33}
34

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 35why, one step back
35export function createProxy(base, parent) {

feat: limited class supportaleclarsonbefore adoption9877d64

line 36why, one step back
36 const isArray = Array.isArray(base)

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

line 37why, one step back
37 const draft = clonePotentialDraft(base)

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 38why, one step back
38 each(draft, prop => {

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

line 39why, one step back
39 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 40–41why, one step back
40 })
41

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 42why, one step back
42 // See "proxy.js" for property documentation.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 43why, one step back
43 const scope = parent ? parent.scope : ImmerScope.current

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 44why, one step back
44 const state = {

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 45why, one step back
45 scope,

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 46why, one step back
46 modified: false,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 47why, one step back
47 finalizing: false, // es5 only

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 48why, one step back
48 finalized: false,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 49why, one step back
49 assigned: {},

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 50–51why, one step back
50 parent,
51 base,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 52–54why, one step back
52 draft,
53 copy: null,54 revoke,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 55why, one step back
55 revoked: false // es5 only

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 56why, one step back
56 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 57–58why, one step back
57
58 createHiddenProperty(draft, DRAFT_STATE, state)

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 59why, one step back
59 scope.drafts.push(draft)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 60why, one step back
60 return draft

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 61–62why, one step back
61}
62

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 63–66why, one step back
63function revoke() {
64 this.revoked = true65}66

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 67why, one step back
67function source(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 68why, one step back
68 return state.copy || state.base

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 69–71why, one step back
69}
7071function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 72why, one step back
72 assertUnrevoked(state)

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 73why, one step back
73 const value = source(state)[prop]

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 74–75why, one step back
74 // Drafts are only created for proxyable values that exist in the base state.
75 if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 77why, one step back
77 return (state.copy[prop] = createProxy(value, state))

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 78–82why, one step back
78 }
79 return value80}8182function set(state, prop, value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 83why, one step back
83 assertUnrevoked(state)

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 84why, one step back
84 state.assigned[prop] = true

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 85–100why, one step back
85 if (!state.modified) {
86 if (is(source(state)[prop], value)) return87 markChanged(state)88 prepareCopy(state)89 }90 state.copy[prop] = value91}9293function markChanged(state) {94 if (!state.modified) {95 state.modified = true96 if (state.parent) markChanged(state.parent)97 }98}99100function prepareCopy(state) {

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

lines 101–112why, one step back
101 if (!state.copy) state.copy = clonePotentialDraft(state.base)
102}103104function clonePotentialDraft(base) {105 const state = base && base[DRAFT_STATE]106 if (state) {107 state.finalizing = true108 const draft = shallowCopy(state.draft, true)109 state.finalizing = false110 return draft111 }112 return shallowCopy(base)

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 113–114why, one step back
113}
114

feat: limited class supportaleclarsonbefore adoption9877d64

lines 115–120why, one step back
115function proxyProperty(draft, prop, enumerable) {
116 let desc = descriptors[prop]117 if (desc) {118 desc.enumerable = enumerable119 } else {120 descriptors[prop] = desc = {

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 121why, one step back
121 configurable: true,

feat: limited class supportaleclarsonbefore adoption9877d64

line 122why, one step back
122 enumerable,

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 123why, one step back
123 get() {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 124why, one step back
124 return get(this[DRAFT_STATE], prop)

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 125–126why, one step back
125 },
126 set(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 127why, one step back
127 set(this[DRAFT_STATE], prop, value)

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 128why, one step back
128 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 129–131why, one step back
129 }
130 }131 Object.defineProperty(draft, prop, desc)

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 134–135why, one step back
134function assertUnrevoked(state) {
135 if (state.revoked === true)

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 136–137why, one step back
136 throw new Error(
137 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +

test: improve coveragealeclarsonbefore adoption14d7e3c

line 138why, one step back
138 JSON.stringify(source(state))

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 139–141why, one step back
139 )
140}141

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 142why, one step back
142// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 143why, one step back
143function markChangesSweep(drafts) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 144–147why, one step back
144 // The natural order of drafts in the `scope` array is based on when they
145 // were accessed. By processing drafts in reverse natural order, we have a146 // better chance of processing leaf nodes first. When a leaf node is known to147 // have changed, we can avoid any traversal of its ancestor nodes.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 148–150why, one step back
148 for (let i = drafts.length - 1; i >= 0; i--) {
149 const state = drafts[i][DRAFT_STATE]150 if (!state.modified) {

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 151–159why, one step back
151 if (Array.isArray(state.base)) {
152 if (hasArrayChanges(state)) markChanged(state)153 } else if (hasObjectChanges(state)) markChanged(state)154 }155 }156}157158function markChangesRecursively(object) {159 if (!object || typeof object !== "object") return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 160why, one step back
160 const state = object[DRAFT_STATE]

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 161why, one step back
161 if (!state) return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 162why, one step back
162 const {base, draft, assigned} = state

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 163–164why, one step back
163 if (!Array.isArray(object)) {
164 // Look for added keys.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 165why, one step back
165 Object.keys(draft).forEach(key => {

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 166–171why, one step back
166 // The `undefined` check is a fast path for pre-existing keys.
167 if (base[key] === undefined && !has(base, key)) {168 assigned[key] = true169 markChanged(state)170 } else if (!assigned[key]) {171 // Only untouched properties trigger recursion.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 172why, one step back
172 markChangesRecursively(draft[key])

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 173why, one step back
173 }

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 174why, one step back
174 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 175–177why, one step back
175 // Look for removed keys.
176 Object.keys(base).forEach(key => {177 // The `undefined` check is a fast path for pre-existing keys.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 178why, one step back
178 if (draft[key] === undefined && !has(draft, key)) {

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 179–181why, one step back
179 assigned[key] = false
180 markChanged(state)181 }

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

line 182why, one step back
182 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 183–185why, one step back
183 } else if (hasArrayChanges(state)) {
184 markChanged(state)185 assigned.length = true

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 186–187why, one step back
186 if (draft.length < base.length) {
187 for (let i = draft.length; i < base.length; i++) assigned[i] = false

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 188why, one step back
188 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 189why, one step back
189 for (let i = base.length; i < draft.length; i++) assigned[i] = true

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 190why, one step back
190 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 191why, one step back
191 for (let i = 0; i < draft.length; i++) {

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 192why, one step back
192 // Only untouched indices trigger recursion.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 193why, one step back
193 if (assigned[i] === undefined) markChangesRecursively(draft[i])

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 194why, one step back
194 }

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 195–198why, one step back
195 }
196}197198function hasObjectChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 199why, one step back
199 const {base, draft} = state

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

line 200why, one step back
200

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

lines 201–202why, one step back
201 // Search for added keys and changed keys. Start at the back, because
202 // non-numeric keys are ordered by time of definition on the object.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 203why, one step back
203 const keys = Object.keys(draft)

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

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

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

lines 205–206why, one step back
205 const key = keys[i]
206 const baseValue = base[key]

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

line 208why, one step back
208 if (baseValue === undefined && !has(base, key)) {

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 209–210why, one step back
209 return true
210 }

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

lines 211–219why, one step back
211 // Once a base key is deleted, future changes go undetected, because its
212 // descriptor is erased. This branch detects any missed changes.213 else {214 const value = draft[key]215 const state = value && value[DRAFT_STATE]216 if (state ? state.base !== baseValue : !is(value, baseValue)) {217 return true218 }219 }

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 220–221why, one step back
220 }
221

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

lines 222–223why, one step back
222 // At this point, no keys were added or changed.
223 // Compare key count to determine if keys were deleted.

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

line 224why, one step back
224 return keys.length !== Object.keys(base).length

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 225–227why, one step back
225}
226227function hasArrayChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 228–229why, one step back
228 const {draft} = state
229 if (draft.length !== state.base.length) return true

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 230–236why, one step back
230 // See #116
231 // If we first shorten the length, our array interceptors will be removed.232 // If after that new items are added, result in the same original length,233 // those last items will have no intercepting property.234 // So if there is no own descriptor on the last position, we know that items were removed and added235 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check236 // the last one

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12

lines 238–250why, one step back
238 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
239 if (descriptor && !descriptor.get) return true240 // For all other cases, we don't have to compare, as they would have been picked up by the index setters241 return false242}243244function createHiddenProperty(target, prop, value) {245 Object.defineProperty(target, prop, {246 value: value,247 enumerable: false,248 writable: true249 })250}