es5.ts
This file
Path
src/plugins/es5.ts
299 lines · 8.6 KiB · TypeScript
Last changed by
Small improvementsMichel Weststratebefore adoption712af5c
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.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 back51TSDXMichel Weststratebefore adoption4c4fecc
lines 52–56why, one step back52 function createES5Proxy<T>(53 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 = {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 64–75why, one step back64 type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),65 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 }77TSDXMichel Weststratebefore adoption4c4fecc
lines 78–80why, one step back78 createHiddenProperty(draft, DRAFT_STATE, state)79 return draft80 }More WIPMichel Weststratebefore adoptionee1c977
line 81why, one step back81TSDXMichel Weststratebefore adoption4c4fecc
lines 82–83why, one step back82 // Access a property without creating an Immer draft.83 function peek(draft: Drafted, prop: PropertyKey) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 84–86why, one step back84 const state: ES5State = draft[DRAFT_STATE]85 if (state && !state.finalizing_) {86 state.finalizing_ = trueTSDXMichel Weststratebefore adoption4c4fecc
line 87why, one step back87 const value = draft[prop]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 88why, one step back88 state.finalizing_ = falseMore WIPMichel Weststratebefore adoptionee1c977
lines 89–90why, one step back89 return value90 }TSDXMichel Weststratebefore adoption4c4fecc
lines 91–92why, one step back91 return draft[prop]92 }More WIPMichel Weststratebefore adoptionee1c977
line 93why, one step back93TSDXMichel Weststratebefore adoption4c4fecc
lines 94–96why, one step back94 function get(state: ES5State, prop: string | number) {95 assertUnrevoked(state)96 const value = peek(latest(state), prop)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 97why, one step back97 if (state.finalizing_) return valueTSDXMichel Weststratebefore adoption4c4fecc
line 98why, one step back98 // Create a draft if the value is unmodified.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 99why, one step back99 if (value === peek(state.base_, prop) && isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 100why, one step back100 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 101why, one step back101 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 102–106why, one step back102 return (state.copy_![prop] = createProxy(103 state.scope_.immer_,104 value,105 state106 ))More WIPMichel Weststratebefore adoptionee1c977
line 107why, one step back107 }TSDXMichel Weststratebefore adoption4c4fecc
lines 108–109why, one step back108 return value109 }More WIPMichel Weststratebefore adoptionee1c977
line 110why, one step back110TSDXMichel Weststratebefore adoption4c4fecc
lines 111–112why, one step back111 function set(state: ES5State, prop: string | number, value: any) {112 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 113–114why, one step back113 state.assigned_[prop] = true114 if (!state.modified_) {TSDXMichel Weststratebefore adoption4c4fecc
lines 115–117why, one step back115 if (is(value, peek(latest(state), prop))) return116 markChangedES5(state)117 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 118why, one step back118 }TSDXMichel Weststratebefore adoption4c4fecc
line 119why, one step back119 // @ts-ignoreMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 120why, one step back120 state.copy_![prop] = valueTSDXMichel Weststratebefore adoption4c4fecc
line 121why, one step back121 }More WIPMichel Weststratebefore adoptionee1c977
line 122why, one step back122TSDXMichel Weststratebefore adoption4c4fecc
line 123why, one step back123 function markChangedES5(state: ImmerState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 124–126why, one step back124 if (!state.modified_) {125 state.modified_ = true126 if (state.parent_) markChangedES5(state.parent_)More WIPMichel Weststratebefore adoptionee1c977
line 127why, one step back127 }TSDXMichel Weststratebefore adoption4c4fecc
line 128why, one step back128 }More WIPMichel Weststratebefore adoptionee1c977
line 129why, one step back129TSDXMichel Weststratebefore adoption4c4fecc
line 130why, one step back130 function prepareCopy(state: ES5State) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 131why, one step back131 if (!state.copy_) state.copy_ = clonePotentialDraft(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 132–134why, one step back132 }133134 function clonePotentialDraft(base: Objectish) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 135why, one step back135 const state: ES5State | undefined = base && (base as any)[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 136why, one step back136 if (state) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 137–139why, one step back137 state.finalizing_ = true138 const draft = shallowCopy(state.draft_, true)139 state.finalizing_ = falseTSDXMichel Weststratebefore adoption4c4fecc
line 140why, one step back140 return draftMore WIPMichel Weststratebefore adoptionee1c977
line 141why, one step back141 }TSDXMichel Weststratebefore adoption4c4fecc
lines 142–143why, one step back142 return shallowCopy(base)143 }More WIPMichel Weststratebefore adoptionee1c977
line 144why, one step back144TSDXMichel Weststratebefore adoption4c4fecc
lines 145–147why, one step back145 // property descriptors are recycled to make sure we don't create a get and set closure per property,146 // but share them all instead147 const descriptors: {[prop: string]: PropertyDescriptor} = {}More WIPMichel Weststratebefore adoptionee1c977
line 148why, one step back148TSDXMichel Weststratebefore adoption4c4fecc
lines 149–158why, one step back149 function proxyProperty(150 draft: Drafted<any, ES5State>,151 prop: string | number,152 enumerable: boolean153 ) {154 let desc = descriptors[prop]155 if (desc) {156 desc.enumerable = enumerable157 } else {158 descriptors[prop] = desc = {Some notesMichel Weststratebefore adoption746dc6f
line 159why, one step back159 // configurable: true,TSDXMichel Weststratebefore adoption4c4fecc
lines 160–165why, one step back160 enumerable,161 get(this: any) {162 return get(this[DRAFT_STATE], prop)163 },164 set(this: any, value) {165 set(this[DRAFT_STATE], prop, value)More WIPMichel Weststratebefore adoptionee1c977
lines 166–168why, one step back166 }167 }168 }TSDXMichel Weststratebefore adoption4c4fecc
lines 169–170why, one step back169 Object.defineProperty(draft, prop, desc)170 }More WIPMichel Weststratebefore adoptionee1c977
line 171why, one step back171TSDXMichel Weststratebefore adoption4c4fecc
lines 172–178why, one step back172 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.173 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {174 // The natural order of drafts in the `scope` array is based on when they175 // were accessed. By processing drafts in reverse natural order, we have a176 // better chance of processing leaf nodes first. When a leaf node is known to177 // have changed, we can avoid any traversal of its ancestor nodes.178 for (let i = drafts.length - 1; i >= 0; i--) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 179–181why, one step back179 const state: ES5State = drafts[i][DRAFT_STATE]180 if (!state.modified_) {181 switch (state.type_) {TSDXMichel Weststratebefore adoption4c4fecc
lines 182–187why, one step back182 case ProxyType.ES5Array:183 if (hasArrayChanges(state)) markChangedES5(state)184 break185 case ProxyType.ES5Object:186 if (hasObjectChanges(state)) markChangedES5(state)187 breakMore WIPMichel Weststratebefore adoptionee1c977
lines 188–190why, one step back188 }189 }190 }TSDXMichel Weststratebefore adoption4c4fecc
line 191why, one step back191 }More WIPMichel Weststratebefore adoptionee1c977
line 192why, one step back192TSDXMichel Weststratebefore adoption4c4fecc
lines 193–194why, one step back193 function markChangesRecursively(object: any) {194 if (!object || typeof object !== "object") returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 195why, one step back195 const state: ES5State | undefined = object[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 196why, one step back196 if (!state) returnMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 197–198why, one step back197 const {base_, draft_, assigned_, type_} = state198 if (type_ === ProxyType.ES5Object) {TSDXMichel Weststratebefore adoption4c4fecc
lines 199–203why, one step back199 // Look for added keys.200 // TODO: looks quite duplicate to hasObjectChanges,201 // probably there is a faster way to detect changes, as sweep + recurse seems to do some202 // unnecessary work.203 // also: probably we can store the information we detect here, to speed up tree finalization!Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 204why, one step back204 each(draft_, key => {TSDXMichel Weststratebefore adoption4c4fecc
lines 205–206why, one step back205 if ((key as any) === DRAFT_STATE) return206 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 207–208why, one step back207 if ((base_ as any)[key] === undefined && !has(base_, key)) {208 assigned_[key] = trueMore WIPMichel Weststratebefore adoptionee1c977
line 209why, one step back209 markChangedES5(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 210why, one step back210 } else if (!assigned_[key]) {TSDXMichel Weststratebefore adoption4c4fecc
line 211why, one step back211 // Only untouched properties trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 212why, one step back212 markChangesRecursively(draft_[key])More WIPMichel Weststratebefore adoptionee1c977
line 213why, one step back213 }TSDXMichel Weststratebefore adoption4c4fecc
lines 214–215why, one step back214 })215 // Look for removed keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 216why, one step back216 each(base_, key => {TSDXMichel Weststratebefore adoption4c4fecc
line 217why, one step back217 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 218–219why, one step back218 if (draft_[key] === undefined && !has(draft_, key)) {219 assigned_[key] = falseTSDXMichel Weststratebefore adoption4c4fecc
line 220why, one step back220 markChangedES5(state)More WIPMichel Weststratebefore adoptionee1c977
line 221why, one step back221 }TSDXMichel Weststratebefore adoption4c4fecc
line 222why, one step back222 })Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 223–224why, one step back223 } else if (type_ === ProxyType.ES5Array) {224 if (hasArrayChanges(state as ES5ArrayState)) {TSDXMichel Weststratebefore adoption4c4fecc
line 225why, one step back225 markChangedES5(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 226why, one step back226 assigned_.length = trueTSDXMichel Weststratebefore adoption4c4fecc
line 227why, one step back227 }More WIPMichel Weststratebefore adoptionee1c977
line 228why, one step back228Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 229–230why, one step back229 if (draft_.length < base_.length) {230 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = falseTSDXMichel Weststratebefore adoption4c4fecc
line 231why, one step back231 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 232why, one step back232 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = trueTSDXMichel Weststratebefore adoption4c4fecc
line 233why, one step back233 }More WIPMichel Weststratebefore adoptionee1c977
line 234why, one step back234TSDXMichel Weststratebefore adoption4c4fecc
line 235why, one step back235 // Minimum count is enough, the other parts has been processed.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 236why, one step back236 const min = Math.min(draft_.length, base_.length)TSDXMichel Weststratebefore adoption4c4fecc
lines 237–239why, one step back237238 for (let i = 0; i < min; i++) {239 // Only untouched indices trigger recursion.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 240why, one step back240 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])More WIPMichel Weststratebefore adoptionee1c977
lines 241–242why, one step back241 }242 }TSDXMichel Weststratebefore adoption4c4fecc
line 243why, one step back243 }More WIPMichel Weststratebefore adoptionee1c977
line 244why, one step back244TSDXMichel Weststratebefore adoption4c4fecc
line 245why, one step back245 function hasObjectChanges(state: ES5ObjectState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 246why, one step back246 const {base_, draft_} = stateMore WIPMichel Weststratebefore adoptionee1c977
line 247why, one step back247TSDXMichel Weststratebefore adoption4c4fecc
lines 248–249why, one step back248 // Search for added keys and changed keys. Start at the back, because249 // non-numeric keys are ordered by time of definition on the object.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 250why, one step back250 const keys = Object.keys(draft_)TSDXMichel Weststratebefore adoption4c4fecc
lines 251–252why, one step back251 for (let i = keys.length - 1; i >= 0; i--) {252 const key = keys[i]Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 253why, one step back253 const baseValue = base_[key]TSDXMichel Weststratebefore adoption4c4fecc
line 254why, one step back254 // The `undefined` check is a fast path for pre-existing keys.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 255why, one step back255 if (baseValue === undefined && !has(base_, key)) {TSDXMichel Weststratebefore adoption4c4fecc
lines 256–260why, one step back256 return true257 }258 // Once a base key is deleted, future changes go undetected, because its259 // descriptor is erased. This branch detects any missed changes.260 else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 261–263why, one step back261 const value = draft_[key]262 const state: ImmerState = value && value[DRAFT_STATE]263 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {More WIPMichel Weststratebefore adoptionee1c977
lines 264–268why, one step back264 return true265 }266 }267 }268TSDXMichel Weststratebefore adoption4c4fecc
lines 269–270why, one step back269 // At this point, no keys were added or changed.270 // Compare key count to determine if keys were deleted.Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 271why, one step back271 return keys.length !== Object.keys(base_).lengthTSDXMichel Weststratebefore adoption4c4fecc
line 272why, one step back272 }More WIPMichel Weststratebefore adoptionee1c977
line 273why, one step back273TSDXMichel Weststratebefore adoption4c4fecc
line 274why, one step back274 function hasArrayChanges(state: ES5ArrayState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 275–276why, one step back275 const {draft_} = state276 if (draft_.length !== state.base_.length) return trueTSDXMichel Weststratebefore adoption4c4fecc
lines 277–283why, one step back277 // See #116278 // If we first shorten the length, our array interceptors will be removed.279 // If after that new items are added, result in the same original length,280 // those last items will have no intercepting property.281 // So if there is no own descriptor on the last position, we know that items were removed and added282 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check283 // the last oneMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 284–287why, one step back284 const descriptor = Object.getOwnPropertyDescriptor(285 draft_,286 draft_.length - 1287 )TSDXMichel Weststratebefore adoption4c4fecc
lines 288–291why, one step back288 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)289 if (descriptor && !descriptor.get) return true290 // For all other cases, we don't have to compare, as they would have been picked up by the index setters291 return falseMore WIPMichel Weststratebefore adoptionee1c977
line 292why, one step back292 }TSDXMichel Weststratebefore adoption4c4fecc
lines 293–299why, one step back293294 loadPlugin("es5", {295 createES5Proxy,296 markChangedES5,297 willFinalizeES5298 })299}