es5.ts
This file
Path
src/plugins/es5.ts
277 lines · 8.3 KiB · TypeScript
Last changed by
fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–13why, one step back1import {2 ImmerState,3 Drafted,4 ES5ArrayState,5 ES5ObjectState,6 each,7 has,8 isDraft,9 latest,10 DRAFT_STATE,11 is,12 loadPlugin,13 ImmerScope,chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 14why, one step back14 ProxyType,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 15why, one step back15 getCurrentScope,Immer 7Michel Weststratebefore adoption467ea5d
lines 16–17why, one step back16 die,17 markChanged,fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
line 18why, one step back18 objectTraps,fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2
lines 19–20why, one step back19 ownKeys,20 getOwnPropertyDescriptorsMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 21–46why, one step back21} from "../internal"2223type ES5State = ES5ArrayState | ES5ObjectState2425export function enableES5() {26 function willFinalizeES5_(27 scope: ImmerScope,28 result: any,29 isReplaced: boolean30 ) {31 if (!isReplaced) {32 if (scope.patches_) {33 markChangesRecursively(scope.drafts_![0])34 }35 // This is faster when we don't care about which attributes changed.36 markChangesSweep(scope.drafts_)37 }38 // When a child draft is returned, look for changes.39 else if (40 isDraft(result) &&41 (result[DRAFT_STATE] as ES5State).scope_ === scope42 ) {43 markChangesSweep(scope.drafts_)44 }45 }46Immer 7Michel Weststratebefore adoption467ea5d
lines 47–49why, one step back47 function createES5Draft(isArray: boolean, base: any) {48 if (isArray) {49 const draft = new Array(base.length)fix: out of range assignments were broken in ES5 mode. Fixes #638Michel Weststratebefore adoption0fe9132
lines 50–51why, one step back50 for (let i = 0; i < base.length; i++)51 Object.defineProperty(draft, "" + i, proxyProperty(i, true))Immer 7Michel Weststratebefore adoption467ea5d
lines 52–53why, one step back52 return draft53 } else {fix: out of range assignments were broken in ES5 mode. Fixes #638Michel Weststratebefore adoption0fe9132
lines 54–63why, one step back54 const descriptors = getOwnPropertyDescriptors(base)55 delete descriptors[DRAFT_STATE as any]56 const keys = ownKeys(descriptors)57 for (let i = 0; i < keys.length; i++) {58 const key: any = keys[i]59 descriptors[key] = proxyProperty(60 key,61 isArray || !!descriptors[key].enumerable62 )63 }Immer 7Michel Weststratebefore adoption467ea5d
lines 64–67why, one step back64 return Object.create(Object.getPrototypeOf(base), descriptors)65 }66 }67Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 68–72why, one step back68 function createES5Proxy_<T>(69 base: T,70 parent?: ImmerState71 ): Drafted<T, ES5ObjectState | ES5ArrayState> {72 const isArray = Array.isArray(base)Immer 7Michel Weststratebefore adoption467ea5d
line 73why, one step back73 const draft = createES5Draft(isArray, base)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 74–75why, one step back7475 const state: ES5ObjectState | ES5ArrayState = {chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 76why, one step back76 type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 77–81why, one step back77 scope_: parent ? parent.scope_ : getCurrentScope(),78 modified_: false,79 finalized_: false,80 assigned_: {},81 parent_: parent,Immer 7Michel Weststratebefore adoption467ea5d
line 82why, one step back82 // base is the object we are draftingMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 83why, one step back83 base_: base,Immer 7Michel Weststratebefore adoption467ea5d
line 84why, one step back84 // draft is the draft object itself, that traps all reads and reads from either the base (if unmodified) or copy (if modified)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 85–105why, one step back85 draft_: draft,86 copy_: null,87 revoked_: false,88 isManual_: false89 }9091 Object.defineProperty(draft, DRAFT_STATE, {92 value: state,93 // enumerable: false <- the default94 writable: true95 })96 return draft97 }9899 // property descriptors are recycled to make sure we don't create a get and set closure per property,100 // but share them all instead101 const descriptors: {[prop: string]: PropertyDescriptor} = {}102103 function proxyProperty(104 prop: string | number,105 enumerable: booleanImmer 7Michel Weststratebefore adoption467ea5d
line 106why, one step back106 ): PropertyDescriptor {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 107–111why, one step back107 let desc = descriptors[prop]108 if (desc) {109 desc.enumerable = enumerable110 } else {111 descriptors[prop] = desc = {Immer 7Michel Weststratebefore adoption467ea5d
line 112why, one step back112 configurable: true,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 113–114why, one step back113 enumerable,114 get(this: any) {Immer 7Michel Weststratebefore adoption467ea5d
lines 115–118why, one step back115 const state = this[DRAFT_STATE]116 if (__DEV__) assertUnrevoked(state)117 // @ts-ignore118 return objectTraps.get(state, prop)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 119–120why, one step back119 },120 set(this: any, value) {Immer 7Michel Weststratebefore adoption467ea5d
lines 121–124why, one step back121 const state = this[DRAFT_STATE]122 if (__DEV__) assertUnrevoked(state)123 // @ts-ignore124 objectTraps.set(state, prop, value)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 125–127why, one step back125 }126 }127 }Immer 7Michel Weststratebefore adoption467ea5d
line 128why, one step back128 return descMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 129–140why, one step back129 }130131 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.132 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {133 // The natural order of drafts in the `scope` array is based on when they134 // were accessed. By processing drafts in reverse natural order, we have a135 // better chance of processing leaf nodes first. When a leaf node is known to136 // have changed, we can avoid any traversal of its ancestor nodes.137 for (let i = drafts.length - 1; i >= 0; i--) {138 const state: ES5State = drafts[i][DRAFT_STATE]139 if (!state.modified_) {140 switch (state.type_) {chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 141why, one step back141 case ProxyType.ES5Array:Immer 7Michel Weststratebefore adoption467ea5d
line 142why, one step back142 if (hasArrayChanges(state)) markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 143why, one step back143 breakchore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 144why, one step back144 case ProxyType.ES5Object:Immer 7Michel Weststratebefore adoption467ea5d
line 145why, one step back145 if (hasObjectChanges(state)) markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 146–156why, one step back146 break147 }148 }149 }150 }151152 function markChangesRecursively(object: any) {153 if (!object || typeof object !== "object") return154 const state: ES5State | undefined = object[DRAFT_STATE]155 if (!state) return156 const {base_, draft_, assigned_, type_} = statechore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 157why, one step back157 if (type_ === ProxyType.ES5Object) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 158–166why, one step back158 // Look for added keys.159 // probably there is a faster way to detect changes, as sweep + recurse seems to do some160 // unnecessary work.161 // also: probably we can store the information we detect here, to speed up tree finalization!162 each(draft_, key => {163 if ((key as any) === DRAFT_STATE) return164 // The `undefined` check is a fast path for pre-existing keys.165 if ((base_ as any)[key] === undefined && !has(base_, key)) {166 assigned_[key] = trueImmer 7Michel Weststratebefore adoption467ea5d
line 167why, one step back167 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 168–177why, one step back168 } else if (!assigned_[key]) {169 // Only untouched properties trigger recursion.170 markChangesRecursively(draft_[key])171 }172 })173 // Look for removed keys.174 each(base_, key => {175 // The `undefined` check is a fast path for pre-existing keys.176 if (draft_[key] === undefined && !has(draft_, key)) {177 assigned_[key] = falseImmer 7Michel Weststratebefore adoption467ea5d
line 178why, one step back178 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 179–180why, one step back179 }180 })chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 181why, one step back181 } else if (type_ === ProxyType.ES5Array) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 182why, one step back182 if (hasArrayChanges(state as ES5ArrayState)) {Immer 7Michel Weststratebefore adoption467ea5d
line 183why, one step back183 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 184–197why, one step back184 assigned_.length = true185 }186187 if (draft_.length < base_.length) {188 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = false189 } else {190 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = true191 }192193 // Minimum count is enough, the other parts has been processed.194 const min = Math.min(draft_.length, base_.length)195196 for (let i = 0; i < min; i++) {197 // Only untouched indices trigger recursion.fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 198–200why, one step back198 if (!draft_.hasOwnProperty(i)) {199 assigned_[i] = true200 }Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 201–210why, one step back201 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])202 }203 }204 }205206 function hasObjectChanges(state: ES5ObjectState) {207 const {base_, draft_} = state208209 // Search for added keys and changed keys. Start at the back, because210 // non-numeric keys are ordered by time of definition on the object.fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
line 211why, one step back211 const keys = ownKeys(draft_)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 212why, one step back212 for (let i = keys.length - 1; i >= 0; i--) {fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 213–214why, one step back213 const key: any = keys[i]214 if (key === DRAFT_STATE) continueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 215–232why, one step back215 const baseValue = base_[key]216 // The `undefined` check is a fast path for pre-existing keys.217 if (baseValue === undefined && !has(base_, key)) {218 return true219 }220 // Once a base key is deleted, future changes go undetected, because its221 // descriptor is erased. This branch detects any missed changes.222 else {223 const value = draft_[key]224 const state: ImmerState = value && value[DRAFT_STATE]225 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {226 return true227 }228 }229 }230231 // At this point, no keys were added or changed.232 // Compare key count to determine if keys were deleted.fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 233–234why, one step back233 const baseIsDraft = !!base_[DRAFT_STATE as any]234 return keys.length !== ownKeys(base_).length + (baseIsDraft ? 0 : 1) // + 1 to correct for DRAFT_STATEMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 235–246why, one step back235 }236237 function hasArrayChanges(state: ES5ArrayState) {238 const {draft_} = state239 if (draft_.length !== state.base_.length) return true240 // See #116241 // If we first shorten the length, our array interceptors will be removed.242 // If after that new items are added, result in the same original length,243 // those last items will have no intercepting property.244 // So if there is no own descriptor on the last position, we know that items were removed and added245 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check246 // the last onefix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
line 247why, one step back247 // last descriptor can be not a trap, if the array was extendedMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 248–253why, one step back248 const descriptor = Object.getOwnPropertyDescriptor(249 draft_,250 draft_.length - 1251 )252 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)253 if (descriptor && !descriptor.get) return truefix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659
lines 254–257why, one step back254 // if we miss a property, it has been deleted, so array probobaly changed255 for (let i = 0; i < draft_.length; i++) {256 if (!draft_.hasOwnProperty(i)) return true257 }Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 258–261why, one step back258 // For all other cases, we don't have to compare, as they would have been picked up by the index setters259 return false260 }261Immer 7Michel Weststratebefore adoption467ea5d
line 262why, one step back262 function hasChanges_(state: ES5State) {chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 263why, one step back263 return state.type_ === ProxyType.ES5ObjectImmer 7Michel Weststratebefore adoption467ea5d
lines 264–265why, one step back264 ? hasObjectChanges(state)265 : hasArrayChanges(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 266–273why, one step back266 }267268 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {269 if (state.revoked_) die(3, JSON.stringify(latest(state)))270 }271272 loadPlugin("ES5", {273 createES5Proxy_,Immer 7Michel Weststratebefore adoption467ea5d
lines 274–275why, one step back274 willFinalizeES5_,275 hasChanges_Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 276–277why, one step back276 })277}