Skip to content
Myr

es5.js

This file

Path

src/es5.js

263 lines · 7.3 KiB · JavaScript

Last changed by

style: use tabsuse-tabsbefore adoptiona9c0fd0

History of this file

Take it

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 3–10why, one step back
3 each,
4 has,5 is,6 isDraft,7 isDraftable,8 isEnumerable,9 shallowCopy,10 DRAFT_STATE

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 18why, one step back
18export function willFinalize(scope, result, isReplaced) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 19–32why, one step back
19 scope.drafts.forEach(draft => {
20 draft[DRAFT_STATE].finalizing = true21 })22 if (!isReplaced) {23 if (scope.patches) {24 markChangesRecursively(scope.drafts[0])25 }26 // This is faster when we don't care about which attributes changed.27 markChangesSweep(scope.drafts)28 }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) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 36–40why, one step back
36 const isArray = Array.isArray(base)
37 const draft = clonePotentialDraft(base)38 each(draft, prop => {39 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))40 })

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 41why, one step back
41

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 42–56why, one step back
42 // See "proxy.js" for property documentation.
43 const scope = parent ? parent.scope : ImmerScope.current44 const state = {45 scope,46 modified: false,47 finalizing: false, // es5 only48 finalized: false,49 assigned: {},50 parent,51 base,52 draft,53 copy: null,54 revoke,55 revoked: false // es5 only56 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 57why, one step back
57

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 58–60why, one step back
58 createHiddenProperty(draft, DRAFT_STATE, state)
59 scope.drafts.push(draft)60 return draft

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 63why, one step back
63function revoke() {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 64why, one step back
64 this.revoked = true

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 65–66why, one step back
65}
66

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 71–72why, one step back
71// Access a property without creating an Immer draft.
72function peek(draft, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 73–80why, one step back
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]

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 81–82why, one step back
81}
82

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 84–92why, one step back
84 assertUnrevoked(state)
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)) {89 prepareCopy(state)90 return (state.copy[prop] = createProxy(value, state))91 }92 return value

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 93–95why, one step back
93}
9495function set(state, prop, value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 96–103why, one step back
96 assertUnrevoked(state)
97 state.assigned[prop] = true98 if (!state.modified) {99 if (is(value, peek(source(state), prop))) return100 markChanged(state)101 prepareCopy(state)102 }103 state.copy[prop] = value

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 104–106why, one step back
104}
105106function markChanged(state) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 107–110why, one step back
107 if (!state.modified) {
108 state.modified = true109 if (state.parent) markChanged(state.parent)110 }

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 111–113why, one step back
111}
112113function prepareCopy(state) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 114why, one step back
114 if (!state.copy) state.copy = clonePotentialDraft(state.base)

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

lines 115–117why, one step back
115}
116117function clonePotentialDraft(base) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 118–125why, one step back
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)

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 128why, one step back
128function proxyProperty(draft, prop, enumerable) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 129–144why, one step back
129 let desc = descriptors[prop]
130 if (desc) {131 desc.enumerable = enumerable132 } else {133 descriptors[prop] = desc = {134 configurable: true,135 enumerable,136 get() {137 return get(this[DRAFT_STATE], prop)138 },139 set(value) {140 set(this[DRAFT_STATE], prop, value)141 }142 }143 }144 Object.defineProperty(draft, prop, desc)

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 147why, one step back
147function assertUnrevoked(state) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 148–152why, one step back
148 if (state.revoked === true)
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? " +151 JSON.stringify(source(state))152 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 153–154why, one step back
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) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 157–168why, 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.161 for (let i = drafts.length - 1; i >= 0; i--) {162 const state = drafts[i][DRAFT_STATE]163 if (!state.modified) {164 if (Array.isArray(state.base)) {165 if (hasArrayChanges(state)) markChanged(state)166 } else if (hasObjectChanges(state)) markChanged(state)167 }168 }

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 169–171why, one step back
169}
170171function markChangesRecursively(object) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 172–208why, one step back
172 if (!object || typeof object !== "object") return
173 const state = object[DRAFT_STATE]174 if (!state) return175 const {base, draft, assigned} = state176 if (!Array.isArray(object)) {177 // Look for added keys.178 Object.keys(draft).forEach(key => {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.185 markChangesRecursively(draft[key])186 }187 })188 // Look for removed keys.189 Object.keys(base).forEach(key => {190 // The `undefined` check is a fast path for pre-existing keys.191 if (draft[key] === undefined && !has(draft, key)) {192 assigned[key] = false193 markChanged(state)194 }195 })196 } else if (hasArrayChanges(state)) {197 markChanged(state)198 assigned.length = true199 if (draft.length < base.length) {200 for (let i = draft.length; i < base.length; i++) assigned[i] = false201 } else {202 for (let i = base.length; i < draft.length; i++) assigned[i] = true203 }204 for (let i = 0; i < draft.length; i++) {205 // Only untouched indices trigger recursion.206 if (assigned[i] === undefined) markChangesRecursively(draft[i])207 }208 }

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 214–233why, 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.216 const keys = Object.keys(draft)217 for (let i = keys.length - 1; i >= 0; i--) {218 const key = keys[i]219 const baseValue = base[key]220 // The `undefined` check is a fast path for pre-existing keys.221 if (baseValue === undefined && !has(base, key)) {222 return true223 }224 // Once a base key is deleted, future changes go undetected, because its225 // 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 }233 }

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

line 234why, one step back
234

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 235–237why, one step back
235 // At this point, no keys were added or changed.
236 // Compare key count to determine if keys were deleted.237 return keys.length !== Object.keys(base).length

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 241–254why, one step back
241 const {draft} = state
242 if (draft.length !== state.base.length) return true243 // See #116244 // 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 one250 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)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 false

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 255–257why, one step back
255}
256257function createHiddenProperty(target, prop, value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 258–262why, one step back
258 Object.defineProperty(target, prop, {
259 value: value,260 enumerable: false,261 writable: true262 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 263why, one step back
263}