es5.js
This file
Path
src/es5.js
250 lines · 8.0 KiB · JavaScript
Last changed by
fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
History of this fileTake it
The walk stopped after 200 commits: lines older than 2897e12 point at it.
Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 1–2why, one step back1"use strict"2import {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 3–4why, one step back3 each,4 has,Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
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,Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 9why, one step back9 shallowCopy,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 10why, one step back10 DRAFT_STATEMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 11why, one step back11} from "./common"Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 12why, one step back12import {ImmerScope} from "./scope"Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
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 insteadMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
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,Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
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: {},Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
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 onlyMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
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 draftMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 61–62why, one step back61}62feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 63–66why, one step back63function revoke() {64 this.revoked = true65}66Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 67why, one step back67function source(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 68why, one step back68 return state.copy || state.baseMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 69–71why, one step back69}7071function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 72why, one step back72 assertUnrevoked(state)Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 73why, one step back73 const value = source(state)[prop]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 74–75why, one step back74 // Drafts are only created for proxyable values that exist in the base state.75 if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 76why, one step back76 prepareCopy(state)Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 77why, one step back77 return (state.copy[prop] = createProxy(value, state))Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 78–82why, one step back78 }79 return value80}8182function set(state, prop, value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 83why, one step back83 assertUnrevoked(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 84why, one step back84 state.assigned[prop] = trueMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 85–100why, one step back85 if (!state.modified) {86 if (is(source(state)[prop], value)) return87 markChanged(state)88 prepareCopy(state)89 }90 state.copy[prop] = value91}9293function markChanged(state) {94 if (!state.modified) {95 state.modified = true96 if (state.parent) markChanged(state.parent)97 }98}99100function prepareCopy(state) {fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425
lines 101–112why, one step back101 if (!state.copy) state.copy = clonePotentialDraft(state.base)102}103104function clonePotentialDraft(base) {105 const state = base && base[DRAFT_STATE]106 if (state) {107 state.finalizing = true108 const draft = shallowCopy(state.draft, true)109 state.finalizing = false110 return draft111 }112 return shallowCopy(base)Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 113–114why, one step back113}114feat: limited class supportaleclarsonbefore adoption9877d64
lines 115–120why, one step back115function proxyProperty(draft, prop, enumerable) {116 let desc = descriptors[prop]117 if (desc) {118 desc.enumerable = enumerable119 } else {120 descriptors[prop] = desc = {Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 121why, one step back121 configurable: true,feat: limited class supportaleclarsonbefore adoption9877d64
line 122why, one step back122 enumerable,Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 123why, one step back123 get() {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 124why, one step back124 return get(this[DRAFT_STATE], prop)Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 125–126why, one step back125 },126 set(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 127why, one step back127 set(this[DRAFT_STATE], prop, value)Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 128why, one step back128 }feat: limited class supportaleclarsonbefore adoption9877d64
lines 129–131why, one step back129 }130 }131 Object.defineProperty(draft, prop, desc)Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 132–133why, one step back132}133feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 134–135why, one step back134function assertUnrevoked(state) {135 if (state.revoked === true)Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 136–137why, one step back136 throw new Error(137 "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 138why, one step back138 JSON.stringify(source(state))Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 139–141why, one step back139 )140}141feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 142why, one step back142// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 143why, one step back143function markChangesSweep(drafts) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 144–147why, one step back144 // The natural order of drafts in the `scope` array is based on when they145 // were accessed. By processing drafts in reverse natural order, we have a146 // better chance of processing leaf nodes first. When a leaf node is known to147 // have changed, we can avoid any traversal of its ancestor nodes.Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 148–150why, one step back148 for (let i = drafts.length - 1; i >= 0; i--) {149 const state = drafts[i][DRAFT_STATE]150 if (!state.modified) {Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 151–159why, one step back151 if (Array.isArray(state.base)) {152 if (hasArrayChanges(state)) markChanged(state)153 } else if (hasObjectChanges(state)) markChanged(state)154 }155 }156}157158function markChangesRecursively(object) {159 if (!object || typeof object !== "object") returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 160why, one step back160 const state = object[DRAFT_STATE]Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 161why, one step back161 if (!state) returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 162why, one step back162 const {base, draft, assigned} = stateperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 163–164why, one step back163 if (!Array.isArray(object)) {164 // Look for added keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 165why, one step back165 Object.keys(draft).forEach(key => {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 166–171why, one step back166 // The `undefined` check is a fast path for pre-existing keys.167 if (base[key] === undefined && !has(base, key)) {168 assigned[key] = true169 markChanged(state)170 } else if (!assigned[key]) {171 // Only untouched properties trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 172why, one step back172 markChangesRecursively(draft[key])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 173why, one step back173 }Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 174why, one step back174 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 175–177why, one step back175 // Look for removed keys.176 Object.keys(base).forEach(key => {177 // The `undefined` check is a fast path for pre-existing keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 178why, one step back178 if (draft[key] === undefined && !has(draft, key)) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 179–181why, one step back179 assigned[key] = false180 markChanged(state)181 }Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
line 182why, one step back182 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 183–185why, one step back183 } else if (hasArrayChanges(state)) {184 markChanged(state)185 assigned.length = truefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 186–187why, one step back186 if (draft.length < base.length) {187 for (let i = draft.length; i < base.length; i++) assigned[i] = falseperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 188why, one step back188 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 189why, one step back189 for (let i = base.length; i < draft.length; i++) assigned[i] = trueperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 190why, one step back190 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 191why, one step back191 for (let i = 0; i < draft.length; i++) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 192why, one step back192 // Only untouched indices trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 193why, one step back193 if (assigned[i] === undefined) markChangesRecursively(draft[i])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 194why, one step back194 }Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 195–198why, one step back195 }196}197198function hasObjectChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 199why, one step back199 const {base, draft} = stateMerge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 200why, one step back200fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 201–202why, one step back201 // Search for added keys and changed keys. Start at the back, because202 // non-numeric keys are ordered by time of definition on the object.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 203why, one step back203 const keys = Object.keys(draft)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 204why, one step back204 for (let i = keys.length - 1; i >= 0; i--) {fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 205–206why, one step back205 const key = keys[i]206 const baseValue = base[key]Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 207why, one step back207 // The `undefined` check is a fast path for pre-existing keys.fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
line 208why, one step back208 if (baseValue === undefined && !has(base, key)) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 209–210why, one step back209 return true210 }fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 211–219why, one step back211 // Once a base key is deleted, future changes go undetected, because its212 // descriptor is erased. This branch detects any missed changes.213 else {214 const value = draft[key]215 const state = value && value[DRAFT_STATE]216 if (state ? state.base !== baseValue : !is(value, baseValue)) {217 return true218 }219 }Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 220–221why, one step back220 }221fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420
lines 222–223why, one step back222 // At this point, no keys were added or changed.223 // Compare key count to determine if keys were deleted.Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 224why, one step back224 return keys.length !== Object.keys(base).lengthMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 225–227why, one step back225}226227function hasArrayChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 228–229why, one step back228 const {draft} = state229 if (draft.length !== state.base.length) return trueMerge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 230–236why, one step back230 // See #116231 // If we first shorten the length, our array interceptors will be removed.232 // If after that new items are added, result in the same original length,233 // those last items will have no intercepting property.234 // So if there is no own descriptor on the last position, we know that items were removed and added235 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check236 // the last onefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 237why, one step back237 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)Merge pull request #181 from alecmce/patch-1Michel Weststratebefore adoption2897e12
lines 238–250why, one step back238 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)239 if (descriptor && !descriptor.get) return true240 // For all other cases, we don't have to compare, as they would have been picked up by the index setters241 return false242}243244function createHiddenProperty(target, prop, value) {245 Object.defineProperty(target, prop, {246 value: value,247 enumerable: false,248 writable: true249 })250}