es5.ts
This file
Path
src/plugins/es5.ts
300 lines · 8.6 KiB · TypeScript
Last changed by
scramble plugin names, -56Michel Weststratebefore adoption037b474
History of this fileTake it
More WIPMichel Weststratebefore adoptionee1c977
lines 1–6why, one step back1import {2 ImmerState,3 Drafted,4 Objectish,5 ES5ArrayState,6 ES5ObjectState,TSDXMichel Weststratebefore adoption4c4fecc
lines 7–18why, one step back7 each,8 has,9 isDraft,10 isDraftable,11 isEnumerable,12 shallowCopy,13 latest,14 createHiddenProperty,15 DRAFT_STATE,16 assertUnrevoked,17 is,18 loadPlugin,New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 19why, one step back19 ImmerScope,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 20–22why, one step back20 createProxy,21 ProxyTypeES5Array,22 ProxyTypeES5ObjectMore WIPMichel Weststratebefore adoptionee1c977
lines 23–26why, one step back23} from "../internal"2425type ES5State = ES5ArrayState | ES5ObjectState26TSDXMichel Weststratebefore adoption4c4fecc
line 27why, one step back27export function enableES5() {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 28why, one step back28 function willFinalizeES5_(TSDXMichel Weststratebefore adoption4c4fecc
lines 29–32why, one step back29 scope: ImmerScope,30 result: any,31 isReplaced: boolean32 ) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 33–34why, one step back33 scope.drafts_!.forEach((draft: any) => {34 ;(draft[DRAFT_STATE] as ES5State).finalizing_ = trueTSDXMichel Weststratebefore adoption4c4fecc
lines 35–36why, one step back35 })36 if (!isReplaced) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 37–38why, one step back37 if (scope.patches_) {38 markChangesRecursively(scope.drafts_![0])More WIPMichel Weststratebefore adoptionee1c977
line 39why, one step back39 }TSDXMichel Weststratebefore adoption4c4fecc
line 40why, one step back40 // This is faster when we don't care about which attributes changed.Small improvementsMichel Weststratebefore adoption712af5c
line 41why, one step back41 // TODO: should be prefixed with else!Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 42why, one step back42 markChangesSweep(scope.drafts_)More WIPMichel Weststratebefore adoptionee1c977
line 43why, one step back43 }TSDXMichel Weststratebefore adoption4c4fecc
line 44why, one step back44 // When a child draft is returned, look for changes.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 45–49why, one step back45 else if (46 isDraft(result) &&47 (result[DRAFT_STATE] as ES5State).scope_ === scope48 ) {49 markChangesSweep(scope.drafts_)TSDXMichel Weststratebefore adoption4c4fecc
lines 50–51why, one step back50 }51 }More WIPMichel Weststratebefore adoptionee1c977
line 52why, one step back52scramble plugin names, -56Michel Weststratebefore adoption037b474
line 53why, one step back53 function createES5Proxy_<T>(TSDXMichel Weststratebefore adoption4c4fecc
lines 54–57why, one step back54 base: T,55 parent?: ImmerState56 ): Drafted<T, ES5ObjectState | ES5ArrayState> {57 const isArray = Array.isArray(base)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 58why, one step back58 const draft: any = clonePotentialDraft(base)More WIPMichel Weststratebefore adoptionee1c977
line 59why, one step back59TSDXMichel Weststratebefore adoption4c4fecc
lines 60–62why, one step back60 each(draft, prop => {61 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))62 })More WIPMichel Weststratebefore adoptionee1c977
line 63why, one step back63TSDXMichel Weststratebefore adoption4c4fecc
line 64why, one step back64 const state: ES5ObjectState | ES5ArrayState = {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 65why, one step back65 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 66–76why, one step back66 scope_: parent ? parent.scope_ : ImmerScope.current_!,67 modified_: false,68 finalizing_: false,69 finalized_: false,70 assigned_: {},71 parent_: parent,72 base_: base,73 draft_: draft,74 copy_: null,75 revoked_: false,76 isManual_: falseMore WIPMichel Weststratebefore adoptionee1c977
lines 77–78why, one step back77 }78TSDXMichel Weststratebefore adoption4c4fecc
lines 79–81why, one step back79 createHiddenProperty(draft, DRAFT_STATE, state)80 return draft81 }More WIPMichel Weststratebefore adoptionee1c977
line 82why, one step back82TSDXMichel Weststratebefore adoption4c4fecc
lines 83–84why, one step back83 // Access a property without creating an Immer draft.84 function peek(draft: Drafted, prop: PropertyKey) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 85–87why, one step back85 const state: ES5State = draft[DRAFT_STATE]86 if (state && !state.finalizing_) {87 state.finalizing_ = trueTSDXMichel Weststratebefore adoption4c4fecc
line 88why, one step back88 const value = draft[prop]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 89why, one step back89 state.finalizing_ = falseMore WIPMichel Weststratebefore adoptionee1c977
lines 90–91why, one step back90 return value91 }TSDXMichel Weststratebefore adoption4c4fecc
lines 92–93why, one step back92 return draft[prop]93 }More WIPMichel Weststratebefore adoptionee1c977
line 94why, one step back94TSDXMichel Weststratebefore adoption4c4fecc
lines 95–97why, one step back95 function get(state: ES5State, prop: string | number) {96 assertUnrevoked(state)97 const value = peek(latest(state), prop)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 98why, one step back98 if (state.finalizing_) return valueTSDXMichel Weststratebefore adoption4c4fecc
line 99why, one step back99 // Create a draft if the value is unmodified.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 100why, one step back100 if (value === peek(state.base_, prop) && isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 101why, one step back101 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 102why, one step back102 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 103–107why, one step back103 return (state.copy_![prop] = createProxy(104 state.scope_.immer_,105 value,106 state107 ))More WIPMichel Weststratebefore adoptionee1c977
line 108why, one step back108 }TSDXMichel Weststratebefore adoption4c4fecc
lines 109–110why, one step back109 return value110 }More WIPMichel Weststratebefore adoptionee1c977
line 111why, one step back111TSDXMichel Weststratebefore adoption4c4fecc
lines 112–113why, one step back112 function set(state: ES5State, prop: string | number, value: any) {113 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 114–115why, one step back114 state.assigned_[prop] = true115 if (!state.modified_) {TSDXMichel Weststratebefore adoption4c4fecc
line 116why, one step back116 if (is(value, peek(latest(state), prop))) returnscramble plugin names, -56Michel Weststratebefore adoption037b474
line 117why, one step back117 markChangedES5_(state)TSDXMichel Weststratebefore adoption4c4fecc
line 118why, one step back118 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 119why, one step back119 }TSDXMichel Weststratebefore adoption4c4fecc
line 120why, one step back120 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 121why, one step back121 state.copy_![prop] = valueTSDXMichel Weststratebefore adoption4c4fecc
line 122why, one step back122 }More WIPMichel Weststratebefore adoptionee1c977
line 123why, one step back123scramble plugin names, -56Michel Weststratebefore adoption037b474
line 124why, one step back124 function markChangedES5_(state: ImmerState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 125–126why, one step back125 if (!state.modified_) {126 state.modified_ = truescramble plugin names, -56Michel Weststratebefore adoption037b474
line 127why, one step back127 if (state.parent_) markChangedES5_(state.parent_)More WIPMichel Weststratebefore adoptionee1c977
line 128why, one step back128 }TSDXMichel Weststratebefore adoption4c4fecc
line 129why, one step back129 }More WIPMichel Weststratebefore adoptionee1c977
line 130why, one step back130TSDXMichel Weststratebefore adoption4c4fecc
line 131why, one step back131 function prepareCopy(state: ES5State) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 132why, one step back132 if (!state.copy_) state.copy_ = clonePotentialDraft(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 133–135why, one step back133 }134135 function clonePotentialDraft(base: Objectish) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 136why, one step back136 const state: ES5State | undefined = base && (base as any)[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 137why, one step back137 if (state) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 138–140why, one step back138 state.finalizing_ = true139 const draft = shallowCopy(state.draft_, true)140 state.finalizing_ = falseTSDXMichel Weststratebefore adoption4c4fecc
line 141why, one step back141 return draftMore WIPMichel Weststratebefore adoptionee1c977
line 142why, one step back142 }TSDXMichel Weststratebefore adoption4c4fecc
lines 143–144why, one step back143 return shallowCopy(base)144 }More WIPMichel Weststratebefore adoptionee1c977
line 145why, one step back145TSDXMichel Weststratebefore adoption4c4fecc
lines 146–148why, one step back146 // property descriptors are recycled to make sure we don't create a get and set closure per property,147 // but share them all instead148 const descriptors: {[prop: string]: PropertyDescriptor} = {}More WIPMichel Weststratebefore adoptionee1c977
line 149why, one step back149TSDXMichel Weststratebefore adoption4c4fecc
lines 150–159why, one step back150 function proxyProperty(151 draft: Drafted<any, ES5State>,152 prop: string | number,153 enumerable: boolean154 ) {155 let desc = descriptors[prop]156 if (desc) {157 desc.enumerable = enumerable158 } else {159 descriptors[prop] = desc = {Some notesMichel Weststratebefore adoption746dc6f
line 160why, one step back160 // configurable: true,TSDXMichel Weststratebefore adoption4c4fecc
lines 161–166why, one step back161 enumerable,162 get(this: any) {163 return get(this[DRAFT_STATE], prop)164 },165 set(this: any, value) {166 set(this[DRAFT_STATE], prop, value)More WIPMichel Weststratebefore adoptionee1c977
lines 167–169why, one step back167 }168 }169 }TSDXMichel Weststratebefore adoption4c4fecc
lines 170–171why, one step back170 Object.defineProperty(draft, prop, desc)171 }More WIPMichel Weststratebefore adoptionee1c977
line 172why, one step back172TSDXMichel Weststratebefore adoption4c4fecc
lines 173–179why, one step back173 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.174 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {175 // The natural order of drafts in the `scope` array is based on when they176 // were accessed. By processing drafts in reverse natural order, we have a177 // better chance of processing leaf nodes first. When a leaf node is known to178 // have changed, we can avoid any traversal of its ancestor nodes.179 for (let i = drafts.length - 1; i >= 0; i--) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 180–182why, one step back180 const state: ES5State = drafts[i][DRAFT_STATE]181 if (!state.modified_) {182 switch (state.type_) {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 183why, one step back183 case ProxyTypeES5Array:scramble plugin names, -56Michel Weststratebefore adoption037b474
line 184why, one step back184 if (hasArrayChanges(state)) markChangedES5_(state)TSDXMichel Weststratebefore adoption4c4fecc
line 185why, one step back185 breakKilled enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 186why, one step back186 case ProxyTypeES5Object:scramble plugin names, -56Michel Weststratebefore adoption037b474
line 187why, one step back187 if (hasObjectChanges(state)) markChangedES5_(state)TSDXMichel Weststratebefore adoption4c4fecc
line 188why, one step back188 breakMore WIPMichel Weststratebefore adoptionee1c977
lines 189–191why, one step back189 }190 }191 }TSDXMichel Weststratebefore adoption4c4fecc
line 192why, one step back192 }More WIPMichel Weststratebefore adoptionee1c977
line 193why, one step back193TSDXMichel Weststratebefore adoption4c4fecc
lines 194–195why, one step back194 function markChangesRecursively(object: any) {195 if (!object || typeof object !== "object") returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 196why, one step back196 const state: ES5State | undefined = object[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 197why, one step back197 if (!state) returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 198why, one step back198 const {base_, draft_, assigned_, type_} = stateKilled enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 199why, one step back199 if (type_ === ProxyTypeES5Object) {TSDXMichel Weststratebefore adoption4c4fecc
lines 200–204why, one step back200 // Look for added keys.201 // TODO: looks quite duplicate to hasObjectChanges,202 // probably there is a faster way to detect changes, as sweep + recurse seems to do some203 // unnecessary work.204 // also: probably we can store the information we detect here, to speed up tree finalization!Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 205why, one step back205 each(draft_, key => {TSDXMichel Weststratebefore adoption4c4fecc
lines 206–207why, one step back206 if ((key as any) === DRAFT_STATE) return207 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 208–209why, one step back208 if ((base_ as any)[key] === undefined && !has(base_, key)) {209 assigned_[key] = truescramble plugin names, -56Michel Weststratebefore adoption037b474
line 210why, one step back210 markChangedES5_(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 211why, one step back211 } else if (!assigned_[key]) {TSDXMichel Weststratebefore adoption4c4fecc
line 212why, one step back212 // Only untouched properties trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 213why, one step back213 markChangesRecursively(draft_[key])More WIPMichel Weststratebefore adoptionee1c977
line 214why, one step back214 }TSDXMichel Weststratebefore adoption4c4fecc
lines 215–216why, one step back215 })216 // Look for removed keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 217why, one step back217 each(base_, key => {TSDXMichel Weststratebefore adoption4c4fecc
line 218why, one step back218 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 219–220why, one step back219 if (draft_[key] === undefined && !has(draft_, key)) {220 assigned_[key] = falsescramble plugin names, -56Michel Weststratebefore adoption037b474
line 221why, one step back221 markChangedES5_(state)More WIPMichel Weststratebefore adoptionee1c977
line 222why, one step back222 }TSDXMichel Weststratebefore adoption4c4fecc
line 223why, one step back223 })Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 224why, one step back224 } else if (type_ === ProxyTypeES5Array) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 225why, one step back225 if (hasArrayChanges(state as ES5ArrayState)) {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 226why, one step back226 markChangedES5_(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 227why, one step back227 assigned_.length = trueTSDXMichel Weststratebefore adoption4c4fecc
line 228why, one step back228 }More WIPMichel Weststratebefore adoptionee1c977
line 229why, one step back229Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 230–231why, one step back230 if (draft_.length < base_.length) {231 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = falseTSDXMichel Weststratebefore adoption4c4fecc
line 232why, one step back232 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 233why, one step back233 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = trueTSDXMichel Weststratebefore adoption4c4fecc
line 234why, one step back234 }More WIPMichel Weststratebefore adoptionee1c977
line 235why, one step back235TSDXMichel Weststratebefore adoption4c4fecc
line 236why, one step back236 // Minimum count is enough, the other parts has been processed.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 237why, one step back237 const min = Math.min(draft_.length, base_.length)TSDXMichel Weststratebefore adoption4c4fecc
lines 238–240why, one step back238239 for (let i = 0; i < min; i++) {240 // Only untouched indices trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 241why, one step back241 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])More WIPMichel Weststratebefore adoptionee1c977
lines 242–243why, one step back242 }243 }TSDXMichel Weststratebefore adoption4c4fecc
line 244why, one step back244 }More WIPMichel Weststratebefore adoptionee1c977
line 245why, one step back245TSDXMichel Weststratebefore adoption4c4fecc
line 246why, one step back246 function hasObjectChanges(state: ES5ObjectState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 247why, one step back247 const {base_, draft_} = stateMore WIPMichel Weststratebefore adoptionee1c977
line 248why, one step back248TSDXMichel Weststratebefore adoption4c4fecc
lines 249–250why, one step back249 // Search for added keys and changed keys. Start at the back, because250 // non-numeric keys are ordered by time of definition on the object.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 251why, one step back251 const keys = Object.keys(draft_)TSDXMichel Weststratebefore adoption4c4fecc
lines 252–253why, one step back252 for (let i = keys.length - 1; i >= 0; i--) {253 const key = keys[i]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 254why, one step back254 const baseValue = base_[key]TSDXMichel Weststratebefore adoption4c4fecc
line 255why, one step back255 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 256why, one step back256 if (baseValue === undefined && !has(base_, key)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 257–261why, one step back257 return true258 }259 // Once a base key is deleted, future changes go undetected, because its260 // descriptor is erased. This branch detects any missed changes.261 else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 262–264why, one step back262 const value = draft_[key]263 const state: ImmerState = value && value[DRAFT_STATE]264 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {More WIPMichel Weststratebefore adoptionee1c977
lines 265–269why, one step back265 return true266 }267 }268 }269TSDXMichel Weststratebefore adoption4c4fecc
lines 270–271why, one step back270 // At this point, no keys were added or changed.271 // Compare key count to determine if keys were deleted.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 272why, one step back272 return keys.length !== Object.keys(base_).lengthTSDXMichel Weststratebefore adoption4c4fecc
line 273why, one step back273 }More WIPMichel Weststratebefore adoptionee1c977
line 274why, one step back274TSDXMichel Weststratebefore adoption4c4fecc
line 275why, one step back275 function hasArrayChanges(state: ES5ArrayState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 276–277why, one step back276 const {draft_} = state277 if (draft_.length !== state.base_.length) return trueTSDXMichel Weststratebefore adoption4c4fecc
lines 278–284why, one step back278 // See #116279 // If we first shorten the length, our array interceptors will be removed.280 // If after that new items are added, result in the same original length,281 // those last items will have no intercepting property.282 // So if there is no own descriptor on the last position, we know that items were removed and added283 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check284 // the last oneMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 285–288why, one step back285 const descriptor = Object.getOwnPropertyDescriptor(286 draft_,287 draft_.length - 1288 )TSDXMichel Weststratebefore adoption4c4fecc
lines 289–292why, one step back289 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)290 if (descriptor && !descriptor.get) return true291 // For all other cases, we don't have to compare, as they would have been picked up by the index setters292 return falseMore WIPMichel Weststratebefore adoptionee1c977
line 293why, one step back293 }TSDXMichel Weststratebefore adoption4c4fecc
lines 294–295why, one step back294295 loadPlugin("es5", {scramble plugin names, -56Michel Weststratebefore adoption037b474
lines 296–298why, one step back296 createES5Proxy_,297 markChangedES5_,298 willFinalizeES5_TSDXMichel Weststratebefore adoption4c4fecc
lines 299–300why, one step back299 })300}