es5.ts
This file
Path
src/es5.ts
309 lines · 8.4 KiB · TypeScript
Last changed by
Processed bunch of todosMichel Weststratebefore adoption00425e1
History of this fileTake it
Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 1–12why, one step back1"use strict"2import {3 each,4 has,5 is,6 isDraft,7 isDraftable,8 isEnumerable,9 isMap,10 isSet,11 shallowCopy,12 DRAFT_STATE,TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 13–14why, one step back13 latest,14 createHiddenPropertyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 15why, one step back15} from "./common"added set and iteror stuffMichel Weststratebefore adoption08f4682
line 16why, one step back16Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 17why, one step back17import {ImmerScope} from "./scope"Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 18–26why, one step back18import {19 ImmerState,20 Drafted,21 AnyObject,22 AnyMap,23 Objectish,24 ImmerBaseState,25 AnyArray26} from "./types"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 27why, one step back27Processed bunch of todosMichel Weststratebefore adoption00425e1
line 28why, one step back28interface ES5BaseState extends ImmerBaseState {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 29why, one step back29 finalizing: booleanIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 30why, one step back30 assigned: {[key: string]: any}clean up create proxyMichel Weststratebefore adoptionc182c1e
line 31why, one step back31 parent?: ImmerStateTS type improvementsMichel Weststratebefore adoptiondd1808e
line 32why, one step back32 revoke(): voidMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 33–35why, one step back33 revoked: boolean34}35Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 36–45why, one step back36export interface ES5ObjectState extends ES5BaseState {37 type: "es5_object"38 draft: Drafted<AnyObject, ES5ObjectState>39 base: AnyObject40 copy: AnyObject | null41}4243export interface ES5ArrayState extends ES5BaseState {44 type: "es5_array"45 draft: Drafted<AnyObject, ES5ArrayState>Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 46–47why, one step back46 base: AnyArray47 copy: AnyArray | nullIntroduced proper state typesMichel Weststratebefore adoption3842f85
lines 48–51why, one step back48}4950type ES5State = ES5ArrayState | ES5ObjectState51Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53
line 52why, one step back52export function willFinalizeES5(Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 53–72why, one step back53 scope: ImmerScope,54 result: any,55 isReplaced: boolean56) {57 scope.drafts!.forEach(draft => {58 draft[DRAFT_STATE].finalizing = true59 })60 if (!isReplaced) {61 if (scope.patches) {62 markChangesRecursively(scope.drafts![0])63 }64 // This is faster when we don't care about which attributes changed.65 markChangesSweep(scope.drafts)66 }67 // When a child draft is returned, look for changes.68 else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {69 markChangesSweep(scope.drafts)70 }71}72Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53
line 73why, one step back73export function createES5Proxy<T>(Introduced proper state typesMichel Weststratebefore adoption3842f85
line 74why, one step back74 base: T,clean up create proxyMichel Weststratebefore adoptionc182c1e
line 75why, one step back75 parent?: ImmerStateTS type improvementsMichel Weststratebefore adoptiondd1808e
line 76why, one step back76): Drafted<T, ES5ObjectState | ES5ArrayState> {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 77–79why, one step back77 const isArray = Array.isArray(base)78 const draft = clonePotentialDraft(base)79added set and iteror stuffMichel Weststratebefore adoption08f4682
lines 80–82why, one step back80 each(draft, prop => {81 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))82 })Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 83why, one step back83Introduced proper state typesMichel Weststratebefore adoption3842f85
lines 84–85why, one step back84 const state: ES5ObjectState | ES5ArrayState = {85 type: isArray ? "es5_array" : ("es5_object" as any),clean up create proxyMichel Weststratebefore adoptionc182c1e
line 86why, one step back86 scope: parent ? parent.scope : ImmerScope.current!,Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 87why, one step back87 modified: false,Processed bunch of todosMichel Weststratebefore adoption00425e1
line 88why, one step back88 finalizing: false,Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 89why, one step back89 finalized: false,clean up create proxyMichel Weststratebefore adoptionc182c1e
line 90why, one step back90 assigned: {},Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 91–95why, one step back91 parent,92 base,93 draft,94 copy: null,95 revoke,Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 96–97why, one step back96 revoked: false,97 isManual: falseMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 98–103why, one step back98 }99100 createHiddenProperty(draft, DRAFT_STATE, state)101 return draft102}103Introduced proper state typesMichel Weststratebefore adoption3842f85
line 104why, one step back104function revoke(this: ES5BaseState) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 105–108why, one step back105 this.revoked = true106}107108// Access a property without creating an Immer draft.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 109why, one step back109function peek(draft: Drafted, prop: PropertyKey) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 110–119why, one step back110 const state = draft[DRAFT_STATE]111 if (state && !state.finalizing) {112 state.finalizing = true113 const value = draft[prop]114 state.finalizing = false115 return value116 }117 return draft[prop]118}119TS type improvementsMichel Weststratebefore adoptiondd1808e
line 120why, one step back120function get(state: ES5State, prop: string | number) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 121–126why, one step back121 assertUnrevoked(state)122 const value = peek(latest(state), prop)123 if (state.finalizing) return value124 // Create a draft if the value is unmodified.125 if (value === peek(state.base, prop) && isDraftable(value)) {126 prepareCopy(state)Processed bunch of todosMichel Weststratebefore adoption00425e1
line 127why, one step back127 // @ts-ignoreTS type improvementsMichel Weststratebefore adoptiondd1808e
line 128why, one step back128 return (state.copy![prop] = state.scope.immer.createProxy(value, state))Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 129–132why, one step back129 }130 return value131}132TS type improvementsMichel Weststratebefore adoptiondd1808e
line 133why, one step back133function set(state: ES5State, prop: string | number, value: any) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 134–137why, one step back134 assertUnrevoked(state)135 state.assigned[prop] = true136 if (!state.modified) {137 if (is(value, peek(latest(state), prop))) returnStop abusing modules as exportsMichel Weststratebefore adoption7b0ad53
line 138why, one step back138 markChangedES5(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 139–140why, one step back139 prepareCopy(state)140 }Processed bunch of todosMichel Weststratebefore adoption00425e1
line 141why, one step back141 // @ts-ignoreIntroduced proper state typesMichel Weststratebefore adoption3842f85
line 142why, one step back142 state.copy![prop] = valueMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 143–144why, one step back143}144TS type improvementsMichel Weststratebefore adoptiondd1808e
line 145why, one step back145export function markChangedES5(state: ImmerState) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 146–147why, one step back146 if (!state.modified) {147 state.modified = trueStop abusing modules as exportsMichel Weststratebefore adoption7b0ad53
line 148why, one step back148 if (state.parent) markChangedES5(state.parent)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 149–151why, one step back149 }150}151TS type improvementsMichel Weststratebefore adoptiondd1808e
line 152why, one step back152function prepareCopy(state: ES5State) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 153–155why, one step back153 if (!state.copy) state.copy = clonePotentialDraft(state.base)154}155TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 156–157why, one step back156function clonePotentialDraft(base: Objectish) {157 const state = base && (base as any)[DRAFT_STATE]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 158–168why, one step back158 if (state) {159 state.finalizing = true160 const draft = shallowCopy(state.draft, true)161 state.finalizing = false162 return draft163 }164 return shallowCopy(base)165}166167// property descriptors are recycled to make sure we don't create a get and set closure per property,168// but share them all insteadTS type improvementsMichel Weststratebefore adoptiondd1808e
line 169why, one step back169const descriptors: {[prop: string]: PropertyDescriptor} = {}Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 170why, one step back170TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 171–175why, one step back171function proxyProperty(172 draft: Drafted<any, ES5State>,173 prop: string | number,174 enumerable: boolean175) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 176–182why, one step back176 let desc = descriptors[prop]177 if (desc) {178 desc.enumerable = enumerable179 } else {180 descriptors[prop] = desc = {181 configurable: true,182 enumerable,TS type improvementsMichel Weststratebefore adoptiondd1808e
line 183why, one step back183 get(this: any) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 184–185why, one step back184 return get(this[DRAFT_STATE], prop)185 },TS type improvementsMichel Weststratebefore adoptiondd1808e
line 186why, one step back186 set(this: any, value) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 187–193why, one step back187 set(this[DRAFT_STATE], prop, value)188 }189 }190 }191 Object.defineProperty(draft, prop, desc)192}193TS type improvementsMichel Weststratebefore adoptiondd1808e
line 194why, one step back194export function assertUnrevoked(state: ES5State) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 195–202why, one step back195 if (state.revoked === true)196 throw new Error(197 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +198 JSON.stringify(latest(state))199 )200}201202// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 203why, one step back203function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 204–210why, one step back204 // The natural order of drafts in the `scope` array is based on when they205 // were accessed. By processing drafts in reverse natural order, we have a206 // better chance of processing leaf nodes first. When a leaf node is known to207 // have changed, we can avoid any traversal of its ancestor nodes.208 for (let i = drafts.length - 1; i >= 0; i--) {209 const state = drafts[i][DRAFT_STATE]210 if (!state.modified) {TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 211–217why, one step back211 switch (state.type) {212 case "es5_array":213 if (hasArrayChanges(state)) markChangedES5(state)214 break215 case "es5_object":216 if (hasObjectChanges(state)) markChangedES5(state)217 breakMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 218–222why, one step back218 }219 }220 }221}222TS type improvementsMichel Weststratebefore adoptiondd1808e
line 223why, one step back223function markChangesRecursively(object: any) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 224–226why, one step back224 if (!object || typeof object !== "object") return225 const state = object[DRAFT_STATE]226 if (!state) returnTS type improvementsMichel Weststratebefore adoptiondd1808e
lines 227–228why, one step back227 const {base, draft, assigned, type} = state228 if (type === "es5_object") {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 229why, one step back229 // Look for added keys.TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 230–232why, one step back230 // TODO: looks quite duplicate to hasObjectChanges231 each(draft, key => {232 if ((key as any) === DRAFT_STATE) returnMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 233–235why, one step back233 // The `undefined` check is a fast path for pre-existing keys.234 if (base[key] === undefined && !has(base, key)) {235 assigned[key] = trueStop abusing modules as exportsMichel Weststratebefore adoption7b0ad53
line 236why, one step back236 markChangedES5(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 237–242why, one step back237 } else if (!assigned[key]) {238 // Only untouched properties trigger recursion.239 markChangesRecursively(draft[key])240 }241 })242 // Look for removed keys.TS type improvementsMichel Weststratebefore adoptiondd1808e
line 243why, one step back243 each(base, key => {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 244–246why, one step back244 // The `undefined` check is a fast path for pre-existing keys.245 if (draft[key] === undefined && !has(draft, key)) {246 assigned[key] = falseStop abusing modules as exportsMichel Weststratebefore adoption7b0ad53
line 247why, one step back247 markChangedES5(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 248–249why, one step back248 }249 })TS type improvementsMichel Weststratebefore adoptiondd1808e
line 250why, one step back250 } else if (type === "es5_array" && hasArrayChanges(state)) {Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53
line 251why, one step back251 markChangedES5(state)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 252–264why, one step back252 assigned.length = true253 if (draft.length < base.length) {254 for (let i = draft.length; i < base.length; i++) assigned[i] = false255 } else {256 for (let i = base.length; i < draft.length; i++) assigned[i] = true257 }258 for (let i = 0; i < draft.length; i++) {259 // Only untouched indices trigger recursion.260 if (assigned[i] === undefined) markChangesRecursively(draft[i])261 }262 }263}264TS type improvementsMichel Weststratebefore adoptiondd1808e
line 265why, one step back265function hasObjectChanges(state: ES5ObjectState) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 266–293why, one step back266 const {base, draft} = state267268 // Search for added keys and changed keys. Start at the back, because269 // non-numeric keys are ordered by time of definition on the object.270 const keys = Object.keys(draft)271 for (let i = keys.length - 1; i >= 0; i--) {272 const key = keys[i]273 const baseValue = base[key]274 // The `undefined` check is a fast path for pre-existing keys.275 if (baseValue === undefined && !has(base, key)) {276 return true277 }278 // Once a base key is deleted, future changes go undetected, because its279 // descriptor is erased. This branch detects any missed changes.280 else {281 const value = draft[key]282 const state = value && value[DRAFT_STATE]283 if (state ? state.base !== baseValue : !is(value, baseValue)) {284 return true285 }286 }287 }288289 // At this point, no keys were added or changed.290 // Compare key count to determine if keys were deleted.291 return keys.length !== Object.keys(base).length292}293TS type improvementsMichel Weststratebefore adoptiondd1808e
line 294why, one step back294function hasArrayChanges(state: ES5ArrayState) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 295–309why, one step back295 const {draft} = state296 if (draft.length !== state.base.length) return true297 // See #116298 // If we first shorten the length, our array interceptors will be removed.299 // If after that new items are added, result in the same original length,300 // those last items will have no intercepting property.301 // So if there is no own descriptor on the last position, we know that items were removed and added302 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check303 // the last one304 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)305 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)306 if (descriptor && !descriptor.get) return true307 // For all other cases, we don't have to compare, as they would have been picked up by the index setters308 return false309}