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 fileTake 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 back1"use strict"2import {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 3–4why, one step back3 each,4 has,Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 5why, one step back5 is,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 6–7why, one step back6 isDraft,7 isDraftable,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 8why, one step back8 isEnumerable,Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 9why, one step back9 shallowCopy,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 10why, one step back10 DRAFT_STATEAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 11why, one step back11} from "./common"Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 12why, one step back12import {ImmerScope} from "./scope"Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 13why, one step back13Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 14–15why, one step back14// property descriptors are recycled to make sure we don't create a get and set closure per property,15// but share them all insteadAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 16–17why, one step back16const descriptors = {}17Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 18–25why, one step back18export 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 back26 // This is faster when we don't care about which attributes changed.Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 27why, one step back27 markChangesSweep(scope.drafts)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 28why, one step back28 }fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db
lines 29–32why, one step back29 // 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 back33}34Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 35why, one step back35export function createProxy(base, parent) {feat: limited class supportaleclarsonbefore adoption9877d64
line 36why, one step back36 const isArray = Array.isArray(base)fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425
line 37why, one step back37 const draft = clonePotentialDraft(base)nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 38why, one step back38 each(draft, prop => {fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425
line 39why, one step back39 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 40–41why, one step back40 })41feat: passing drafts to producealeclarsonbefore adoption9c53415
line 42why, one step back42 // See "proxy.js" for property documentation.Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 43why, one step back43 const scope = parent ? parent.scope : ImmerScope.currentfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 44why, one step back44 const state = {Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 45why, one step back45 scope,Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 46why, one step back46 modified: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 47why, one step back47 finalizing: false, // es5 onlyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 48why, one step back48 finalized: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 49why, one step back49 assigned: {},Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 50–51why, one step back50 parent,51 base,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 52–54why, one step back52 draft,53 copy: null,54 revoke,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 55why, one step back55 revoked: false // es5 onlyAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 56why, one step back56 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 57–58why, one step back5758 createHiddenProperty(draft, DRAFT_STATE, state)Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 59why, one step back59 scope.drafts.push(draft)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 60why, one step back60 return draftAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 61–62why, one step back61}62feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 63–66why, one step back63function revoke() {64 this.revoked = true65}66Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 67why, one step back67function source(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 68why, one step back68 return state.copy || state.baseAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 69–70why, one step back69}70fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
lines 71–82why, one step back71// 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}82Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 83why, one step back83function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 84why, one step back84 assertUnrevoked(state)fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
lines 85–88why, one step back85 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 back89 prepareCopy(state)Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 90why, one step back90 return (state.copy[prop] = createProxy(value, state))Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 91–95why, one step back91 }92 return value93}9495function set(state, prop, value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 96why, one step back96 assertUnrevoked(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 97why, one step back97 state.assigned[prop] = trueAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
line 98why, one step back98 if (!state.modified) {fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
line 99why, one step back99 if (is(value, peek(source(state), prop))) returnAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 100–113why, one step back100 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 back114 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 back126}127feat: limited class supportaleclarsonbefore adoption9877d64
lines 128–133why, one step back128function 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 back134 configurable: true,feat: limited class supportaleclarsonbefore adoption9877d64
line 135why, one step back135 enumerable,Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 136why, one step back136 get() {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 137why, one step back137 return get(this[DRAFT_STATE], prop)Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 138–139why, one step back138 },139 set(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 140why, one step back140 set(this[DRAFT_STATE], prop, value)Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 141why, one step back141 }feat: limited class supportaleclarsonbefore adoption9877d64
lines 142–144why, one step back142 }143 }144 Object.defineProperty(draft, prop, desc)Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 145–146why, one step back145}146feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 147–148why, one step back147function assertUnrevoked(state) {148 if (state.revoked === true)Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 149–150why, one step back149 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 back151 JSON.stringify(source(state))Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 152–154why, one step back152 )153}154feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 155why, one step back155// 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 back156function markChangesSweep(drafts) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 157–160why, one step back157 // The natural order of drafts in the `scope` array is based on when they158 // 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 back161 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 back164 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") returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 173why, one step back173 const state = object[DRAFT_STATE]Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 174why, one step back174 if (!state) returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 175why, one step back175 const {base, draft, assigned} = stateperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 176–177why, one step back176 if (!Array.isArray(object)) {177 // Look for added keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 178why, one step back178 Object.keys(draft).forEach(key => {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 179–184why, one step back179 // 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 back185 markChangesRecursively(draft[key])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 186why, one step back186 }Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 187why, one step back187 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 188–190why, one step back188 // 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 back191 if (draft[key] === undefined && !has(draft, key)) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 192–194why, one step back192 assigned[key] = false193 markChanged(state)194 }Added some external resources to readmeMichel Weststratebefore adoptionc018c49
line 195why, one step back195 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 196–198why, one step back196 } else if (hasArrayChanges(state)) {197 markChanged(state)198 assigned.length = truefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 199–200why, one step back199 if (draft.length < base.length) {200 for (let i = draft.length; i < base.length; i++) assigned[i] = falseperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 201why, one step back201 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 202why, one step back202 for (let i = base.length; i < draft.length; i++) assigned[i] = trueperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 203why, one step back203 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 204why, one step back204 for (let i = 0; i < draft.length; i++) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 205why, one step back205 // Only untouched indices trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 206why, one step back206 if (assigned[i] === undefined) markChangesRecursively(draft[i])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 207why, one step back207 }Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 208–211why, one step back208 }209}210211function hasObjectChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 212why, one step back212 const {base, draft} = stateMerge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 213why, one step back213fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 214–215why, one step back214 // Search for added keys and changed keys. Start at the back, because215 // non-numeric keys are ordered by time of definition on the object.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 216why, one step back216 const keys = Object.keys(draft)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 217why, one step back217 for (let i = keys.length - 1; i >= 0; i--) {fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 218–219why, one step back218 const key = keys[i]219 const baseValue = base[key]Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 220why, one step back220 // 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 back221 if (baseValue === undefined && !has(base, key)) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 222–223why, one step back222 return true223 }fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 224–232why, one step back224 // 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 }Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 233–234why, one step back233 }234fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 235–236why, one step back235 // 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 back237 return keys.length !== Object.keys(base).lengthAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 238–240why, one step back238}239240function hasArrayChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 241–242why, one step back241 const {draft} = state242 if (draft.length !== state.base.length) return trueAdded some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 243–249why, one step back243 // 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 onefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 250why, one step back250 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)Added some external resources to readmeMichel Weststratebefore adoptionc018c49
lines 251–263why, one step back251 // 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}