Skip to content
Myr

es5.ts

This file

Path

src/plugins/es5.ts

298 lines · 8.5 KiB · TypeScript

Last changed by

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

History of this file

Take it

More WIPMichel Weststratebefore adoptionee1c977

lines 1–6why, one step back
1import {
2 ImmerState,3 Drafted,4 Objectish,5 ES5ArrayState,6 ES5ObjectState,

TSDXMichel Weststratebefore adoption4c4fecc

lines 7–19why, one step back
7 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 back
20 ImmerScope,
21 createProxy

More WIPMichel Weststratebefore adoptionee1c977

lines 22–25why, one step back
22} from "../internal"
2324type ES5State = ES5ArrayState | ES5ObjectState25

TSDXMichel Weststratebefore adoption4c4fecc

lines 26–31why, one step back
26export 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 back
32 scope.drafts_!.forEach((draft: any) => {
33 ;(draft[DRAFT_STATE] as ES5State).finalizing_ = true

TSDXMichel Weststratebefore adoption4c4fecc

lines 34–35why, one step back
34 })
35 if (!isReplaced) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 36–37why, one step back
36 if (scope.patches_) {
37 markChangesRecursively(scope.drafts_![0])

More WIPMichel Weststratebefore adoptionee1c977

line 38why, one step back
38 }

TSDXMichel Weststratebefore adoption4c4fecc

line 39why, one step back
39 // This is faster when we don't care about which attributes changed.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 40why, one step back
40 markChangesSweep(scope.drafts_)

More WIPMichel Weststratebefore adoptionee1c977

line 41why, one step back
41 }

TSDXMichel Weststratebefore adoption4c4fecc

line 42why, one step back
42 // When a child draft is returned, look for changes.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 43–47why, one step back
43 else if (
44 isDraft(result) &&45 (result[DRAFT_STATE] as ES5State).scope_ === scope46 ) {47 markChangesSweep(scope.drafts_)

TSDXMichel Weststratebefore adoption4c4fecc

lines 48–49why, one step back
48 }
49 }

More WIPMichel Weststratebefore adoptionee1c977

line 50why, one step back
50

TSDXMichel Weststratebefore adoption4c4fecc

lines 51–55why, one step back
51 function createES5Proxy<T>(
52 base: T,53 parent?: ImmerState54 ): Drafted<T, ES5ObjectState | ES5ArrayState> {55 const isArray = Array.isArray(base)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 56why, one step back
56 const draft: any = clonePotentialDraft(base)

More WIPMichel Weststratebefore adoptionee1c977

line 57why, one step back
57

TSDXMichel Weststratebefore adoption4c4fecc

lines 58–60why, one step back
58 each(draft, prop => {
59 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))60 })

More WIPMichel Weststratebefore adoptionee1c977

line 61why, one step back
61

TSDXMichel Weststratebefore adoption4c4fecc

line 62why, one step back
62 const state: ES5ObjectState | ES5ArrayState = {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 63–74why, one step back
63 type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),
64 scope_: parent ? parent.scope_ : ImmerScope.current_!,65 modified_: false,66 finalizing_: false,67 finalized_: false,68 assigned_: {},69 parent_: parent,70 base_: base,71 draft_: draft,72 copy_: null,73 revoked_: false,74 isManual_: false

More WIPMichel Weststratebefore adoptionee1c977

lines 75–76why, one step back
75 }
76

TSDXMichel Weststratebefore adoption4c4fecc

lines 77–79why, one step back
77 createHiddenProperty(draft, DRAFT_STATE, state)
78 return draft79 }

More WIPMichel Weststratebefore adoptionee1c977

line 80why, one step back
80

TSDXMichel Weststratebefore adoption4c4fecc

lines 81–82why, one step back
81 // Access a property without creating an Immer draft.
82 function peek(draft: Drafted, prop: PropertyKey) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 83–85why, one step back
83 const state: ES5State = draft[DRAFT_STATE]
84 if (state && !state.finalizing_) {85 state.finalizing_ = true

TSDXMichel Weststratebefore adoption4c4fecc

line 86why, one step back
86 const value = draft[prop]

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 87why, one step back
87 state.finalizing_ = false

More WIPMichel Weststratebefore adoptionee1c977

lines 88–89why, one step back
88 return value
89 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 90–91why, one step back
90 return draft[prop]
91 }

More WIPMichel Weststratebefore adoptionee1c977

line 92why, one step back
92

TSDXMichel Weststratebefore adoption4c4fecc

lines 93–95why, one step back
93 function get(state: ES5State, prop: string | number) {
94 assertUnrevoked(state)95 const value = peek(latest(state), prop)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 96why, one step back
96 if (state.finalizing_) return value

TSDXMichel Weststratebefore adoption4c4fecc

line 97why, one step back
97 // Create a draft if the value is unmodified.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 98why, one step back
98 if (value === peek(state.base_, prop) && isDraftable(value)) {

TSDXMichel Weststratebefore adoption4c4fecc

line 99why, one step back
99 prepareCopy(state)

More WIPMichel Weststratebefore adoptionee1c977

line 100why, one step back
100 // @ts-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 101–105why, one step back
101 return (state.copy_![prop] = createProxy(
102 state.scope_.immer_,103 value,104 state105 ))

More WIPMichel Weststratebefore adoptionee1c977

line 106why, one step back
106 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 107–108why, one step back
107 return value
108 }

More WIPMichel Weststratebefore adoptionee1c977

line 109why, one step back
109

TSDXMichel Weststratebefore adoption4c4fecc

lines 110–111why, one step back
110 function set(state: ES5State, prop: string | number, value: any) {
111 assertUnrevoked(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 112–113why, one step back
112 state.assigned_[prop] = true
113 if (!state.modified_) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 114–116why, one step back
114 if (is(value, peek(latest(state), prop))) return
115 markChangedES5(state)116 prepareCopy(state)

More WIPMichel Weststratebefore adoptionee1c977

line 117why, one step back
117 }

TSDXMichel Weststratebefore adoption4c4fecc

line 118why, one step back
118 // @ts-ignore

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 119why, one step back
119 state.copy_![prop] = value

TSDXMichel Weststratebefore adoption4c4fecc

line 120why, one step back
120 }

More WIPMichel Weststratebefore adoptionee1c977

line 121why, one step back
121

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 123–125why, one step back
123 if (!state.modified_) {
124 state.modified_ = true125 if (state.parent_) markChangedES5(state.parent_)

More WIPMichel Weststratebefore adoptionee1c977

line 126why, one step back
126 }

TSDXMichel Weststratebefore adoption4c4fecc

line 127why, one step back
127 }

More WIPMichel Weststratebefore adoptionee1c977

line 128why, one step back
128

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 130why, one step back
130 if (!state.copy_) state.copy_ = clonePotentialDraft(state.base_)

TSDXMichel Weststratebefore adoption4c4fecc

lines 131–133why, one step back
131 }
132133 function clonePotentialDraft(base: Objectish) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 134why, one step back
134 const state: ES5State | undefined = base && (base as any)[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

line 135why, one step back
135 if (state) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 136–138why, one step back
136 state.finalizing_ = true
137 const draft = shallowCopy(state.draft_, true)138 state.finalizing_ = false

TSDXMichel Weststratebefore adoption4c4fecc

line 139why, one step back
139 return draft

More WIPMichel Weststratebefore adoptionee1c977

line 140why, one step back
140 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 141–142why, one step back
141 return shallowCopy(base)
142 }

More WIPMichel Weststratebefore adoptionee1c977

line 143why, one step back
143

TSDXMichel Weststratebefore adoption4c4fecc

lines 144–146why, one step back
144 // property descriptors are recycled to make sure we don't create a get and set closure per property,
145 // but share them all instead146 const descriptors: {[prop: string]: PropertyDescriptor} = {}

More WIPMichel Weststratebefore adoptionee1c977

line 147why, one step back
147

TSDXMichel Weststratebefore adoption4c4fecc

lines 148–164why, one step back
148 function proxyProperty(
149 draft: Drafted<any, ES5State>,150 prop: string | number,151 enumerable: boolean152 ) {153 let desc = descriptors[prop]154 if (desc) {155 desc.enumerable = enumerable156 } else {157 descriptors[prop] = desc = {158 configurable: true,159 enumerable,160 get(this: any) {161 return get(this[DRAFT_STATE], prop)162 },163 set(this: any, value) {164 set(this[DRAFT_STATE], prop, value)

More WIPMichel Weststratebefore adoptionee1c977

lines 165–167why, one step back
165 }
166 }167 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 168–169why, one step back
168 Object.defineProperty(draft, prop, desc)
169 }

More WIPMichel Weststratebefore adoptionee1c977

line 170why, one step back
170

TSDXMichel Weststratebefore adoption4c4fecc

lines 171–177why, one step back
171 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
172 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {173 // The natural order of drafts in the `scope` array is based on when they174 // were accessed. By processing drafts in reverse natural order, we have a175 // better chance of processing leaf nodes first. When a leaf node is known to176 // have changed, we can avoid any traversal of its ancestor nodes.177 for (let i = drafts.length - 1; i >= 0; i--) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 178–180why, one step back
178 const state: ES5State = drafts[i][DRAFT_STATE]
179 if (!state.modified_) {180 switch (state.type_) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 181–186why, one step back
181 case ProxyType.ES5Array:
182 if (hasArrayChanges(state)) markChangedES5(state)183 break184 case ProxyType.ES5Object:185 if (hasObjectChanges(state)) markChangedES5(state)186 break

More WIPMichel Weststratebefore adoptionee1c977

lines 187–189why, one step back
187 }
188 }189 }

TSDXMichel Weststratebefore adoption4c4fecc

line 190why, one step back
190 }

More WIPMichel Weststratebefore adoptionee1c977

line 191why, one step back
191

TSDXMichel Weststratebefore adoption4c4fecc

lines 192–193why, one step back
192 function markChangesRecursively(object: any) {
193 if (!object || typeof object !== "object") return

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 194why, one step back
194 const state: ES5State | undefined = object[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

line 195why, one step back
195 if (!state) return

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 196–197why, one step back
196 const {base_, draft_, assigned_, type_} = state
197 if (type_ === ProxyType.ES5Object) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 198–202why, one step back
198 // Look for added keys.
199 // TODO: looks quite duplicate to hasObjectChanges,200 // probably there is a faster way to detect changes, as sweep + recurse seems to do some201 // unnecessary work.202 // also: probably we can store the information we detect here, to speed up tree finalization!

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 203why, one step back
203 each(draft_, key => {

TSDXMichel Weststratebefore adoption4c4fecc

lines 204–205why, one step back
204 if ((key as any) === DRAFT_STATE) return
205 // The `undefined` check is a fast path for pre-existing keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 206–207why, one step back
206 if ((base_ as any)[key] === undefined && !has(base_, key)) {
207 assigned_[key] = true

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 209why, one step back
209 } else if (!assigned_[key]) {

TSDXMichel Weststratebefore adoption4c4fecc

line 210why, one step back
210 // Only untouched properties trigger recursion.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 211why, one step back
211 markChangesRecursively(draft_[key])

More WIPMichel Weststratebefore adoptionee1c977

line 212why, one step back
212 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 213–214why, one step back
213 })
214 // Look for removed keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 215why, one step back
215 each(base_, key => {

TSDXMichel Weststratebefore adoption4c4fecc

line 216why, one step back
216 // The `undefined` check is a fast path for pre-existing keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 217–218why, one step back
217 if (draft_[key] === undefined && !has(draft_, key)) {
218 assigned_[key] = false

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 220why, one step back
220 }

TSDXMichel Weststratebefore adoption4c4fecc

line 221why, one step back
221 })

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 222–223why, one step back
222 } else if (type_ === ProxyType.ES5Array) {
223 if (hasArrayChanges(state as ES5ArrayState)) {

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 225why, one step back
225 assigned_.length = true

TSDXMichel Weststratebefore adoption4c4fecc

line 226why, one step back
226 }

More WIPMichel Weststratebefore adoptionee1c977

line 227why, one step back
227

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 228–229why, one step back
228 if (draft_.length < base_.length) {
229 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = false

TSDXMichel Weststratebefore adoption4c4fecc

line 230why, one step back
230 } else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 231why, one step back
231 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = true

TSDXMichel Weststratebefore adoption4c4fecc

line 232why, one step back
232 }

More WIPMichel Weststratebefore adoptionee1c977

line 233why, one step back
233

TSDXMichel Weststratebefore adoption4c4fecc

line 234why, one step back
234 // Minimum count is enough, the other parts has been processed.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 235why, one step back
235 const min = Math.min(draft_.length, base_.length)

TSDXMichel Weststratebefore adoption4c4fecc

lines 236–238why, one step back
236
237 for (let i = 0; i < min; i++) {238 // Only untouched indices trigger recursion.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 239why, one step back
239 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])

More WIPMichel Weststratebefore adoptionee1c977

lines 240–241why, one step back
240 }
241 }

TSDXMichel Weststratebefore adoption4c4fecc

line 242why, one step back
242 }

More WIPMichel Weststratebefore adoptionee1c977

line 243why, one step back
243

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 245why, one step back
245 const {base_, draft_} = state

More WIPMichel Weststratebefore adoptionee1c977

line 246why, one step back
246

TSDXMichel Weststratebefore adoption4c4fecc

lines 247–248why, one step back
247 // Search for added keys and changed keys. Start at the back, because
248 // non-numeric keys are ordered by time of definition on the object.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 249why, one step back
249 const keys = Object.keys(draft_)

TSDXMichel Weststratebefore adoption4c4fecc

lines 250–251why, one step back
250 for (let i = keys.length - 1; i >= 0; i--) {
251 const key = keys[i]

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 252why, one step back
252 const baseValue = base_[key]

TSDXMichel Weststratebefore adoption4c4fecc

line 253why, one step back
253 // The `undefined` check is a fast path for pre-existing keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 254why, one step back
254 if (baseValue === undefined && !has(base_, key)) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 255–259why, one step back
255 return true
256 }257 // Once a base key is deleted, future changes go undetected, because its258 // descriptor is erased. This branch detects any missed changes.259 else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 260–262why, one step back
260 const value = draft_[key]
261 const state: ImmerState = value && value[DRAFT_STATE]262 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {

More WIPMichel Weststratebefore adoptionee1c977

lines 263–267why, one step back
263 return true
264 }265 }266 }267

TSDXMichel Weststratebefore adoption4c4fecc

lines 268–269why, one step back
268 // At this point, no keys were added or changed.
269 // Compare key count to determine if keys were deleted.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 270why, one step back
270 return keys.length !== Object.keys(base_).length

TSDXMichel Weststratebefore adoption4c4fecc

line 271why, one step back
271 }

More WIPMichel Weststratebefore adoptionee1c977

line 272why, one step back
272

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 274–275why, one step back
274 const {draft_} = state
275 if (draft_.length !== state.base_.length) return true

TSDXMichel Weststratebefore adoption4c4fecc

lines 276–282why, one step back
276 // See #116
277 // If we first shorten the length, our array interceptors will be removed.278 // If after that new items are added, result in the same original length,279 // those last items will have no intercepting property.280 // So if there is no own descriptor on the last position, we know that items were removed and added281 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check282 // the last one

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 283–286why, one step back
283 const descriptor = Object.getOwnPropertyDescriptor(
284 draft_,285 draft_.length - 1286 )

TSDXMichel Weststratebefore adoption4c4fecc

lines 287–290why, one step back
287 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)
288 if (descriptor && !descriptor.get) return true289 // For all other cases, we don't have to compare, as they would have been picked up by the index setters290 return false

More WIPMichel Weststratebefore adoptionee1c977

line 291why, one step back
291 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 292–298why, one step back
292
293 loadPlugin("es5", {294 createES5Proxy,295 markChangedES5,296 willFinalizeES5297 })298}