es5.ts
This file
Path
src/plugins/es5.ts
288 lines · 8.2 KiB · TypeScript
Last changed by
Some measurements fixesMichel Weststratebefore adoption737432a
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–37why, one step back26export function enableES5() {27 function willFinalizeES5(28 scope: ImmerScope,29 result: any,30 isReplaced: boolean31 ) {32 scope.drafts!.forEach((draft: any) => {33 draft[DRAFT_STATE].finalizing = true34 })35 if (!isReplaced) {36 if (scope.patches) {37 markChangesRecursively(scope.drafts![0])More WIPMichel Weststratebefore adoptionee1c977
line 38why, one step back38 }TSDXMichel Weststratebefore adoption4c4fecc
lines 39–40why, one step back39 // This is faster when we don't care about which attributes changed.40 markChangesSweep(scope.drafts)More WIPMichel Weststratebefore adoptionee1c977
line 41why, one step back41 }TSDXMichel Weststratebefore adoption4c4fecc
lines 42–46why, one step back42 // When a child draft is returned, look for changes.43 else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {44 markChangesSweep(scope.drafts)45 }46 }More WIPMichel Weststratebefore adoptionee1c977
line 47why, one step back47TSDXMichel Weststratebefore adoption4c4fecc
lines 48–53why, one step back48 function createES5Proxy<T>(49 base: T,50 parent?: ImmerState51 ): Drafted<T, ES5ObjectState | ES5ArrayState> {52 const isArray = Array.isArray(base)53 const draft = clonePotentialDraft(base)More WIPMichel Weststratebefore adoptionee1c977
line 54why, one step back54TSDXMichel Weststratebefore adoption4c4fecc
lines 55–57why, one step back55 each(draft, prop => {56 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))57 })More WIPMichel Weststratebefore adoptionee1c977
line 58why, one step back58TSDXMichel Weststratebefore adoption4c4fecc
lines 59–71why, one step back59 const state: ES5ObjectState | ES5ArrayState = {60 type: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),61 scope: parent ? parent.scope : ImmerScope.current!,62 modified: false,63 finalizing: false,64 finalized: false,65 assigned: {},66 parent,67 base,68 draft,69 copy: null,70 revoked: false,71 isManual: falseMore WIPMichel Weststratebefore adoptionee1c977
lines 72–73why, one step back72 }73TSDXMichel Weststratebefore adoption4c4fecc
lines 74–76why, one step back74 createHiddenProperty(draft, DRAFT_STATE, state)75 return draft76 }More WIPMichel Weststratebefore adoptionee1c977
line 77why, one step back77TSDXMichel Weststratebefore adoption4c4fecc
lines 78–84why, one step back78 // Access a property without creating an Immer draft.79 function peek(draft: Drafted, prop: PropertyKey) {80 const state = draft[DRAFT_STATE]81 if (state && !state.finalizing) {82 state.finalizing = true83 const value = draft[prop]84 state.finalizing = falseMore WIPMichel Weststratebefore adoptionee1c977
lines 85–86why, one step back85 return value86 }TSDXMichel Weststratebefore adoption4c4fecc
lines 87–88why, one step back87 return draft[prop]88 }More WIPMichel Weststratebefore adoptionee1c977
line 89why, one step back89TSDXMichel Weststratebefore adoption4c4fecc
lines 90–96why, one step back90 function get(state: ES5State, prop: string | number) {91 assertUnrevoked(state)92 const value = peek(latest(state), prop)93 if (state.finalizing) return value94 // Create a draft if the value is unmodified.95 if (value === peek(state.base, prop) && isDraftable(value)) {96 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 97why, one step back97 // @ts-ignoreNew build set. Current statsMichel Weststratebefore adoption9aec0fe
line 98why, one step back98 return (state.copy![prop] = createProxy(state.scope.immer, value, state))More WIPMichel Weststratebefore adoptionee1c977
line 99why, one step back99 }TSDXMichel Weststratebefore adoption4c4fecc
lines 100–101why, one step back100 return value101 }More WIPMichel Weststratebefore adoptionee1c977
line 102why, one step back102TSDXMichel Weststratebefore adoption4c4fecc
lines 103–109why, one step back103 function set(state: ES5State, prop: string | number, value: any) {104 assertUnrevoked(state)105 state.assigned[prop] = true106 if (!state.modified) {107 if (is(value, peek(latest(state), prop))) return108 markChangedES5(state)109 prepareCopy(state)More WIPMichel Weststratebefore adoptionee1c977
line 110why, one step back110 }TSDXMichel Weststratebefore adoption4c4fecc
lines 111–113why, one step back111 // @ts-ignore112 state.copy![prop] = value113 }More WIPMichel Weststratebefore adoptionee1c977
line 114why, one step back114TSDXMichel Weststratebefore adoption4c4fecc
lines 115–118why, one step back115 function markChangedES5(state: ImmerState) {116 if (!state.modified) {117 state.modified = true118 if (state.parent) markChangedES5(state.parent)More WIPMichel Weststratebefore adoptionee1c977
line 119why, one step back119 }TSDXMichel Weststratebefore adoption4c4fecc
line 120why, one step back120 }More WIPMichel Weststratebefore adoptionee1c977
line 121why, one step back121TSDXMichel Weststratebefore adoption4c4fecc
lines 122–132why, one step back122 function prepareCopy(state: ES5State) {123 if (!state.copy) state.copy = clonePotentialDraft(state.base)124 }125126 function clonePotentialDraft(base: Objectish) {127 const state = base && (base as any)[DRAFT_STATE]128 if (state) {129 state.finalizing = true130 const draft = shallowCopy(state.draft, true)131 state.finalizing = false132 return draftMore WIPMichel Weststratebefore adoptionee1c977
line 133why, one step back133 }TSDXMichel Weststratebefore adoption4c4fecc
lines 134–135why, one step back134 return shallowCopy(base)135 }More WIPMichel Weststratebefore adoptionee1c977
line 136why, one step back136TSDXMichel Weststratebefore adoption4c4fecc
lines 137–139why, one step back137 // property descriptors are recycled to make sure we don't create a get and set closure per property,138 // but share them all instead139 const descriptors: {[prop: string]: PropertyDescriptor} = {}More WIPMichel Weststratebefore adoptionee1c977
line 140why, one step back140TSDXMichel Weststratebefore adoption4c4fecc
lines 141–157why, one step back141 function proxyProperty(142 draft: Drafted<any, ES5State>,143 prop: string | number,144 enumerable: boolean145 ) {146 let desc = descriptors[prop]147 if (desc) {148 desc.enumerable = enumerable149 } else {150 descriptors[prop] = desc = {151 configurable: true,152 enumerable,153 get(this: any) {154 return get(this[DRAFT_STATE], prop)155 },156 set(this: any, value) {157 set(this[DRAFT_STATE], prop, value)More WIPMichel Weststratebefore adoptionee1c977
lines 158–160why, one step back158 }159 }160 }TSDXMichel Weststratebefore adoption4c4fecc
lines 161–162why, one step back161 Object.defineProperty(draft, prop, desc)162 }More WIPMichel Weststratebefore adoptionee1c977
line 163why, one step back163TSDXMichel Weststratebefore adoption4c4fecc
lines 164–179why, one step back164 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.165 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {166 // The natural order of drafts in the `scope` array is based on when they167 // were accessed. By processing drafts in reverse natural order, we have a168 // better chance of processing leaf nodes first. When a leaf node is known to169 // have changed, we can avoid any traversal of its ancestor nodes.170 for (let i = drafts.length - 1; i >= 0; i--) {171 const state = drafts[i][DRAFT_STATE]172 if (!state.modified) {173 switch (state.type) {174 case ProxyType.ES5Array:175 if (hasArrayChanges(state)) markChangedES5(state)176 break177 case ProxyType.ES5Object:178 if (hasObjectChanges(state)) markChangedES5(state)179 breakMore WIPMichel Weststratebefore adoptionee1c977
lines 180–182why, one step back180 }181 }182 }TSDXMichel Weststratebefore adoption4c4fecc
line 183why, one step back183 }More WIPMichel Weststratebefore adoptionee1c977
line 184why, one step back184TSDXMichel Weststratebefore adoption4c4fecc
lines 185–200why, one step back185 function markChangesRecursively(object: any) {186 if (!object || typeof object !== "object") return187 const state = object[DRAFT_STATE]188 if (!state) return189 const {base, draft, assigned, type} = state190 if (type === ProxyType.ES5Object) {191 // Look for added keys.192 // TODO: looks quite duplicate to hasObjectChanges,193 // probably there is a faster way to detect changes, as sweep + recurse seems to do some194 // unnecessary work.195 // also: probably we can store the information we detect here, to speed up tree finalization!196 each(draft, key => {197 if ((key as any) === DRAFT_STATE) return198 // The `undefined` check is a fast path for pre-existing keys.199 if (base[key] === undefined && !has(base, key)) {200 assigned[key] = trueMore WIPMichel Weststratebefore adoptionee1c977
line 201why, one step back201 markChangedES5(state)TSDXMichel Weststratebefore adoption4c4fecc
lines 202–204why, one step back202 } else if (!assigned[key]) {203 // Only untouched properties trigger recursion.204 markChangesRecursively(draft[key])More WIPMichel Weststratebefore adoptionee1c977
line 205why, one step back205 }TSDXMichel Weststratebefore adoption4c4fecc
lines 206–212why, one step back206 })207 // Look for removed keys.208 each(base, key => {209 // The `undefined` check is a fast path for pre-existing keys.210 if (draft[key] === undefined && !has(draft, key)) {211 assigned[key] = false212 markChangedES5(state)More WIPMichel Weststratebefore adoptionee1c977
line 213why, one step back213 }TSDXMichel Weststratebefore adoption4c4fecc
lines 214–219why, one step back214 })215 } else if (type === ProxyType.ES5Array) {216 if (hasArrayChanges(state)) {217 markChangedES5(state)218 assigned.length = true219 }More WIPMichel Weststratebefore adoptionee1c977
line 220why, one step back220TSDXMichel Weststratebefore adoption4c4fecc
lines 221–225why, one step back221 if (draft.length < base.length) {222 for (let i = draft.length; i < base.length; i++) assigned[i] = false223 } else {224 for (let i = base.length; i < draft.length; i++) assigned[i] = true225 }More WIPMichel Weststratebefore adoptionee1c977
line 226why, one step back226TSDXMichel Weststratebefore adoption4c4fecc
lines 227–232why, one step back227 // Minimum count is enough, the other parts has been processed.228 const min = Math.min(draft.length, base.length)229230 for (let i = 0; i < min; i++) {231 // Only untouched indices trigger recursion.232 if (assigned[i] === undefined) markChangesRecursively(draft[i])More WIPMichel Weststratebefore adoptionee1c977
lines 233–234why, one step back233 }234 }TSDXMichel Weststratebefore adoption4c4fecc
line 235why, one step back235 }More WIPMichel Weststratebefore adoptionee1c977
line 236why, one step back236TSDXMichel Weststratebefore adoption4c4fecc
lines 237–238why, one step back237 function hasObjectChanges(state: ES5ObjectState) {238 const {base, draft} = stateMore WIPMichel Weststratebefore adoptionee1c977
line 239why, one step back239TSDXMichel Weststratebefore adoption4c4fecc
lines 240–255why, one step back240 // Search for added keys and changed keys. Start at the back, because241 // non-numeric keys are ordered by time of definition on the object.242 const keys = Object.keys(draft)243 for (let i = keys.length - 1; i >= 0; i--) {244 const key = keys[i]245 const baseValue = base[key]246 // The `undefined` check is a fast path for pre-existing keys.247 if (baseValue === undefined && !has(base, key)) {248 return true249 }250 // Once a base key is deleted, future changes go undetected, because its251 // descriptor is erased. This branch detects any missed changes.252 else {253 const value = draft[key]254 const state = value && value[DRAFT_STATE]255 if (state ? state.base !== baseValue : !is(value, baseValue)) {More WIPMichel Weststratebefore adoptionee1c977
lines 256–260why, one step back256 return true257 }258 }259 }260TSDXMichel Weststratebefore adoption4c4fecc
lines 261–264why, one step back261 // At this point, no keys were added or changed.262 // Compare key count to determine if keys were deleted.263 return keys.length !== Object.keys(base).length264 }More WIPMichel Weststratebefore adoptionee1c977
line 265why, one step back265TSDXMichel Weststratebefore adoption4c4fecc
lines 266–280why, one step back266 function hasArrayChanges(state: ES5ArrayState) {267 const {draft} = state268 if (draft.length !== state.base.length) return true269 // See #116270 // If we first shorten the length, our array interceptors will be removed.271 // If after that new items are added, result in the same original length,272 // those last items will have no intercepting property.273 // So if there is no own descriptor on the last position, we know that items were removed and added274 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check275 // the last one276 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)277 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)278 if (descriptor && !descriptor.get) return true279 // For all other cases, we don't have to compare, as they would have been picked up by the index setters280 return falseMore WIPMichel Weststratebefore adoptionee1c977
line 281why, one step back281 }TSDXMichel Weststratebefore adoption4c4fecc
lines 282–288why, one step back282283 loadPlugin("es5", {284 createES5Proxy,285 markChangedES5,286 willFinalizeES5287 })288}