es5.ts
This file
Path
src/es5.ts
303 lines · 8.5 KiB · TypeScript
Last changed by
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
History of this fileTake it
feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 1–9why, one step back1"use strict"2import {3 each,4 has,5 is,6 isDraft,7 isDraftable,8 isEnumerable,9 shallowCopy,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 10why, one step back10 latest,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 11–12why, one step back11 createHiddenProperty,12 ImmerScope,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 13–18why, one step back13 ImmerState,14 Drafted,15 AnyObject,16 Objectish,17 ImmerBaseState,18 AnyArray,fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 19–23why, one step back19 ProxyType,20 MapState,21 SetState,22 DRAFT_STATE23} from "./internal"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 24why, one step back24feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 25why, one step back25interface ES5BaseState extends ImmerBaseState {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 26why, one step back26 finalizing: booleanfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 27–28why, one step back27 assigned: {[key: string]: any}28 parent?: ImmerStatefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 29–31why, one step back29 revoked: boolean30}31feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 32–48why, one step back32export interface ES5ObjectState extends ES5BaseState {33 type: ProxyType.ES5Object34 draft: Drafted<AnyObject, ES5ObjectState>35 base: AnyObject36 copy: AnyObject | null37}3839export interface ES5ArrayState extends ES5BaseState {40 type: ProxyType.ES5Array41 draft: Drafted<AnyObject, ES5ArrayState>42 base: AnyArray43 copy: AnyArray | null44}4546type ES5State = ES5ArrayState | ES5ObjectState4748export function willFinalizeES5(feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 49–68why, one step back49 scope: ImmerScope,50 result: any,51 isReplaced: boolean52) {53 scope.drafts!.forEach(draft => {54 draft[DRAFT_STATE].finalizing = true55 })56 if (!isReplaced) {57 if (scope.patches) {58 markChangesRecursively(scope.drafts![0])59 }60 // This is faster when we don't care about which attributes changed.61 markChangesSweep(scope.drafts)62 }63 // When a child draft is returned, look for changes.64 else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {65 markChangesSweep(scope.drafts)66 }67}68feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 69–72why, one step back69export function createES5Proxy<T>(70 base: T,71 parent?: ImmerState72): Drafted<T, ES5ObjectState | ES5ArrayState> {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 73–75why, one step back73 const isArray = Array.isArray(base)74 const draft = clonePotentialDraft(base)75feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 76–78why, one step back76 each(draft, prop => {77 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))78 })feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 79why, one step back79feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 80–82why, one step back80 const state: ES5ObjectState | ES5ArrayState = {81 type: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),82 scope: parent ? parent.scope : ImmerScope.current!,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 83why, one step back83 modified: false,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 84why, one step back84 finalizing: false,feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 85why, one step back85 finalized: false,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 86why, one step back86 assigned: {},feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 87–90why, one step back87 parent,88 base,89 draft,90 copy: null,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 91–92why, one step back91 revoked: false,92 isManual: falsefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 93–99why, one step back93 }9495 createHiddenProperty(draft, DRAFT_STATE, state)96 return draft97}9899// Access a property without creating an Immer draft.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 100why, one step back100function peek(draft: Drafted, prop: PropertyKey) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 101–110why, one step back101 const state = draft[DRAFT_STATE]102 if (state && !state.finalizing) {103 state.finalizing = true104 const value = draft[prop]105 state.finalizing = false106 return value107 }108 return draft[prop]109}110feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 111why, one step back111function get(state: ES5State, prop: string | number) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 112–117why, one step back112 assertUnrevoked(state)113 const value = peek(latest(state), prop)114 if (state.finalizing) return value115 // Create a draft if the value is unmodified.116 if (value === peek(state.base, prop) && isDraftable(value)) {117 prepareCopy(state)feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 118–119why, one step back118 // @ts-ignore119 return (state.copy![prop] = state.scope.immer.createProxy(value, state))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 120–123why, one step back120 }121 return value122}123feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 124why, one step back124function set(state: ES5State, prop: string | number, value: any) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 125–128why, one step back125 assertUnrevoked(state)126 state.assigned[prop] = true127 if (!state.modified) {128 if (is(value, peek(latest(state), prop))) returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 129why, one step back129 markChangedES5(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 130–131why, one step back130 prepareCopy(state)131 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 132–133why, one step back132 // @ts-ignore133 state.copy![prop] = valuefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 134–135why, one step back134}135feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 136why, one step back136export function markChangedES5(state: ImmerState) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 137–138why, one step back137 if (!state.modified) {138 state.modified = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 139why, one step back139 if (state.parent) markChangedES5(state.parent)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 140–142why, one step back140 }141}142feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 143why, one step back143function prepareCopy(state: ES5State) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 144–146why, one step back144 if (!state.copy) state.copy = clonePotentialDraft(state.base)145}146feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 147–148why, one step back147function clonePotentialDraft(base: Objectish) {148 const state = base && (base as any)[DRAFT_STATE]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 149–159why, one step back149 if (state) {150 state.finalizing = true151 const draft = shallowCopy(state.draft, true)152 state.finalizing = false153 return draft154 }155 return shallowCopy(base)156}157158// property descriptors are recycled to make sure we don't create a get and set closure per property,159// but share them all insteadfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 160why, one step back160const descriptors: {[prop: string]: PropertyDescriptor} = {}feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 161why, one step back161feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 162–166why, one step back162function proxyProperty(163 draft: Drafted<any, ES5State>,164 prop: string | number,165 enumerable: boolean166) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 167–173why, one step back167 let desc = descriptors[prop]168 if (desc) {169 desc.enumerable = enumerable170 } else {171 descriptors[prop] = desc = {172 configurable: true,173 enumerable,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 174why, one step back174 get(this: any) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 175–176why, one step back175 return get(this[DRAFT_STATE], prop)176 },feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 177why, one step back177 set(this: any, value) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 178–184why, one step back178 set(this[DRAFT_STATE], prop, value)179 }180 }181 }182 Object.defineProperty(draft, prop, desc)183}184feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 185why, one step back185export function assertUnrevoked(state: ES5State | MapState | SetState) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 186–193why, one step back186 if (state.revoked === true)187 throw new Error(188 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +189 JSON.stringify(latest(state))190 )191}192193// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 194why, one step back194function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 195–201why, one step back195 // The natural order of drafts in the `scope` array is based on when they196 // were accessed. By processing drafts in reverse natural order, we have a197 // better chance of processing leaf nodes first. When a leaf node is known to198 // have changed, we can avoid any traversal of its ancestor nodes.199 for (let i = drafts.length - 1; i >= 0; i--) {200 const state = drafts[i][DRAFT_STATE]201 if (!state.modified) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 202–208why, one step back202 switch (state.type) {203 case ProxyType.ES5Array:204 if (hasArrayChanges(state)) markChangedES5(state)205 break206 case ProxyType.ES5Object:207 if (hasObjectChanges(state)) markChangedES5(state)208 breakfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 209–213why, one step back209 }210 }211 }212}213feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 214why, one step back214function markChangesRecursively(object: any) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 215–217why, one step back215 if (!object || typeof object !== "object") return216 const state = object[DRAFT_STATE]217 if (!state) returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 218–219why, one step back218 const {base, draft, assigned, type} = state219 if (type === ProxyType.ES5Object) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 220why, one step back220 // Look for added keys.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 221–226why, one step back221 // TODO: looks quite duplicate to hasObjectChanges,222 // probably there is a faster way to detect changes, as sweep + recurse seems to do some223 // unnecessary work.224 // also: probably we can store the information we detect here, to speed up tree finalization!225 each(draft, key => {226 if ((key as any) === DRAFT_STATE) returnfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 227–229why, one step back227 // The `undefined` check is a fast path for pre-existing keys.228 if (base[key] === undefined && !has(base, key)) {229 assigned[key] = truefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 230why, one step back230 markChangedES5(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 231–236why, one step back231 } else if (!assigned[key]) {232 // Only untouched properties trigger recursion.233 markChangesRecursively(draft[key])234 }235 })236 // Look for removed keys.feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 237why, one step back237 each(base, key => {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 238–240why, one step back238 // The `undefined` check is a fast path for pre-existing keys.239 if (draft[key] === undefined && !has(draft, key)) {240 assigned[key] = falsefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 241why, one step back241 markChangedES5(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 242–243why, one step back242 }243 })feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 244–245why, one step back244 } else if (type === ProxyType.ES5Array && hasArrayChanges(state)) {245 markChangedES5(state)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 246–258why, one step back246 assigned.length = true247 if (draft.length < base.length) {248 for (let i = draft.length; i < base.length; i++) assigned[i] = false249 } else {250 for (let i = base.length; i < draft.length; i++) assigned[i] = true251 }252 for (let i = 0; i < draft.length; i++) {253 // Only untouched indices trigger recursion.254 if (assigned[i] === undefined) markChangesRecursively(draft[i])255 }256 }257}258feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 259why, one step back259function hasObjectChanges(state: ES5ObjectState) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 260–287why, one step back260 const {base, draft} = state261262 // Search for added keys and changed keys. Start at the back, because263 // non-numeric keys are ordered by time of definition on the object.264 const keys = Object.keys(draft)265 for (let i = keys.length - 1; i >= 0; i--) {266 const key = keys[i]267 const baseValue = base[key]268 // The `undefined` check is a fast path for pre-existing keys.269 if (baseValue === undefined && !has(base, key)) {270 return true271 }272 // Once a base key is deleted, future changes go undetected, because its273 // descriptor is erased. This branch detects any missed changes.274 else {275 const value = draft[key]276 const state = value && value[DRAFT_STATE]277 if (state ? state.base !== baseValue : !is(value, baseValue)) {278 return true279 }280 }281 }282283 // At this point, no keys were added or changed.284 // Compare key count to determine if keys were deleted.285 return keys.length !== Object.keys(base).length286}287feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 288why, one step back288function hasArrayChanges(state: ES5ArrayState) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 289–303why, one step back289 const {draft} = state290 if (draft.length !== state.base.length) return true291 // See #116292 // If we first shorten the length, our array interceptors will be removed.293 // If after that new items are added, result in the same original length,294 // those last items will have no intercepting property.295 // So if there is no own descriptor on the last position, we know that items were removed and added296 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check297 // the last one298 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)299 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)300 if (descriptor && !descriptor.get) return true301 // For all other cases, we don't have to compare, as they would have been picked up by the index setters302 return false303}