Skip to content
Myr

es5.js

This file

Path

src/es5.js

235 lines · 7.4 KiB · JavaScript

Last changed by

refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8

History of this file

Take it

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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,

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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,

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 9why, one step back
9 shallowCopy,

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

line 10why, one step back
10 DRAFT_STATE

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: support async recipesaleclarsonbefore adoption5fee518

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 13why, one step back
13

Some additional comments, improvement in error handlingMichel Weststratebefore adoptioned6dfcc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: support async recipesaleclarsonbefore adoption5fee518

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.

feat: support async recipesaleclarsonbefore adoption5fee518

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 28–30why, one step back
28 }
29}30

refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

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

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

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

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 36–37why, one step back
36 })
37

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: support async recipesaleclarsonbefore adoption5fee518

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: support async recipesaleclarsonbefore adoption5fee518

line 41why, one step back
41 scope,

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 46–47why, one step back
46 parent,
47 base,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 48–50why, one step back
48 draft,
49 copy: null,50 revoke,

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 52why, one step back
52 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 53–54why, one step back
53
54 createHiddenProperty(draft, DRAFT_STATE, state)

feat: support async recipesaleclarsonbefore adoption5fee518

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 56why, one step back
56 return draft

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 57–58why, one step back
57}
58

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 59–62why, one step back
59function revoke() {
60 this.revoked = true61}62

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 65–67why, one step back
65}
6667function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 74–78why, one step back
74 }
75 return value76}7778function set(state, prop, value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 81–96why, one step back
81 if (!state.modified) {
82 if (is(source(state)[prop], value)) return83 markChanged(state)84 prepareCopy(state)85 }86 state.copy[prop] = value87}8889function markChanged(state) {90 if (!state.modified) {91 state.modified = true92 if (state.parent) markChanged(state.parent)93 }94}9596function prepareCopy(state) {

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

lines 97–108why, one step back
97 if (!state.copy) state.copy = clonePotentialDraft(state.base)
98}99100function clonePotentialDraft(base) {101 const state = base && base[DRAFT_STATE]102 if (state) {103 state.finalizing = true104 const draft = shallowCopy(state.draft, true)105 state.finalizing = false106 return draft107 }108 return shallowCopy(base)

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 109–110why, one step back
109}
110

feat: limited class supportaleclarsonbefore adoption9877d64

lines 111–116why, one step back
111function proxyProperty(draft, prop, enumerable) {
112 let desc = descriptors[prop]113 if (desc) {114 desc.enumerable = enumerable115 } else {116 descriptors[prop] = desc = {

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 118why, one step back
118 enumerable,

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 121–122why, one step back
121 },
122 set(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 124why, one step back
124 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 125–127why, one step back
125 }
126 }127 Object.defineProperty(draft, prop, desc)

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 128–129why, one step back
128}
129

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 132why, one step back
132 throw new Error(

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

line 133why, one step back
133 "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 134why, one step back
134 JSON.stringify(source(state))

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 135–137why, one step back
135 )
136}137

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: support async recipesaleclarsonbefore adoption5fee518

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: support async recipesaleclarsonbefore adoption5fee518

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 147–153why, one step back
147 if (Array.isArray(state.base)) {
148 if (hasArrayChanges(state)) markChanged(state)149 } else if (hasObjectChanges(state)) markChanged(state)150 }151 }152}153

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 154–155why, one step back
154function markChangesRecursively(object) {
155 if (!object || typeof object !== "object") return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 169why, one step back
169 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 170why, one step back
170 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 175–177why, one step back
175 assigned[key] = false
176 markChanged(state)177 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 178why, one step back
178 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 179–181why, one step back
179 } else if (hasArrayChanges(state)) {
180 markChanged(state)181 assigned.length = true

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 184why, one step back
184 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 186why, one step back
186 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 190why, one step back
190 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 196–198why, one step back
196
197 // Search for added keys. Start at the back, because non-numeric keys198 // are ordered by time of definition on the object.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 203–209why, one step back
203 return true
204 }205 }206207 // Since no keys have been added, we can compare lengths to know if an208 // object has been deleted.209 return keys.length !== Object.keys(base).length

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 210–212why, one step back
210}
211212function hasArrayChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 223–235why, one step back
223 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
224 if (descriptor && !descriptor.get) return true225 // For all other cases, we don't have to compare, as they would have been picked up by the index setters226 return false227}228229function createHiddenProperty(target, prop, value) {230 Object.defineProperty(target, prop, {231 value: value,232 enumerable: false,233 writable: true234 })235}