Skip to content
Myr

es5.ts

This file

Path

src/plugins/es5.ts

300 lines · 8.6 KiB · TypeScript

Last changed by

scramble plugin names, -56Michel Weststratebefore adoption037b474

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–18why, one step back
7 each,
8 has,9 isDraft,10 isDraftable,11 isEnumerable,12 shallowCopy,13 latest,14 createHiddenProperty,15 DRAFT_STATE,16 assertUnrevoked,17 is,18 loadPlugin,

New build set. Current statsMichel Weststratebefore adoption9aec0fe

line 19why, one step back
19 ImmerScope,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 20–22why, one step back
20 createProxy,
21 ProxyTypeES5Array,22 ProxyTypeES5Object

More WIPMichel Weststratebefore adoptionee1c977

lines 23–26why, one step back
23} from "../internal"
2425type ES5State = ES5ArrayState | ES5ObjectState26

TSDXMichel Weststratebefore adoption4c4fecc

line 27why, one step back
27export function enableES5() {

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 28why, one step back
28 function willFinalizeES5_(

TSDXMichel Weststratebefore adoption4c4fecc

lines 29–32why, one step back
29 scope: ImmerScope,
30 result: any,31 isReplaced: boolean32 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 33–34why, one step back
33 scope.drafts_!.forEach((draft: any) => {
34 ;(draft[DRAFT_STATE] as ES5State).finalizing_ = true

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 39why, one step back
39 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Small improvementsMichel Weststratebefore adoption712af5c

line 41why, one step back
41 // TODO: should be prefixed with else!

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 43why, one step back
43 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 50–51why, one step back
50 }
51 }

More WIPMichel Weststratebefore adoptionee1c977

line 52why, one step back
52

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 53why, one step back
53 function createES5Proxy_<T>(

TSDXMichel Weststratebefore adoption4c4fecc

lines 54–57why, one step back
54 base: T,
55 parent?: ImmerState56 ): Drafted<T, ES5ObjectState | ES5ArrayState> {57 const isArray = Array.isArray(base)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 59why, one step back
59

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 63why, one step back
63

TSDXMichel Weststratebefore adoption4c4fecc

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 65why, one step back
65 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 66–76why, one step back
66 scope_: parent ? parent.scope_ : ImmerScope.current_!,
67 modified_: false,68 finalizing_: false,69 finalized_: false,70 assigned_: {},71 parent_: parent,72 base_: base,73 draft_: draft,74 copy_: null,75 revoked_: false,76 isManual_: false

More WIPMichel Weststratebefore adoptionee1c977

lines 77–78why, one step back
77 }
78

TSDXMichel Weststratebefore adoption4c4fecc

lines 79–81why, one step back
79 createHiddenProperty(draft, DRAFT_STATE, state)
80 return draft81 }

More WIPMichel Weststratebefore adoptionee1c977

line 82why, one step back
82

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 90–91why, one step back
90 return value
91 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 92–93why, one step back
92 return draft[prop]
93 }

More WIPMichel Weststratebefore adoptionee1c977

line 94why, one step back
94

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 108why, one step back
108 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 109–110why, one step back
109 return value
110 }

More WIPMichel Weststratebefore adoptionee1c977

line 111why, one step back
111

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 116why, one step back
116 if (is(value, peek(latest(state), prop))) return

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 117why, one step back
117 markChangedES5_(state)

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 119why, one step back
119 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 122why, one step back
122 }

More WIPMichel Weststratebefore adoptionee1c977

line 123why, one step back
123

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 124why, one step back
124 function markChangedES5_(state: ImmerState) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 125–126why, one step back
125 if (!state.modified_) {
126 state.modified_ = true

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 127why, one step back
127 if (state.parent_) markChangedES5_(state.parent_)

More WIPMichel Weststratebefore adoptionee1c977

line 128why, one step back
128 }

TSDXMichel Weststratebefore adoption4c4fecc

line 129why, one step back
129 }

More WIPMichel Weststratebefore adoptionee1c977

line 130why, one step back
130

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 141why, one step back
141 return draft

More WIPMichel Weststratebefore adoptionee1c977

line 142why, one step back
142 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 143–144why, one step back
143 return shallowCopy(base)
144 }

More WIPMichel Weststratebefore adoptionee1c977

line 145why, one step back
145

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 149why, one step back
149

TSDXMichel Weststratebefore adoption4c4fecc

lines 150–159why, one step back
150 function proxyProperty(
151 draft: Drafted<any, ES5State>,152 prop: string | number,153 enumerable: boolean154 ) {155 let desc = descriptors[prop]156 if (desc) {157 desc.enumerable = enumerable158 } else {159 descriptors[prop] = desc = {

Some notesMichel Weststratebefore adoption746dc6f

line 160why, one step back
160 // configurable: true,

TSDXMichel Weststratebefore adoption4c4fecc

lines 161–166why, one step back
161 enumerable,
162 get(this: any) {163 return get(this[DRAFT_STATE], prop)164 },165 set(this: any, value) {166 set(this[DRAFT_STATE], prop, value)

More WIPMichel Weststratebefore adoptionee1c977

lines 167–169why, one step back
167 }
168 }169 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 172why, one step back
172

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 183why, one step back
183 case ProxyTypeES5Array:

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 184why, one step back
184 if (hasArrayChanges(state)) markChangedES5_(state)

TSDXMichel Weststratebefore adoption4c4fecc

line 185why, one step back
185 break

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 186why, one step back
186 case ProxyTypeES5Object:

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 187why, one step back
187 if (hasObjectChanges(state)) markChangedES5_(state)

TSDXMichel Weststratebefore adoption4c4fecc

line 188why, one step back
188 break

More WIPMichel Weststratebefore adoptionee1c977

lines 189–191why, one step back
189 }
190 }191 }

TSDXMichel Weststratebefore adoption4c4fecc

line 192why, one step back
192 }

More WIPMichel Weststratebefore adoptionee1c977

line 193why, one step back
193

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 198why, one step back
198 const {base_, draft_, assigned_, type_} = state

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 199why, one step back
199 if (type_ === ProxyTypeES5Object) {

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 210why, one step back
210 markChangedES5_(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 214why, one step back
214 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 215–216why, one step back
215 })
216 // Look for removed keys.

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 221why, one step back
221 markChangedES5_(state)

More WIPMichel Weststratebefore adoptionee1c977

line 222why, one step back
222 }

TSDXMichel Weststratebefore adoption4c4fecc

line 223why, one step back
223 })

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 224why, one step back
224 } else if (type_ === ProxyTypeES5Array) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 225why, one step back
225 if (hasArrayChanges(state as ES5ArrayState)) {

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 226why, one step back
226 markChangedES5_(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 228why, one step back
228 }

More WIPMichel Weststratebefore adoptionee1c977

line 229why, one step back
229

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 232why, one step back
232 } else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 234why, one step back
234 }

More WIPMichel Weststratebefore adoptionee1c977

line 235why, one step back
235

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

line 244why, one step back
244 }

More WIPMichel Weststratebefore adoptionee1c977

line 245why, one step back
245

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 248why, one step back
248

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

lines 265–269why, one step back
265 return true
266 }267 }268 }269

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 273why, one step back
273 }

More WIPMichel Weststratebefore adoptionee1c977

line 274why, one step back
274

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 285–288why, one step back
285 const descriptor = Object.getOwnPropertyDescriptor(
286 draft_,287 draft_.length - 1288 )

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 293why, one step back
293 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 294–295why, one step back
294
295 loadPlugin("es5", {

scramble plugin names, -56Michel Weststratebefore adoption037b474

lines 296–298why, one step back
296 createES5Proxy_,
297 markChangedES5_,298 willFinalizeES5_

TSDXMichel Weststratebefore adoption4c4fecc

lines 299–300why, one step back
299 })
300}