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 fileTake 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 back1"use strict"2import {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 3–4why, one step back3 each,4 has,Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
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,Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 9why, one step back9 shallowCopy,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 10why, one step back10 DRAFT_STATEUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 11why, one step back11} from "./common"feat: support async recipesaleclarsonbefore adoption5fee518
line 12why, one step back12import {ImmerScope} from "./scope"Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 13why, one step back13Some additional comments, improvement in error handlingMichel Weststratebefore adoptioned6dfcc
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 insteadUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 16–17why, one step back16const descriptors = {}17feat: support async recipesaleclarsonbefore adoption5fee518
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.feat: support async recipesaleclarsonbefore adoption5fee518
line 27why, one step back27 markChangesSweep(scope.drafts)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 28–30why, one step back28 }29}30refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8
line 31why, one step back31export function createProxy(base, parent) {feat: limited class supportaleclarsonbefore adoption9877d64
line 32why, one step back32 const isArray = Array.isArray(base)fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425
line 33why, one step back33 const draft = clonePotentialDraft(base)nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 34why, one step back34 each(draft, prop => {fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425
line 35why, one step back35 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 36–37why, one step back36 })37feat: passing drafts to producealeclarsonbefore adoption9c53415
line 38why, one step back38 // See "proxy.js" for property documentation.feat: support async recipesaleclarsonbefore adoption5fee518
line 39why, one step back39 const scope = parent ? parent.scope : ImmerScope.currentfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 40why, one step back40 const state = {feat: support async recipesaleclarsonbefore adoption5fee518
line 41why, one step back41 scope,Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 42why, one step back42 modified: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 43why, one step back43 finalizing: false, // es5 onlyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 44why, one step back44 finalized: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 45why, one step back45 assigned: {},Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 46–47why, one step back46 parent,47 base,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 48–50why, one step back48 draft,49 copy: null,50 revoke,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 51why, one step back51 revoked: false // es5 onlyUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 52why, one step back52 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 53–54why, one step back5354 createHiddenProperty(draft, DRAFT_STATE, state)feat: support async recipesaleclarsonbefore adoption5fee518
line 55why, one step back55 scope.drafts.push(draft)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 56why, one step back56 return draftUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 57–58why, one step back57}58feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 59–62why, one step back59function revoke() {60 this.revoked = true61}62Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 63why, one step back63function source(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 64why, one step back64 return state.copy || state.baseUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 65–67why, one step back65}6667function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 68why, one step back68 assertUnrevoked(state)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 69why, one step back69 const value = source(state)[prop]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 70–71why, one step back70 // 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 back72 prepareCopy(state)refactor: rename createPublicDraft/finishPublicDraft for consistencyaleclarsonbefore adoption44ef0c8
line 73why, one step back73 return (state.copy[prop] = createProxy(value, state))Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 74–78why, one step back74 }75 return value76}7778function set(state, prop, value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 79why, one step back79 assertUnrevoked(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 80why, one step back80 state.assigned[prop] = trueUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 81–96why, one step back81 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 back97 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 back109}110feat: limited class supportaleclarsonbefore adoption9877d64
lines 111–116why, one step back111function 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 back117 configurable: true,feat: limited class supportaleclarsonbefore adoption9877d64
line 118why, one step back118 enumerable,Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 119why, one step back119 get() {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 120why, one step back120 return get(this[DRAFT_STATE], prop)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 121–122why, one step back121 },122 set(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 123why, one step back123 set(this[DRAFT_STATE], prop, value)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 124why, one step back124 }feat: limited class supportaleclarsonbefore adoption9877d64
lines 125–127why, one step back125 }126 }127 Object.defineProperty(draft, prop, desc)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 128–129why, one step back128}129feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 130–131why, one step back130function assertUnrevoked(state) {131 if (state.revoked === true)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 132why, one step back132 throw new Error(Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
line 133why, one step back133 "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 back134 JSON.stringify(source(state))Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 135–137why, one step back135 )136}137feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 138why, one step back138// 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 back139function markChangesSweep(drafts) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 140–143why, one step back140 // The natural order of drafts in the `scope` array is based on when they141 // 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 back144 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 back147 if (Array.isArray(state.base)) {148 if (hasArrayChanges(state)) markChanged(state)149 } else if (hasObjectChanges(state)) markChanged(state)150 }151 }152}153Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 154–155why, one step back154function markChangesRecursively(object) {155 if (!object || typeof object !== "object") returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 156why, one step back156 const state = object[DRAFT_STATE]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 157why, one step back157 if (!state) returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 158why, one step back158 const {base, draft, assigned} = stateperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 159–160why, one step back159 if (!Array.isArray(object)) {160 // Look for added keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 161why, one step back161 Object.keys(draft).forEach(key => {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 162–167why, one step back162 // 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 back168 markChangesRecursively(draft[key])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 169why, one step back169 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 170why, one step back170 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 171–173why, one step back171 // 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 back174 if (draft[key] === undefined && !has(draft, key)) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 175–177why, one step back175 assigned[key] = false176 markChanged(state)177 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 178why, one step back178 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 179–181why, one step back179 } else if (hasArrayChanges(state)) {180 markChanged(state)181 assigned.length = truefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 182–183why, one step back182 if (draft.length < base.length) {183 for (let i = draft.length; i < base.length; i++) assigned[i] = falseperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 184why, one step back184 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 185why, one step back185 for (let i = base.length; i < draft.length; i++) assigned[i] = trueperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 186why, one step back186 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 187why, one step back187 for (let i = 0; i < draft.length; i++) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 188why, one step back188 // Only untouched indices trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 189why, one step back189 if (assigned[i] === undefined) markChangesRecursively(draft[i])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 190why, one step back190 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 191–193why, one step back191 }192}193Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 194why, one step back194function hasObjectChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 195why, one step back195 const {base, draft} = stateMerge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 196–198why, one step back196197 // 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 back199 const keys = Object.keys(draft)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 200why, one step back200 for (let i = keys.length - 1; i >= 0; i--) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 201why, one step back201 // The `undefined` check is a fast path for pre-existing keys.nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 202why, one step back202 if (base[keys[i]] === undefined && !has(base, keys[i])) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 203–209why, one step back203 return true204 }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).lengthUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 210–212why, one step back210}211212function hasArrayChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 213–214why, one step back213 const {draft} = state214 if (draft.length !== state.base.length) return trueUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 215–221why, one step back215 // See #116216 // 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 onefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 222why, one step back222 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 223–235why, one step back223 // 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}