es5.ts
This file
Path
src/plugins/es5.ts
298 lines · 8.5 KiB · TypeScript
Last changed by
Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
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–19why, one step back7 each,8 has,9 isDraft,10 isDraftable,11 isEnumerable,12 shallowCopy,13 latest,14 createHiddenProperty,15 ProxyType,16 DRAFT_STATE,17 assertUnrevoked,18 is,19 loadPlugin,New build set. Current statsMichel Weststratebefore adoption9aec0fe
lines 20–21why, one step back20 ImmerScope,21 createProxyMore WIPMichel Weststratebefore adoptionee1c977
lines 22–25why, one step back22} from "../internal"2324type ES5State = ES5ArrayState | ES5ObjectState25TSDXMichel Weststratebefore adoption4c4fecc
lines 26–31why, one step back26export function enableES5() {27 function willFinalizeES5(28 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.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 40why, one step back40 markChangesSweep(scope.drafts_)More WIPMichel Weststratebefore adoptionee1c977
line 41why, one step back41 }TSDXMichel Weststratebefore adoption4c4fecc
line 42why, one step back42 // When a child draft is returned, look for changes.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 43–47why, one step back43 else if (44 isDraft(result) &&45 (result[DRAFT_STATE] as ES5State).scope_ === scope46 ) {47 markChangesSweep(scope.drafts_)TSDXMichel Weststratebefore adoption4c4fecc
lines 48–49why, one step back48 }49 }More WIPMichel Weststratebefore adoptionee1c977
line 50why, one step back50TSDXMichel Weststratebefore adoption4c4fecc
lines 51–55why, one step back51 function createES5Proxy<T>(52 base: T,53 parent?: ImmerState54 ): Drafted<T, ES5ObjectState | ES5ArrayState> {55 const isArray = Array.isArray(base)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 56why, one step back56 const draft: any = clonePotentialDraft(base)More WIPMichel Weststratebefore adoptionee1c977
line 57why, one step back57TSDXMichel Weststratebefore adoption4c4fecc
lines 58–60why, one step back58 each(draft, prop => {59 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))60 })More WIPMichel Weststratebefore adoptionee1c977
line 61why, one step back61TSDXMichel Weststratebefore adoption4c4fecc
line 62why, one step back62 const state: ES5ObjectState | ES5ArrayState = {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 63–74why, one step back63 type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),64 scope_: parent ? parent.scope_ : ImmerScope.current_!,65 modified_: false,66 finalizing_: false,67 finalized_: false,68 assigned_: {},69 parent_: parent,70 base_: base,71 draft_: draft,72 copy_: null,73 revoked_: false,74 isManual_: falseMore WIPMichel Weststratebefore adoptionee1c977
lines 75–76why, one step back75 }76TSDXMichel Weststratebefore adoption4c4fecc
lines 77–79why, one step back77 createHiddenProperty(draft, DRAFT_STATE, state)78 return draft79 }More WIPMichel Weststratebefore adoptionee1c977
line 80why, one step back80TSDXMichel Weststratebefore adoption4c4fecc
lines 81–82why, one step back81 // Access a property without creating an Immer draft.82 function peek(draft: Drafted, prop: PropertyKey) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 83–85why, one step back83 const state: ES5State = draft[DRAFT_STATE]84 if (state && !state.finalizing_) {85 state.finalizing_ = trueTSDXMichel Weststratebefore adoption4c4fecc
line 86why, one step back86 const value = draft[prop]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 87why, one step back87 state.finalizing_ = falseMore WIPMichel Weststratebefore adoptionee1c977
lines 88–89why, one step back88 return value89 }TSDXMichel Weststratebefore adoption4c4fecc
lines 90–91why, one step back90 return draft[prop]91 }More WIPMichel Weststratebefore adoptionee1c977
line 92why, one step back92TSDXMichel Weststratebefore adoption4c4fecc
lines 93–95why, one step back93 function get(state: ES5State, prop: string | number) {94 assertUnrevoked(state)95 const value = peek(latest(state), prop)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 96why, one step back96 if (state.finalizing_) return valueTSDXMichel Weststratebefore adoption4c4fecc
line 97why, one step back97 // Create a draft if the value is unmodified.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 98why, one step back98 if (value === peek(state.base_, prop) && isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 99why, one step back99 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 100why, one step back100 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 101–105why, one step back101 return (state.copy_![prop] = createProxy(102 state.scope_.immer_,103 value,104 state105 ))More WIPMichel Weststratebefore adoptionee1c977
line 106why, one step back106 }TSDXMichel Weststratebefore adoption4c4fecc
lines 107–108why, one step back107 return value108 }More WIPMichel Weststratebefore adoptionee1c977
line 109why, one step back109TSDXMichel Weststratebefore adoption4c4fecc
lines 110–111why, one step back110 function set(state: ES5State, prop: string | number, value: any) {111 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 112–113why, one step back112 state.assigned_[prop] = true113 if (!state.modified_) {TSDXMichel Weststratebefore adoption4c4fecc
lines 114–116why, one step back114 if (is(value, peek(latest(state), prop))) return115 markChangedES5(state)116 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 117why, one step back117 }TSDXMichel Weststratebefore adoption4c4fecc
line 118why, one step back118 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 119why, one step back119 state.copy_![prop] = valueTSDXMichel Weststratebefore adoption4c4fecc
line 120why, one step back120 }More WIPMichel Weststratebefore adoptionee1c977
line 121why, one step back121TSDXMichel Weststratebefore adoption4c4fecc
line 122why, one step back122 function markChangedES5(state: ImmerState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 123–125why, one step back123 if (!state.modified_) {124 state.modified_ = true125 if (state.parent_) markChangedES5(state.parent_)More WIPMichel Weststratebefore adoptionee1c977
line 126why, one step back126 }TSDXMichel Weststratebefore adoption4c4fecc
line 127why, one step back127 }More WIPMichel Weststratebefore adoptionee1c977
line 128why, one step back128TSDXMichel Weststratebefore adoption4c4fecc
line 129why, one step back129 function prepareCopy(state: ES5State) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 130why, one step back130 if (!state.copy_) state.copy_ = clonePotentialDraft(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 131–133why, one step back131 }132133 function clonePotentialDraft(base: Objectish) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 134why, one step back134 const state: ES5State | undefined = base && (base as any)[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 135why, one step back135 if (state) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 136–138why, one step back136 state.finalizing_ = true137 const draft = shallowCopy(state.draft_, true)138 state.finalizing_ = falseTSDXMichel Weststratebefore adoption4c4fecc
line 139why, one step back139 return draftMore WIPMichel Weststratebefore adoptionee1c977
line 140why, one step back140 }TSDXMichel Weststratebefore adoption4c4fecc
lines 141–142why, one step back141 return shallowCopy(base)142 }More WIPMichel Weststratebefore adoptionee1c977
line 143why, one step back143TSDXMichel Weststratebefore adoption4c4fecc
lines 144–146why, one step back144 // property descriptors are recycled to make sure we don't create a get and set closure per property,145 // but share them all instead146 const descriptors: {[prop: string]: PropertyDescriptor} = {}More WIPMichel Weststratebefore adoptionee1c977
line 147why, one step back147TSDXMichel Weststratebefore adoption4c4fecc
lines 148–164why, one step back148 function proxyProperty(149 draft: Drafted<any, ES5State>,150 prop: string | number,151 enumerable: boolean152 ) {153 let desc = descriptors[prop]154 if (desc) {155 desc.enumerable = enumerable156 } else {157 descriptors[prop] = desc = {158 configurable: true,159 enumerable,160 get(this: any) {161 return get(this[DRAFT_STATE], prop)162 },163 set(this: any, value) {164 set(this[DRAFT_STATE], prop, value)More WIPMichel Weststratebefore adoptionee1c977
lines 165–167why, one step back165 }166 }167 }TSDXMichel Weststratebefore adoption4c4fecc
lines 168–169why, one step back168 Object.defineProperty(draft, prop, desc)169 }More WIPMichel Weststratebefore adoptionee1c977
line 170why, one step back170TSDXMichel Weststratebefore adoption4c4fecc
lines 171–177why, one step back171 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.172 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {173 // The natural order of drafts in the `scope` array is based on when they174 // were accessed. By processing drafts in reverse natural order, we have a175 // better chance of processing leaf nodes first. When a leaf node is known to176 // have changed, we can avoid any traversal of its ancestor nodes.177 for (let i = drafts.length - 1; i >= 0; i--) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 178–180why, one step back178 const state: ES5State = drafts[i][DRAFT_STATE]179 if (!state.modified_) {180 switch (state.type_) {TSDXMichel Weststratebefore adoption4c4fecc
lines 181–186why, one step back181 case ProxyType.ES5Array:182 if (hasArrayChanges(state)) markChangedES5(state)183 break184 case ProxyType.ES5Object:185 if (hasObjectChanges(state)) markChangedES5(state)186 breakMore WIPMichel Weststratebefore adoptionee1c977
lines 187–189why, one step back187 }188 }189 }TSDXMichel Weststratebefore adoption4c4fecc
line 190why, one step back190 }More WIPMichel Weststratebefore adoptionee1c977
line 191why, one step back191TSDXMichel Weststratebefore adoption4c4fecc
lines 192–193why, one step back192 function markChangesRecursively(object: any) {193 if (!object || typeof object !== "object") returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 194why, one step back194 const state: ES5State | undefined = object[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 195why, one step back195 if (!state) returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 196–197why, one step back196 const {base_, draft_, assigned_, type_} = state197 if (type_ === ProxyType.ES5Object) {TSDXMichel Weststratebefore adoption4c4fecc
lines 198–202why, one step back198 // Look for added keys.199 // TODO: looks quite duplicate to hasObjectChanges,200 // probably there is a faster way to detect changes, as sweep + recurse seems to do some201 // unnecessary work.202 // also: probably we can store the information we detect here, to speed up tree finalization!Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 203why, one step back203 each(draft_, key => {TSDXMichel Weststratebefore adoption4c4fecc
lines 204–205why, one step back204 if ((key as any) === DRAFT_STATE) return205 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 206–207why, one step back206 if ((base_ as any)[key] === undefined && !has(base_, key)) {207 assigned_[key] = trueMore WIPMichel Weststratebefore adoptionee1c977
line 208why, one step back208 markChangedES5(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 209why, one step back209 } else if (!assigned_[key]) {TSDXMichel Weststratebefore adoption4c4fecc
line 210why, one step back210 // Only untouched properties trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 211why, one step back211 markChangesRecursively(draft_[key])More WIPMichel Weststratebefore adoptionee1c977
line 212why, one step back212 }TSDXMichel Weststratebefore adoption4c4fecc
lines 213–214why, one step back213 })214 // Look for removed keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 215why, one step back215 each(base_, key => {TSDXMichel Weststratebefore adoption4c4fecc
line 216why, one step back216 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 217–218why, one step back217 if (draft_[key] === undefined && !has(draft_, key)) {218 assigned_[key] = falseTSDXMichel Weststratebefore adoption4c4fecc
line 219why, one step back219 markChangedES5(state)More WIPMichel Weststratebefore adoptionee1c977
line 220why, one step back220 }TSDXMichel Weststratebefore adoption4c4fecc
line 221why, one step back221 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 222–223why, one step back222 } else if (type_ === ProxyType.ES5Array) {223 if (hasArrayChanges(state as ES5ArrayState)) {TSDXMichel Weststratebefore adoption4c4fecc
line 224why, one step back224 markChangedES5(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 225why, one step back225 assigned_.length = trueTSDXMichel Weststratebefore adoption4c4fecc
line 226why, one step back226 }More WIPMichel Weststratebefore adoptionee1c977
line 227why, one step back227Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 228–229why, one step back228 if (draft_.length < base_.length) {229 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = falseTSDXMichel Weststratebefore adoption4c4fecc
line 230why, one step back230 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 231why, one step back231 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = trueTSDXMichel Weststratebefore adoption4c4fecc
line 232why, one step back232 }More WIPMichel Weststratebefore adoptionee1c977
line 233why, one step back233TSDXMichel Weststratebefore adoption4c4fecc
line 234why, one step back234 // Minimum count is enough, the other parts has been processed.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 235why, one step back235 const min = Math.min(draft_.length, base_.length)TSDXMichel Weststratebefore adoption4c4fecc
lines 236–238why, one step back236237 for (let i = 0; i < min; i++) {238 // Only untouched indices trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 239why, one step back239 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])More WIPMichel Weststratebefore adoptionee1c977
lines 240–241why, one step back240 }241 }TSDXMichel Weststratebefore adoption4c4fecc
line 242why, one step back242 }More WIPMichel Weststratebefore adoptionee1c977
line 243why, one step back243TSDXMichel Weststratebefore adoption4c4fecc
line 244why, one step back244 function hasObjectChanges(state: ES5ObjectState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 245why, one step back245 const {base_, draft_} = stateMore WIPMichel Weststratebefore adoptionee1c977
line 246why, one step back246TSDXMichel Weststratebefore adoption4c4fecc
lines 247–248why, one step back247 // Search for added keys and changed keys. Start at the back, because248 // non-numeric keys are ordered by time of definition on the object.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 249why, one step back249 const keys = Object.keys(draft_)TSDXMichel Weststratebefore adoption4c4fecc
lines 250–251why, one step back250 for (let i = keys.length - 1; i >= 0; i--) {251 const key = keys[i]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 252why, one step back252 const baseValue = base_[key]TSDXMichel Weststratebefore adoption4c4fecc
line 253why, one step back253 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 254why, one step back254 if (baseValue === undefined && !has(base_, key)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 255–259why, one step back255 return true256 }257 // Once a base key is deleted, future changes go undetected, because its258 // descriptor is erased. This branch detects any missed changes.259 else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 260–262why, one step back260 const value = draft_[key]261 const state: ImmerState = value && value[DRAFT_STATE]262 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {More WIPMichel Weststratebefore adoptionee1c977
lines 263–267why, one step back263 return true264 }265 }266 }267TSDXMichel Weststratebefore adoption4c4fecc
lines 268–269why, one step back268 // At this point, no keys were added or changed.269 // Compare key count to determine if keys were deleted.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 270why, one step back270 return keys.length !== Object.keys(base_).lengthTSDXMichel Weststratebefore adoption4c4fecc
line 271why, one step back271 }More WIPMichel Weststratebefore adoptionee1c977
line 272why, one step back272TSDXMichel Weststratebefore adoption4c4fecc
line 273why, one step back273 function hasArrayChanges(state: ES5ArrayState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 274–275why, one step back274 const {draft_} = state275 if (draft_.length !== state.base_.length) return trueTSDXMichel Weststratebefore adoption4c4fecc
lines 276–282why, one step back276 // See #116277 // If we first shorten the length, our array interceptors will be removed.278 // If after that new items are added, result in the same original length,279 // those last items will have no intercepting property.280 // So if there is no own descriptor on the last position, we know that items were removed and added281 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check282 // the last oneMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 283–286why, one step back283 const descriptor = Object.getOwnPropertyDescriptor(284 draft_,285 draft_.length - 1286 )TSDXMichel Weststratebefore adoption4c4fecc
lines 287–290why, one step back287 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)288 if (descriptor && !descriptor.get) return true289 // For all other cases, we don't have to compare, as they would have been picked up by the index setters290 return falseMore WIPMichel Weststratebefore adoptionee1c977
line 291why, one step back291 }TSDXMichel Weststratebefore adoption4c4fecc
lines 292–298why, one step back292293 loadPlugin("es5", {294 createES5Proxy,295 markChangedES5,296 willFinalizeES5297 })298}