Skip to content
Myr

es5.js

This file

Path

src/es5.js

234 lines · 7.3 KiB · JavaScript

Last changed by

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

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–4why, one step back
1"use strict"
2// @ts-check34import {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 5–6why, one step back
5 each,
6 has,

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 7why, one step back
7 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 8–9why, one step back
8 isDraft,
9 isDraftable,

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

line 10why, one step back
10 isEnumerable,

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 11why, one step back
11 shallowCopy,

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

line 12why, one step back
12 DRAFT_STATE

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 13–16why, one step back
13} from "./common"
1415const descriptors = {}16

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 17–31why, one step back
17// For nested produce calls:
18export const scopes = []19export const currentScope = () => scopes[scopes.length - 1]2021export function willFinalize(result, baseDraft, needPatches) {22 const scope = currentScope()23 scope.forEach(state => (state.finalizing = true))24 if (result === undefined || result === baseDraft) {25 if (needPatches) markChangesRecursively(baseDraft)26 // This is faster when we don't care about which attributes changed.27 markChangesSweep(scope)28 }29}3031export function createDraft(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: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 40why, one step back
40 scope: parent ? parent.scope : currentScope(),

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 51why, one step back
51 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 54why, one step back
54 state.scope.push(state)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 55why, one step back
55 return draft

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 58–61why, one step back
58function revoke() {
59 this.revoked = true60}61

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 64–66why, one step back
64}
6566function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 73–77why, one step back
73 }
74 return value75}7677function set(state, prop, value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

lines 80–95why, one step back
80 if (!state.modified) {
81 if (is(source(state)[prop], value)) return82 markChanged(state)83 prepareCopy(state)84 }85 state.copy[prop] = value86}8788function markChanged(state) {89 if (!state.modified) {90 state.modified = true91 if (state.parent) markChanged(state.parent)92 }93}9495function prepareCopy(state) {

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

lines 96–107why, one step back
96 if (!state.copy) state.copy = clonePotentialDraft(state.base)
97}9899function clonePotentialDraft(base) {100 const state = base && base[DRAFT_STATE]101 if (state) {102 state.finalizing = true103 const draft = shallowCopy(state.draft, true)104 state.finalizing = false105 return draft106 }107 return shallowCopy(base)

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 117why, one step back
117 enumerable,

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

line 123why, one step back
123 }

feat: limited class supportaleclarsonbefore adoption9877d64

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

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

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 137–144why, one step back
137// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
138function markChangesSweep(scope) {139 // The natural order of drafts in the `scope` array is based on when they140 // were accessed. By processing drafts in reverse natural order, we have a141 // better chance of processing leaf nodes first. When a leaf node is known to142 // have changed, we can avoid any traversal of its ancestor nodes.143 for (let i = scope.length - 1; i >= 0; i--) {144 const state = scope[i]

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 168why, one step back
168 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 169why, one step back
169 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 177why, one step back
177 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 183why, one step back
183 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 185why, one step back
185 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 189why, one step back
189 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13

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