Skip to content
Myr

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 file

Take it

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 1–9why, one step back
1"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 back
10 latest,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 11–12why, one step back
11 createHiddenProperty,
12 ImmerScope,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 13–18why, one step back
13 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 back
19 ProxyType,
20 MapState,21 SetState,22 DRAFT_STATE23} from "./internal"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 24why, one step back
24

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 25why, one step back
25interface ES5BaseState extends ImmerBaseState {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 26why, one step back
26 finalizing: boolean

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 27–28why, one step back
27 assigned: {[key: string]: any}
28 parent?: ImmerState

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 29–31why, one step back
29 revoked: boolean
30}31

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 32–48why, one step back
32export 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 back
49 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}68

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 69–72why, one step back
69export 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 back
73 const isArray = Array.isArray(base)
74 const draft = clonePotentialDraft(base)75

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 76–78why, one step back
76 each(draft, prop => {
77 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))78 })

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 79why, one step back
79

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 80–82why, one step back
80 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 back
83 modified: false,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 84why, one step back
84 finalizing: false,

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 85why, one step back
85 finalized: false,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 86why, one step back
86 assigned: {},

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 87–90why, one step back
87 parent,
88 base,89 draft,90 copy: null,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 91–92why, one step back
91 revoked: false,
92 isManual: false

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 93–99why, one step back
93 }
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 back
100function peek(draft: Drafted, prop: PropertyKey) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 101–110why, one step back
101 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}110

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 111why, one step back
111function get(state: ES5State, prop: string | number) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 112–117why, one step back
112 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 back
118 // @ts-ignore
119 return (state.copy![prop] = state.scope.immer.createProxy(value, state))

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 120–123why, one step back
120 }
121 return value122}123

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 124why, one step back
124function set(state: ES5State, prop: string | number, value: any) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 125–128why, one step back
125 assertUnrevoked(state)
126 state.assigned[prop] = true127 if (!state.modified) {128 if (is(value, peek(latest(state), prop))) return

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 129why, one step back
129 markChangedES5(state)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 130–131why, one step back
130 prepareCopy(state)
131 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 132–133why, one step back
132 // @ts-ignore
133 state.copy![prop] = value

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 134–135why, one step back
134}
135

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 136why, one step back
136export function markChangedES5(state: ImmerState) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 137–138why, one step back
137 if (!state.modified) {
138 state.modified = true

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 139why, one step back
139 if (state.parent) markChangedES5(state.parent)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 140–142why, one step back
140 }
141}142

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 143why, one step back
143function prepareCopy(state: ES5State) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 144–146why, one step back
144 if (!state.copy) state.copy = clonePotentialDraft(state.base)
145}146

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 147–148why, one step back
147function clonePotentialDraft(base: Objectish) {
148 const state = base && (base as any)[DRAFT_STATE]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 149–159why, one step back
149 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 instead

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 160why, one step back
160const descriptors: {[prop: string]: PropertyDescriptor} = {}

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 161why, one step back
161

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 162–166why, one step back
162function 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 back
167 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 back
174 get(this: any) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 175–176why, one step back
175 return get(this[DRAFT_STATE], prop)
176 },

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 177why, one step back
177 set(this: any, value) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 178–184why, one step back
178 set(this[DRAFT_STATE], prop, value)
179 }180 }181 }182 Object.defineProperty(draft, prop, desc)183}184

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 185why, one step back
185export function assertUnrevoked(state: ES5State | MapState | SetState) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 186–193why, one step back
186 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 back
194function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 195–201why, one step back
195 // The natural order of drafts in the `scope` array is based on when they
196 // 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 back
202 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 break

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 209–213why, one step back
209 }
210 }211 }212}213

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 214why, one step back
214function markChangesRecursively(object: any) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 215–217why, one step back
215 if (!object || typeof object !== "object") return
216 const state = object[DRAFT_STATE]217 if (!state) return

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 218–219why, one step back
218 const {base, draft, assigned, type} = state
219 if (type === ProxyType.ES5Object) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 220why, one step back
220 // Look for added keys.

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 221–226why, one step back
221 // 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) return

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 227–229why, one step back
227 // The `undefined` check is a fast path for pre-existing keys.
228 if (base[key] === undefined && !has(base, key)) {229 assigned[key] = true

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 230why, one step back
230 markChangedES5(state)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 231–236why, one step back
231 } 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 back
237 each(base, key => {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 238–240why, one step back
238 // The `undefined` check is a fast path for pre-existing keys.
239 if (draft[key] === undefined && !has(draft, key)) {240 assigned[key] = false

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 241why, one step back
241 markChangedES5(state)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 242–243why, one step back
242 }
243 })

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 244–245why, one step back
244 } else if (type === ProxyType.ES5Array && hasArrayChanges(state)) {
245 markChangedES5(state)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 246–258why, one step back
246 assigned.length = true
247 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}258

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 259why, one step back
259function hasObjectChanges(state: ES5ObjectState) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 260–287why, one step back
260 const {base, draft} = state
261262 // 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}287

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 288why, one step back
288function hasArrayChanges(state: ES5ArrayState) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 289–303why, one step back
289 const {draft} = state
290 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}