Skip to content
Myr

es5.js

This file

Path

src/es5.js

263 lines · 8.3 KiB · JavaScript

Last changed by

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

History of this file

Take it

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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,

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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,

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 9why, one step back
9 shallowCopy,

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

line 10why, one step back
10 DRAFT_STATE

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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,

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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: {},

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 69–70why, one step back
69}
70

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 71–82why, one step back
71// Access a property without creating an Immer draft.
72function peek(draft, prop) {73 const state = draft[DRAFT_STATE]74 if (state && !state.finalizing) {75 state.finalizing = true76 const value = draft[prop]77 state.finalizing = false78 return value79 }80 return draft[prop]81}82

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 83why, one step back
83function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 85–88why, one step back
85 const value = peek(source(state), prop)
86 if (state.finalizing) return value87 // Create a draft if the value is unmodified.88 if (value === peek(state.base, prop) && isDraftable(value)) {

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 91–95why, one step back
91 }
92 return value93}9495function set(state, prop, value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

line 99why, one step back
99 if (is(value, peek(source(state), prop))) return

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 100–113why, one step back
100 markChanged(state)
101 prepareCopy(state)102 }103 state.copy[prop] = value104}105106function markChanged(state) {107 if (!state.modified) {108 state.modified = true109 if (state.parent) markChanged(state.parent)110 }111}112113function prepareCopy(state) {

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

lines 114–125why, one step back
114 if (!state.copy) state.copy = clonePotentialDraft(state.base)
115}116117function clonePotentialDraft(base) {118 const state = base && base[DRAFT_STATE]119 if (state) {120 state.finalizing = true121 const draft = shallowCopy(state.draft, true)122 state.finalizing = false123 return draft124 }125 return shallowCopy(base)

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 126–127why, one step back
126}
127

feat: limited class supportaleclarsonbefore adoption9877d64

lines 128–133why, one step back
128function proxyProperty(draft, prop, enumerable) {
129 let desc = descriptors[prop]130 if (desc) {131 desc.enumerable = enumerable132 } else {133 descriptors[prop] = desc = {

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 135why, one step back
135 enumerable,

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 138–139why, one step back
138 },
139 set(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 141why, one step back
141 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 142–144why, one step back
142 }
143 }144 Object.defineProperty(draft, prop, desc)

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 149–150why, one step back
149 throw new Error(
150 "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 151why, one step back
151 JSON.stringify(source(state))

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 152–154why, one step back
152 )
153}154

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 164–172why, one step back
164 if (Array.isArray(state.base)) {
165 if (hasArrayChanges(state)) markChanged(state)166 } else if (hasObjectChanges(state)) markChanged(state)167 }168 }169}170171function markChangesRecursively(object) {172 if (!object || typeof object !== "object") return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 179–184why, one step back
179 // The `undefined` check is a fast path for pre-existing keys.
180 if (base[key] === undefined && !has(base, key)) {181 assigned[key] = true182 markChanged(state)183 } else if (!assigned[key]) {184 // Only untouched properties trigger recursion.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 186why, one step back
186 }

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 187why, one step back
187 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 192–194why, one step back
192 assigned[key] = false
193 markChanged(state)194 }

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

line 195why, one step back
195 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 196–198why, one step back
196 } else if (hasArrayChanges(state)) {
197 markChanged(state)198 assigned.length = true

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 201why, one step back
201 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 203why, one step back
203 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 207why, one step back
207 }

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 208–211why, one step back
208 }
209}210211function hasObjectChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

line 213why, one step back
213

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

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

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

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

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

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

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 222–223why, one step back
222 return true
223 }

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

lines 224–232why, one step back
224 // Once a base key is deleted, future changes go undetected, because its
225 // descriptor is erased. This branch detects any missed changes.226 else {227 const value = draft[key]228 const state = value && value[DRAFT_STATE]229 if (state ? state.base !== baseValue : !is(value, baseValue)) {230 return true231 }232 }

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 233–234why, one step back
233 }
234

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

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 238–240why, one step back
238}
239240function hasArrayChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 251–263why, one step back
251 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
252 if (descriptor && !descriptor.get) return true253 // For all other cases, we don't have to compare, as they would have been picked up by the index setters254 return false255}256257function createHiddenProperty(target, prop, value) {258 Object.defineProperty(target, prop, {259 value: value,260 enumerable: false,261 writable: true262 })263}