es5.ts
This file
Path
src/plugins/es5.ts
270 lines · 8.1 KiB · TypeScript
Last changed by
fix: out of range assignments were broken in ES5 mode. Fixes #638Michel Weststratebefore adoption0fe9132
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–50why, one step back48 function createES5Draft(isArray: boolean, base: any) {49 if (isArray) {50 const draft = new Array(base.length)fix: out of range assignments were broken in ES5 mode. Fixes #638Michel Weststratebefore adoption0fe9132
lines 51–52why, one step back51 for (let i = 0; i < base.length; i++)52 Object.defineProperty(draft, "" + i, proxyProperty(i, true))Immer 7Michel Weststratebefore adoption467ea5d
lines 53–54why, one step back53 return draft54 } else {fix: out of range assignments were broken in ES5 mode. Fixes #638Michel Weststratebefore adoption0fe9132
lines 55–64why, one step back55 const descriptors = getOwnPropertyDescriptors(base)56 delete descriptors[DRAFT_STATE as any]57 const keys = ownKeys(descriptors)58 for (let i = 0; i < keys.length; i++) {59 const key: any = keys[i]60 descriptors[key] = proxyProperty(61 key,62 isArray || !!descriptors[key].enumerable63 )64 }Immer 7Michel Weststratebefore adoption467ea5d
lines 65–68why, one step back65 return Object.create(Object.getPrototypeOf(base), descriptors)66 }67 }68Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 69–73why, one step back69 function createES5Proxy_<T>(70 base: T,71 parent?: ImmerState72 ): Drafted<T, ES5ObjectState | ES5ArrayState> {73 const isArray = Array.isArray(base)Immer 7Michel Weststratebefore adoption467ea5d
line 74why, one step back74 const draft = createES5Draft(isArray, base)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 75–82why, one step back7576 const state: ES5ObjectState | ES5ArrayState = {77 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),78 scope_: parent ? parent.scope_ : getCurrentScope(),79 modified_: false,80 finalized_: false,81 assigned_: {},82 parent_: parent,Immer 7Michel Weststratebefore adoption467ea5d
line 83why, one step back83 // base is the object we are draftingMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 84why, one step back84 base_: base,Immer 7Michel Weststratebefore adoption467ea5d
line 85why, one step back85 // 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 86–106why, one step back86 draft_: draft,87 copy_: null,88 revoked_: false,89 isManual_: false90 }9192 Object.defineProperty(draft, DRAFT_STATE, {93 value: state,94 // enumerable: false <- the default95 writable: true96 })97 return draft98 }99100 // property descriptors are recycled to make sure we don't create a get and set closure per property,101 // but share them all instead102 const descriptors: {[prop: string]: PropertyDescriptor} = {}103104 function proxyProperty(105 prop: string | number,106 enumerable: booleanImmer 7Michel Weststratebefore adoption467ea5d
line 107why, one step back107 ): PropertyDescriptor {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 108–112why, one step back108 let desc = descriptors[prop]109 if (desc) {110 desc.enumerable = enumerable111 } else {112 descriptors[prop] = desc = {Immer 7Michel Weststratebefore adoption467ea5d
line 113why, one step back113 configurable: true,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 114–115why, one step back114 enumerable,115 get(this: any) {Immer 7Michel Weststratebefore adoption467ea5d
lines 116–119why, one step back116 const state = this[DRAFT_STATE]117 if (__DEV__) assertUnrevoked(state)118 // @ts-ignore119 return objectTraps.get(state, prop)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 120–121why, one step back120 },121 set(this: any, value) {Immer 7Michel Weststratebefore adoption467ea5d
lines 122–125why, one step back122 const state = this[DRAFT_STATE]123 if (__DEV__) assertUnrevoked(state)124 // @ts-ignore125 objectTraps.set(state, prop, value)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 126–128why, one step back126 }127 }128 }Immer 7Michel Weststratebefore adoption467ea5d
line 129why, one step back129 return descMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 130–142why, one step back130 }131132 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.133 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {134 // The natural order of drafts in the `scope` array is based on when they135 // were accessed. By processing drafts in reverse natural order, we have a136 // better chance of processing leaf nodes first. When a leaf node is known to137 // have changed, we can avoid any traversal of its ancestor nodes.138 for (let i = drafts.length - 1; i >= 0; i--) {139 const state: ES5State = drafts[i][DRAFT_STATE]140 if (!state.modified_) {141 switch (state.type_) {142 case ProxyTypeES5Array:Immer 7Michel Weststratebefore adoption467ea5d
line 143why, one step back143 if (hasArrayChanges(state)) markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 144–145why, one step back144 break145 case ProxyTypeES5Object:Immer 7Michel Weststratebefore adoption467ea5d
line 146why, one step back146 if (hasObjectChanges(state)) markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 147–167why, one step back147 break148 }149 }150 }151 }152153 function markChangesRecursively(object: any) {154 if (!object || typeof object !== "object") return155 const state: ES5State | undefined = object[DRAFT_STATE]156 if (!state) return157 const {base_, draft_, assigned_, type_} = state158 if (type_ === ProxyTypeES5Object) {159 // Look for added keys.160 // probably there is a faster way to detect changes, as sweep + recurse seems to do some161 // unnecessary work.162 // also: probably we can store the information we detect here, to speed up tree finalization!163 each(draft_, key => {164 if ((key as any) === DRAFT_STATE) return165 // The `undefined` check is a fast path for pre-existing keys.166 if ((base_ as any)[key] === undefined && !has(base_, key)) {167 assigned_[key] = trueImmer 7Michel Weststratebefore adoption467ea5d
line 168why, one step back168 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 169–178why, one step back169 } else if (!assigned_[key]) {170 // Only untouched properties trigger recursion.171 markChangesRecursively(draft_[key])172 }173 })174 // Look for removed keys.175 each(base_, key => {176 // The `undefined` check is a fast path for pre-existing keys.177 if (draft_[key] === undefined && !has(draft_, key)) {178 assigned_[key] = falseImmer 7Michel Weststratebefore adoption467ea5d
line 179why, one step back179 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 180–183why, one step back180 }181 })182 } else if (type_ === ProxyTypeES5Array) {183 if (hasArrayChanges(state as ES5ArrayState)) {Immer 7Michel Weststratebefore adoption467ea5d
line 184why, one step back184 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 185–208why, one step back185 assigned_.length = true186 }187188 if (draft_.length < base_.length) {189 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = false190 } else {191 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = true192 }193194 // Minimum count is enough, the other parts has been processed.195 const min = Math.min(draft_.length, base_.length)196197 for (let i = 0; i < min; i++) {198 // Only untouched indices trigger recursion.199 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])200 }201 }202 }203204 function hasObjectChanges(state: ES5ObjectState) {205 const {base_, draft_} = state206207 // Search for added keys and changed keys. Start at the back, because208 // 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 209why, one step back209 const keys = ownKeys(draft_)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 210why, one step back210 for (let i = keys.length - 1; i >= 0; i--) {fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 211–212why, one step back211 const key: any = keys[i]212 if (key === DRAFT_STATE) continueMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 213–230why, one step back213 const baseValue = base_[key]214 // The `undefined` check is a fast path for pre-existing keys.215 if (baseValue === undefined && !has(base_, key)) {216 return true217 }218 // Once a base key is deleted, future changes go undetected, because its219 // descriptor is erased. This branch detects any missed changes.220 else {221 const value = draft_[key]222 const state: ImmerState = value && value[DRAFT_STATE]223 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {224 return true225 }226 }227 }228229 // At this point, no keys were added or changed.230 // Compare key count to determine if keys were deleted.fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf
lines 231–232why, one step back231 const baseIsDraft = !!base_[DRAFT_STATE as any]232 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 233–254why, one step back233 }234235 function hasArrayChanges(state: ES5ArrayState) {236 const {draft_} = state237 if (draft_.length !== state.base_.length) return true238 // See #116239 // If we first shorten the length, our array interceptors will be removed.240 // If after that new items are added, result in the same original length,241 // those last items will have no intercepting property.242 // So if there is no own descriptor on the last position, we know that items were removed and added243 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check244 // the last one245 const descriptor = Object.getOwnPropertyDescriptor(246 draft_,247 draft_.length - 1248 )249 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)250 if (descriptor && !descriptor.get) return true251 // For all other cases, we don't have to compare, as they would have been picked up by the index setters252 return false253 }254Immer 7Michel Weststratebefore adoption467ea5d
lines 255–258why, one step back255 function hasChanges_(state: ES5State) {256 return state.type_ === ProxyTypeES5Object257 ? hasObjectChanges(state)258 : hasArrayChanges(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 259–266why, one step back259 }260261 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {262 if (state.revoked_) die(3, JSON.stringify(latest(state)))263 }264265 loadPlugin("ES5", {266 createES5Proxy_,Immer 7Michel Weststratebefore adoption467ea5d
lines 267–268why, one step back267 willFinalizeES5_,268 hasChanges_Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 269–270why, one step back269 })270}