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 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–4why, one step back1"use strict"2// @ts-check34import {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 5–6why, one step back5 each,6 has,Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 7why, one step back7 is,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 8–9why, one step back8 isDraft,9 isDraftable,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 10why, one step back10 isEnumerable,Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 11why, one step back11 shallowCopy,nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419
line 12why, one step back12 DRAFT_STATEUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 13–16why, one step back13} from "./common"1415const descriptors = {}16feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 17–31why, one step back17// 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 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: add `Immer` classaleclarsonbefore adoptionecd04dc
line 39why, one step back39 const state = {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 40why, one step back40 scope: parent ? parent.scope : currentScope(),Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 41why, one step back41 modified: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 42why, one step back42 finalizing: false, // es5 onlyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 43why, one step back43 finalized: false,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 44why, one step back44 assigned: {},Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 45–46why, one step back45 parent,46 base,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 47–49why, one step back47 draft,48 copy: null,49 revoke,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 50why, one step back50 revoked: false // es5 onlyUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 51why, one step back51 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 52–53why, one step back5253 createHiddenProperty(draft, DRAFT_STATE, state)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 54why, one step back54 state.scope.push(state)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 55why, one step back55 return draftUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 56–57why, one step back56}57feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 58–61why, one step back58function revoke() {59 this.revoked = true60}61Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 62why, one step back62function source(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 63why, one step back63 return state.copy || state.baseUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 64–66why, one step back64}6566function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 67why, one step back67 assertUnrevoked(state)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 68why, one step back68 const value = source(state)[prop]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 69–70why, one step back69 // 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 back71 prepareCopy(state)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 72why, one step back72 return (state.copy[prop] = createDraft(value, state))Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 73–77why, one step back73 }74 return value75}7677function set(state, prop, value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 78why, one step back78 assertUnrevoked(state)feat: passing drafts to producealeclarsonbefore adoption9c53415
line 79why, one step back79 state.assigned[prop] = trueUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 80–95why, one step back80 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 back96 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 back108}109feat: limited class supportaleclarsonbefore adoption9877d64
lines 110–115why, one step back110function 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 back116 configurable: true,feat: limited class supportaleclarsonbefore adoption9877d64
line 117why, one step back117 enumerable,Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 118why, one step back118 get() {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 119why, one step back119 return get(this[DRAFT_STATE], prop)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 120–121why, one step back120 },121 set(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 122why, one step back122 set(this[DRAFT_STATE], prop, value)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 123why, one step back123 }feat: limited class supportaleclarsonbefore adoption9877d64
lines 124–126why, one step back124 }125 }126 Object.defineProperty(draft, prop, desc)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 127–128why, one step back127}128feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 129–130why, one step back129function assertUnrevoked(state) {130 if (state.revoked === true)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 131why, one step back131 throw new Error(Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b
line 132why, one step back132 "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 back133 JSON.stringify(source(state))Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 134–136why, one step back134 )135}136feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 137–144why, one step back137// 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 back145 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}152Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 153–154why, one step back153function markChangesRecursively(object) {154 if (!object || typeof object !== "object") returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 155why, one step back155 const state = object[DRAFT_STATE]Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 156why, one step back156 if (!state) returnfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 157why, one step back157 const {base, draft, assigned} = stateperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 158–159why, one step back158 if (!Array.isArray(object)) {159 // Look for added keys.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 160why, one step back160 Object.keys(draft).forEach(key => {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 161–166why, one step back161 // 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 back167 markChangesRecursively(draft[key])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 168why, one step back168 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 169why, one step back169 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 170–172why, one step back170 // 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 back173 if (draft[key] === undefined && !has(draft, key)) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 174–176why, one step back174 assigned[key] = false175 markChanged(state)176 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
line 177why, one step back177 })perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
lines 178–180why, one step back178 } else if (hasArrayChanges(state)) {179 markChanged(state)180 assigned.length = truefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 181–182why, one step back181 if (draft.length < base.length) {182 for (let i = draft.length; i < base.length; i++) assigned[i] = falseperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 183why, one step back183 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 184why, one step back184 for (let i = base.length; i < draft.length; i++) assigned[i] = trueperf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 185why, one step back185 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 186why, one step back186 for (let i = 0; i < draft.length; i++) {perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 187why, one step back187 // Only untouched indices trigger recursion.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 188why, one step back188 if (assigned[i] === undefined) markChangesRecursively(draft[i])perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070
line 189why, one step back189 }Merge branch 'patches'Michel Weststratebefore adoption2513fb8
lines 190–192why, one step back190 }191}192Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
line 193why, one step back193function hasObjectChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 194why, one step back194 const {base, draft} = stateMerge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 195–197why, one step back195196 // 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 back198 const keys = Object.keys(draft)nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 199why, one step back199 for (let i = keys.length - 1; i >= 0; i--) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
line 200why, one step back200 // The `undefined` check is a fast path for pre-existing keys.nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374
line 201why, one step back201 if (base[keys[i]] === undefined && !has(base, keys[i])) {Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c
lines 202–208why, one step back202 return true203 }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).lengthUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 209–211why, one step back209}210211function hasArrayChanges(state) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 212–213why, one step back212 const {draft} = state213 if (draft.length !== state.base.length) return trueUse native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 214–220why, one step back214 // See #116215 // 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 onefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 221why, one step back221 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)Use native object.assign if possibleMichel Weststratebefore adoption5b8ed13
lines 222–234why, one step back222 // 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}