Skip to content
Myr

es5.ts

This file

Path

src/plugins/es5.ts

299 lines · 8.6 KiB · TypeScript

Last changed by

Small improvementsMichel Weststratebefore adoption712af5c

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.

Small improvementsMichel Weststratebefore adoption712af5c

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 42why, one step back
42 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 51why, one step back
51

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 58why, one step back
58

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 62why, one step back
62

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 81why, one step back
81

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 93why, one step back
93

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 107why, one step back
107 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 110why, one step back
110

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 118why, one step back
118 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 121why, one step back
121 }

More WIPMichel Weststratebefore adoptionee1c977

line 122why, one step back
122

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 127why, one step back
127 }

TSDXMichel Weststratebefore adoption4c4fecc

line 128why, one step back
128 }

More WIPMichel Weststratebefore adoptionee1c977

line 129why, one step back
129

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 132–134why, one step back
132 }
133134 function clonePotentialDraft(base: Objectish) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 140why, one step back
140 return draft

More WIPMichel Weststratebefore adoptionee1c977

line 141why, one step back
141 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 144why, one step back
144

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 148why, one step back
148

TSDXMichel Weststratebefore adoption4c4fecc

lines 149–158why, one step back
149 function proxyProperty(
150 draft: Drafted<any, ES5State>,151 prop: string | number,152 enumerable: boolean153 ) {154 let desc = descriptors[prop]155 if (desc) {156 desc.enumerable = enumerable157 } else {158 descriptors[prop] = desc = {

Some notesMichel Weststratebefore adoption746dc6f

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 171why, one step back
171

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

line 191why, one step back
191 }

More WIPMichel Weststratebefore adoptionee1c977

line 192why, one step back
192

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 213why, one step back
213 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 221why, one step back
221 }

TSDXMichel Weststratebefore adoption4c4fecc

line 222why, one step back
222 })

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 227why, one step back
227 }

More WIPMichel Weststratebefore adoptionee1c977

line 228why, one step back
228

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 231why, one step back
231 } else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 233why, one step back
233 }

More WIPMichel Weststratebefore adoptionee1c977

line 234why, one step back
234

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

line 243why, one step back
243 }

More WIPMichel Weststratebefore adoptionee1c977

line 244why, one step back
244

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 247why, one step back
247

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 272why, one step back
272 }

More WIPMichel Weststratebefore adoptionee1c977

line 273why, one step back
273

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 292why, one step back
292 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 293–299why, one step back
293
294 loadPlugin("es5", {295 createES5Proxy,296 markChangedES5,297 willFinalizeES5298 })299}