es5.ts
This file
Path
src/plugins/es5.ts
313 lines · 9.0 KiB · TypeScript
Last changed by
Preserve error codes in productionMichel Weststratebefore adoptiond005fab
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–15why, one step back7 each,8 has,9 isDraft,10 isDraftable,11 shallowCopy,12 latest,13 DRAFT_STATE,14 is,15 loadPlugin,New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 16why, one step back16 ImmerScope,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 17–18why, one step back17 createProxy,18 ProxyTypeES5Array,Some minor cleanupMichel Weststratebefore adoption718b7f6
line 19why, one step back19 ProxyTypeES5Object,Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 20–21why, one step back20 AnyObject,21 dieMore WIPMichel Weststratebefore adoptionee1c977
lines 22–25why, one step back22} from "../internal"2324type ES5State = ES5ArrayState | ES5ObjectState25TSDXMichel Weststratebefore adoption4c4fecc
line 26why, one step back26export function enableES5() {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 27why, one step back27 function willFinalizeES5_(TSDXMichel Weststratebefore adoption4c4fecc
lines 28–31why, one step back28 scope: ImmerScope,29 result: any,30 isReplaced: boolean31 ) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 32–33why, one step back32 scope.drafts_!.forEach((draft: any) => {33 ;(draft[DRAFT_STATE] as ES5State).finalizing_ = trueTSDXMichel Weststratebefore adoption4c4fecc
lines 34–35why, one step back34 })35 if (!isReplaced) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 36–37why, one step back36 if (scope.patches_) {37 markChangesRecursively(scope.drafts_![0])More WIPMichel Weststratebefore adoptionee1c977
line 38why, one step back38 }TSDXMichel Weststratebefore adoption4c4fecc
line 39why, one step back39 // This is faster when we don't care about which attributes changed.Small improvementsMichel Weststratebefore adoption712af5c
line 40why, one step back40 // TODO: should be prefixed with else!Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 41why, one step back41 markChangesSweep(scope.drafts_)More WIPMichel Weststratebefore adoptionee1c977
line 42why, one step back42 }TSDXMichel Weststratebefore adoption4c4fecc
line 43why, one step back43 // When a child draft is returned, look for changes.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 44–48why, one step back44 else if (45 isDraft(result) &&46 (result[DRAFT_STATE] as ES5State).scope_ === scope47 ) {48 markChangesSweep(scope.drafts_)TSDXMichel Weststratebefore adoption4c4fecc
lines 49–50why, one step back49 }50 }More WIPMichel Weststratebefore adoptionee1c977
line 51why, one step back51scramble plugin names, -56Michel Weststratebefore adoption037b474
line 52why, one step back52 function createES5Proxy_<T>(TSDXMichel Weststratebefore adoption4c4fecc
lines 53–56why, one step back53 base: T,54 parent?: ImmerState55 ): Drafted<T, ES5ObjectState | ES5ArrayState> {56 const isArray = Array.isArray(base)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 57why, one step back57 const draft: any = clonePotentialDraft(base)More WIPMichel Weststratebefore adoptionee1c977
line 58why, one step back58TSDXMichel Weststratebefore adoption4c4fecc
lines 59–61why, one step back59 each(draft, prop => {60 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))61 })More WIPMichel Weststratebefore adoptionee1c977
line 62why, one step back62TSDXMichel Weststratebefore adoption4c4fecc
line 63why, one step back63 const state: ES5ObjectState | ES5ArrayState = {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 64why, one step back64 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 65–75why, one step back65 scope_: parent ? parent.scope_ : ImmerScope.current_!,66 modified_: false,67 finalizing_: false,68 finalized_: false,69 assigned_: {},70 parent_: parent,71 base_: base,72 draft_: draft,73 copy_: null,74 revoked_: false,75 isManual_: falseMore WIPMichel Weststratebefore adoptionee1c977
lines 76–77why, one step back76 }77Some minor cleanupMichel Weststratebefore adoption718b7f6
lines 78–82why, one step back78 Object.defineProperty(draft, DRAFT_STATE, {79 value: state,80 // enumerable: false <- the default81 writable: true82 })TSDXMichel Weststratebefore adoption4c4fecc
lines 83–84why, one step back83 return draft84 }More WIPMichel Weststratebefore adoptionee1c977
line 85why, one step back85TSDXMichel Weststratebefore adoption4c4fecc
lines 86–87why, one step back86 // Access a property without creating an Immer draft.87 function peek(draft: Drafted, prop: PropertyKey) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 88–90why, one step back88 const state: ES5State = draft[DRAFT_STATE]89 if (state && !state.finalizing_) {90 state.finalizing_ = trueTSDXMichel Weststratebefore adoption4c4fecc
line 91why, one step back91 const value = draft[prop]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 92why, one step back92 state.finalizing_ = falseMore WIPMichel Weststratebefore adoptionee1c977
lines 93–94why, one step back93 return value94 }TSDXMichel Weststratebefore adoption4c4fecc
lines 95–96why, one step back95 return draft[prop]96 }More WIPMichel Weststratebefore adoptionee1c977
line 97why, one step back97TSDXMichel Weststratebefore adoption4c4fecc
lines 98–100why, one step back98 function get(state: ES5State, prop: string | number) {99 assertUnrevoked(state)100 const value = peek(latest(state), prop)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 101why, one step back101 if (state.finalizing_) return valueTSDXMichel Weststratebefore adoption4c4fecc
line 102why, one step back102 // Create a draft if the value is unmodified.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 103why, one step back103 if (value === peek(state.base_, prop) && isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 104why, one step back104 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 105why, one step back105 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 106–110why, one step back106 return (state.copy_![prop] = createProxy(107 state.scope_.immer_,108 value,109 state110 ))More WIPMichel Weststratebefore adoptionee1c977
line 111why, one step back111 }TSDXMichel Weststratebefore adoption4c4fecc
lines 112–113why, one step back112 return value113 }More WIPMichel Weststratebefore adoptionee1c977
line 114why, one step back114TSDXMichel Weststratebefore adoption4c4fecc
lines 115–116why, one step back115 function set(state: ES5State, prop: string | number, value: any) {116 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 117–118why, one step back117 state.assigned_[prop] = true118 if (!state.modified_) {TSDXMichel Weststratebefore adoption4c4fecc
line 119why, one step back119 if (is(value, peek(latest(state), prop))) returnscramble plugin names, -56Michel Weststratebefore adoption037b474
line 120why, one step back120 markChangedES5_(state)TSDXMichel Weststratebefore adoption4c4fecc
line 121why, one step back121 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 122why, one step back122 }TSDXMichel Weststratebefore adoption4c4fecc
line 123why, one step back123 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 124why, one step back124 state.copy_![prop] = valueTSDXMichel Weststratebefore adoption4c4fecc
line 125why, one step back125 }More WIPMichel Weststratebefore adoptionee1c977
line 126why, one step back126scramble plugin names, -56Michel Weststratebefore adoption037b474
line 127why, one step back127 function markChangedES5_(state: ImmerState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 128–129why, one step back128 if (!state.modified_) {129 state.modified_ = truescramble plugin names, -56Michel Weststratebefore adoption037b474
line 130why, one step back130 if (state.parent_) markChangedES5_(state.parent_)More WIPMichel Weststratebefore adoptionee1c977
line 131why, one step back131 }TSDXMichel Weststratebefore adoption4c4fecc
line 132why, one step back132 }More WIPMichel Weststratebefore adoptionee1c977
line 133why, one step back133TSDXMichel Weststratebefore adoption4c4fecc
line 134why, one step back134 function prepareCopy(state: ES5State) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 135why, one step back135 if (!state.copy_) state.copy_ = clonePotentialDraft(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 136–138why, one step back136 }137138 function clonePotentialDraft(base: Objectish) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 139why, one step back139 const state: ES5State | undefined = base && (base as any)[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 140why, one step back140 if (state) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 141–143why, one step back141 state.finalizing_ = true142 const draft = shallowCopy(state.draft_, true)143 state.finalizing_ = falseTSDXMichel Weststratebefore adoption4c4fecc
line 144why, one step back144 return draftMore WIPMichel Weststratebefore adoptionee1c977
line 145why, one step back145 }TSDXMichel Weststratebefore adoption4c4fecc
lines 146–147why, one step back146 return shallowCopy(base)147 }More WIPMichel Weststratebefore adoptionee1c977
line 148why, one step back148TSDXMichel Weststratebefore adoption4c4fecc
lines 149–151why, one step back149 // property descriptors are recycled to make sure we don't create a get and set closure per property,150 // but share them all instead151 const descriptors: {[prop: string]: PropertyDescriptor} = {}More WIPMichel Weststratebefore adoptionee1c977
line 152why, one step back152TSDXMichel Weststratebefore adoption4c4fecc
lines 153–162why, one step back153 function proxyProperty(154 draft: Drafted<any, ES5State>,155 prop: string | number,156 enumerable: boolean157 ) {158 let desc = descriptors[prop]159 if (desc) {160 desc.enumerable = enumerable161 } else {162 descriptors[prop] = desc = {Some notesMichel Weststratebefore adoption746dc6f
line 163why, one step back163 // configurable: true,TSDXMichel Weststratebefore adoption4c4fecc
lines 164–169why, one step back164 enumerable,165 get(this: any) {166 return get(this[DRAFT_STATE], prop)167 },168 set(this: any, value) {169 set(this[DRAFT_STATE], prop, value)More WIPMichel Weststratebefore adoptionee1c977
lines 170–172why, one step back170 }171 }172 }TSDXMichel Weststratebefore adoption4c4fecc
lines 173–174why, one step back173 Object.defineProperty(draft, prop, desc)174 }More WIPMichel Weststratebefore adoptionee1c977
line 175why, one step back175TSDXMichel Weststratebefore adoption4c4fecc
lines 176–182why, one step back176 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.177 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {178 // The natural order of drafts in the `scope` array is based on when they179 // were accessed. By processing drafts in reverse natural order, we have a180 // better chance of processing leaf nodes first. When a leaf node is known to181 // have changed, we can avoid any traversal of its ancestor nodes.182 for (let i = drafts.length - 1; i >= 0; i--) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 183–185why, one step back183 const state: ES5State = drafts[i][DRAFT_STATE]184 if (!state.modified_) {185 switch (state.type_) {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 186why, one step back186 case ProxyTypeES5Array:scramble plugin names, -56Michel Weststratebefore adoption037b474
line 187why, one step back187 if (hasArrayChanges(state)) markChangedES5_(state)TSDXMichel Weststratebefore adoption4c4fecc
line 188why, one step back188 breakKilled enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 189why, one step back189 case ProxyTypeES5Object:scramble plugin names, -56Michel Weststratebefore adoption037b474
line 190why, one step back190 if (hasObjectChanges(state)) markChangedES5_(state)TSDXMichel Weststratebefore adoption4c4fecc
line 191why, one step back191 breakMore WIPMichel Weststratebefore adoptionee1c977
lines 192–194why, one step back192 }193 }194 }TSDXMichel Weststratebefore adoption4c4fecc
line 195why, one step back195 }More WIPMichel Weststratebefore adoptionee1c977
line 196why, one step back196TSDXMichel Weststratebefore adoption4c4fecc
lines 197–198why, one step back197 function markChangesRecursively(object: any) {198 if (!object || typeof object !== "object") returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 199why, one step back199 const state: ES5State | undefined = object[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 200why, one step back200 if (!state) returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 201why, one step back201 const {base_, draft_, assigned_, type_} = stateKilled enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 202why, one step back202 if (type_ === ProxyTypeES5Object) {TSDXMichel Weststratebefore adoption4c4fecc
lines 203–207why, one step back203 // Look for added keys.204 // TODO: looks quite duplicate to hasObjectChanges,205 // probably there is a faster way to detect changes, as sweep + recurse seems to do some206 // unnecessary work.207 // also: probably we can store the information we detect here, to speed up tree finalization!Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 208why, one step back208 each(draft_, key => {TSDXMichel Weststratebefore adoption4c4fecc
lines 209–210why, one step back209 if ((key as any) === DRAFT_STATE) return210 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 211–212why, one step back211 if ((base_ as any)[key] === undefined && !has(base_, key)) {212 assigned_[key] = truescramble plugin names, -56Michel Weststratebefore adoption037b474
line 213why, one step back213 markChangedES5_(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 214why, one step back214 } else if (!assigned_[key]) {TSDXMichel Weststratebefore adoption4c4fecc
line 215why, one step back215 // Only untouched properties trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 216why, one step back216 markChangesRecursively(draft_[key])More WIPMichel Weststratebefore adoptionee1c977
line 217why, one step back217 }TSDXMichel Weststratebefore adoption4c4fecc
lines 218–219why, one step back218 })219 // Look for removed keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 220why, one step back220 each(base_, key => {TSDXMichel Weststratebefore adoption4c4fecc
line 221why, one step back221 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 222–223why, one step back222 if (draft_[key] === undefined && !has(draft_, key)) {223 assigned_[key] = falsescramble plugin names, -56Michel Weststratebefore adoption037b474
line 224why, one step back224 markChangedES5_(state)More WIPMichel Weststratebefore adoptionee1c977
line 225why, one step back225 }TSDXMichel Weststratebefore adoption4c4fecc
line 226why, one step back226 })Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 227why, one step back227 } else if (type_ === ProxyTypeES5Array) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 228why, one step back228 if (hasArrayChanges(state as ES5ArrayState)) {scramble plugin names, -56Michel Weststratebefore adoption037b474
line 229why, one step back229 markChangedES5_(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 230why, one step back230 assigned_.length = trueTSDXMichel Weststratebefore adoption4c4fecc
line 231why, one step back231 }More WIPMichel Weststratebefore adoptionee1c977
line 232why, one step back232Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 233–234why, one step back233 if (draft_.length < base_.length) {234 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = falseTSDXMichel Weststratebefore adoption4c4fecc
line 235why, one step back235 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 236why, one step back236 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = trueTSDXMichel Weststratebefore adoption4c4fecc
line 237why, one step back237 }More WIPMichel Weststratebefore adoptionee1c977
line 238why, one step back238TSDXMichel Weststratebefore adoption4c4fecc
line 239why, one step back239 // Minimum count is enough, the other parts has been processed.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 240why, one step back240 const min = Math.min(draft_.length, base_.length)TSDXMichel Weststratebefore adoption4c4fecc
lines 241–243why, one step back241242 for (let i = 0; i < min; i++) {243 // Only untouched indices trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 244why, one step back244 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])More WIPMichel Weststratebefore adoptionee1c977
lines 245–246why, one step back245 }246 }TSDXMichel Weststratebefore adoption4c4fecc
line 247why, one step back247 }More WIPMichel Weststratebefore adoptionee1c977
line 248why, one step back248TSDXMichel Weststratebefore adoption4c4fecc
line 249why, one step back249 function hasObjectChanges(state: ES5ObjectState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 250why, one step back250 const {base_, draft_} = stateMore WIPMichel Weststratebefore adoptionee1c977
line 251why, one step back251TSDXMichel Weststratebefore adoption4c4fecc
lines 252–253why, one step back252 // Search for added keys and changed keys. Start at the back, because253 // non-numeric keys are ordered by time of definition on the object.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 254why, one step back254 const keys = Object.keys(draft_)TSDXMichel Weststratebefore adoption4c4fecc
lines 255–256why, one step back255 for (let i = keys.length - 1; i >= 0; i--) {256 const key = keys[i]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 257why, one step back257 const baseValue = base_[key]TSDXMichel Weststratebefore adoption4c4fecc
line 258why, one step back258 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 259why, one step back259 if (baseValue === undefined && !has(base_, key)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 260–264why, one step back260 return true261 }262 // Once a base key is deleted, future changes go undetected, because its263 // descriptor is erased. This branch detects any missed changes.264 else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 265–267why, one step back265 const value = draft_[key]266 const state: ImmerState = value && value[DRAFT_STATE]267 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {More WIPMichel Weststratebefore adoptionee1c977
lines 268–272why, one step back268 return true269 }270 }271 }272TSDXMichel Weststratebefore adoption4c4fecc
lines 273–274why, one step back273 // At this point, no keys were added or changed.274 // Compare key count to determine if keys were deleted.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 275why, one step back275 return keys.length !== Object.keys(base_).lengthTSDXMichel Weststratebefore adoption4c4fecc
line 276why, one step back276 }More WIPMichel Weststratebefore adoptionee1c977
line 277why, one step back277TSDXMichel Weststratebefore adoption4c4fecc
line 278why, one step back278 function hasArrayChanges(state: ES5ArrayState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 279–280why, one step back279 const {draft_} = state280 if (draft_.length !== state.base_.length) return trueTSDXMichel Weststratebefore adoption4c4fecc
lines 281–287why, one step back281 // See #116282 // If we first shorten the length, our array interceptors will be removed.283 // If after that new items are added, result in the same original length,284 // those last items will have no intercepting property.285 // So if there is no own descriptor on the last position, we know that items were removed and added286 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check287 // the last oneMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 288–291why, one step back288 const descriptor = Object.getOwnPropertyDescriptor(289 draft_,290 draft_.length - 1291 )TSDXMichel Weststratebefore adoption4c4fecc
lines 292–295why, one step back292 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)293 if (descriptor && !descriptor.get) return true294 // For all other cases, we don't have to compare, as they would have been picked up by the index setters295 return falseSome minor cleanupMichel Weststratebefore adoption718b7f6
lines 296–301why, one step back296 }297298 /*#__PURE__*/299 function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {300 const desc = Object.getOwnPropertyDescriptor(base, prop)301 return desc && desc.enumerable ? true : falsePreserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 302–305why, one step back302 }303304 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {305 if (state.revoked_) die(3, JSON.stringify(latest(state)))More WIPMichel Weststratebefore adoptionee1c977
line 306why, one step back306 }TSDXMichel Weststratebefore adoption4c4fecc
lines 307–308why, one step back307308 loadPlugin("es5", {scramble plugin names, -56Michel Weststratebefore adoption037b474
lines 309–311why, one step back309 createES5Proxy_,310 markChangedES5_,311 willFinalizeES5_TSDXMichel Weststratebefore adoption4c4fecc
lines 312–313why, one step back312 })313}