es5.ts
This file
Path
src/plugins/es5.ts
272 lines · 8.2 KiB · TypeScript
Last changed by
fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–16why, 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,14 ProxyTypeES5Array,15 ProxyTypeES5Object,16 getCurrentScope,Immer 7Michel Weststratebefore adoption467ea5d
lines 17–18why, one step back17 die,18 markChanged,fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
line 19why, one step back19 objectTraps,fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2
lines 20–21why, one step back20 ownKeys,21 getOwnPropertyDescriptorsMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 22–47why, one step back22} from "../internal"2324type ES5State = ES5ArrayState | ES5ObjectState2526export function enableES5() {27 function willFinalizeES5_(28 scope: ImmerScope,29 result: any,30 isReplaced: boolean31 ) {32 if (!isReplaced) {33 if (scope.patches_) {34 markChangesRecursively(scope.drafts_![0])35 }36 // This is faster when we don't care about which attributes changed.37 markChangesSweep(scope.drafts_)38 }39 // When a child draft is returned, look for changes.40 else if (41 isDraft(result) &&42 (result[DRAFT_STATE] as ES5State).scope_ === scope43 ) {44 markChangesSweep(scope.drafts_)45 }46 }47Immer 7Michel Weststratebefore adoption467ea5d
lines 48–49why, one step back48 function createES5Draft(isArray: boolean, base: any) {49 // Create a new object / array, where each own property is trapped with an accessorfix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2
line 50why, one step back50 const descriptors = getOwnPropertyDescriptors(base)Immer 7Michel Weststratebefore adoption467ea5d
lines 51–53why, one step back51 // Descriptors we want to skip:52 if (isArray) delete descriptors.length53 delete descriptors[DRAFT_STATE as any]fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 54–56why, one step back54 const keys = ownKeys(descriptors)55 for (let i = 0; i < keys.length; i++) {56 const key: any = keys[i]Immer 7Michel Weststratebefore adoption467ea5d
lines 57–70why, one step back57 descriptors[key] = proxyProperty(58 key,59 isArray || !!descriptors[key].enumerable60 )61 }62 if (isArray) {63 const draft = new Array(base.length)64 Object.defineProperties(draft, descriptors)65 return draft66 } else {67 return Object.create(Object.getPrototypeOf(base), descriptors)68 }69 }70Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 71–75why, one step back71 function createES5Proxy_<T>(72 base: T,73 parent?: ImmerState74 ): Drafted<T, ES5ObjectState | ES5ArrayState> {75 const isArray = Array.isArray(base)Immer 7Michel Weststratebefore adoption467ea5d
line 76why, one step back76 const draft = createES5Draft(isArray, base)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 77–84why, one step back7778 const state: ES5ObjectState | ES5ArrayState = {79 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),80 scope_: parent ? parent.scope_ : getCurrentScope(),81 modified_: false,82 finalized_: false,83 assigned_: {},84 parent_: parent,Immer 7Michel Weststratebefore adoption467ea5d
line 85why, one step back85 // base is the object we are draftingMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 86why, one step back86 base_: base,Immer 7Michel Weststratebefore adoption467ea5d
line 87why, one step back87 // 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 88–108why, one step back88 draft_: draft,89 copy_: null,90 revoked_: false,91 isManual_: false92 }9394 Object.defineProperty(draft, DRAFT_STATE, {95 value: state,96 // enumerable: false <- the default97 writable: true98 })99 return draft100 }101102 // property descriptors are recycled to make sure we don't create a get and set closure per property,103 // but share them all instead104 const descriptors: {[prop: string]: PropertyDescriptor} = {}105106 function proxyProperty(107 prop: string | number,108 enumerable: booleanImmer 7Michel Weststratebefore adoption467ea5d
line 109why, one step back109 ): PropertyDescriptor {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 110–114why, one step back110 let desc = descriptors[prop]111 if (desc) {112 desc.enumerable = enumerable113 } else {114 descriptors[prop] = desc = {Immer 7Michel Weststratebefore adoption467ea5d
line 115why, one step back115 configurable: true,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 116–117why, one step back116 enumerable,117 get(this: any) {Immer 7Michel Weststratebefore adoption467ea5d
lines 118–121why, one step back118 const state = this[DRAFT_STATE]119 if (__DEV__) assertUnrevoked(state)120 // @ts-ignore121 return objectTraps.get(state, prop)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 122–123why, one step back122 },123 set(this: any, value) {Immer 7Michel Weststratebefore adoption467ea5d
lines 124–127why, one step back124 const state = this[DRAFT_STATE]125 if (__DEV__) assertUnrevoked(state)126 // @ts-ignore127 objectTraps.set(state, prop, value)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 128–130why, one step back128 }129 }130 }Immer 7Michel Weststratebefore adoption467ea5d
line 131why, one step back131 return descMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 132–144why, one step back132 }133134 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.135 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {136 // The natural order of drafts in the `scope` array is based on when they137 // were accessed. By processing drafts in reverse natural order, we have a138 // better chance of processing leaf nodes first. When a leaf node is known to139 // have changed, we can avoid any traversal of its ancestor nodes.140 for (let i = drafts.length - 1; i >= 0; i--) {141 const state: ES5State = drafts[i][DRAFT_STATE]142 if (!state.modified_) {143 switch (state.type_) {144 case ProxyTypeES5Array:Immer 7Michel Weststratebefore adoption467ea5d
line 145why, one step back145 if (hasArrayChanges(state)) markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 146–147why, one step back146 break147 case ProxyTypeES5Object:Immer 7Michel Weststratebefore adoption467ea5d
line 148why, one step back148 if (hasObjectChanges(state)) markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 149–169why, one step back149 break150 }151 }152 }153 }154155 function markChangesRecursively(object: any) {156 if (!object || typeof object !== "object") return157 const state: ES5State | undefined = object[DRAFT_STATE]158 if (!state) return159 const {base_, draft_, assigned_, type_} = state160 if (type_ === ProxyTypeES5Object) {161 // Look for added keys.162 // probably there is a faster way to detect changes, as sweep + recurse seems to do some163 // unnecessary work.164 // also: probably we can store the information we detect here, to speed up tree finalization!165 each(draft_, key => {166 if ((key as any) === DRAFT_STATE) return167 // The `undefined` check is a fast path for pre-existing keys.168 if ((base_ as any)[key] === undefined && !has(base_, key)) {169 assigned_[key] = trueImmer 7Michel Weststratebefore adoption467ea5d
line 170why, one step back170 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 171–180why, one step back171 } else if (!assigned_[key]) {172 // Only untouched properties trigger recursion.173 markChangesRecursively(draft_[key])174 }175 })176 // Look for removed keys.177 each(base_, key => {178 // The `undefined` check is a fast path for pre-existing keys.179 if (draft_[key] === undefined && !has(draft_, key)) {180 assigned_[key] = falseImmer 7Michel Weststratebefore adoption467ea5d
line 181why, one step back181 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 182–185why, one step back182 }183 })184 } else if (type_ === ProxyTypeES5Array) {185 if (hasArrayChanges(state as ES5ArrayState)) {Immer 7Michel Weststratebefore adoption467ea5d
line 186why, one step back186 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 187–210why, one step back187 assigned_.length = true188 }189190 if (draft_.length < base_.length) {191 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = false192 } else {193 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = true194 }195196 // Minimum count is enough, the other parts has been processed.197 const min = Math.min(draft_.length, base_.length)198199 for (let i = 0; i < min; i++) {200 // Only untouched indices trigger recursion.201 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–256why, 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 one247 const descriptor = Object.getOwnPropertyDescriptor(248 draft_,249 draft_.length - 1250 )251 // 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 }256Immer 7Michel Weststratebefore adoption467ea5d
lines 257–260why, one step back257 function hasChanges_(state: ES5State) {258 return state.type_ === ProxyTypeES5Object259 ? hasObjectChanges(state)260 : hasArrayChanges(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 261–268why, one step back261 }262263 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {264 if (state.revoked_) die(3, JSON.stringify(latest(state)))265 }266267 loadPlugin("ES5", {268 createES5Proxy_,Immer 7Michel Weststratebefore adoption467ea5d
lines 269–270why, one step back269 willFinalizeES5_,270 hasChanges_Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 271–272why, one step back271 })272}