Skip to content
Myr

es5.ts

This file

Path

src/plugins/es5.ts

277 lines · 8.3 KiB · TypeScript

Last changed by

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

History of this file

Take it

The walk stopped after 200 commits: lines older than 497d1a0 point at it.

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 1–13why, one step back
1import {
2 ImmerState,3 Drafted,4 ES5ArrayState,5 ES5ObjectState,6 each,7 has,8 isDraft,9 latest,10 DRAFT_STATE,11 is,12 loadPlugin,13 ImmerScope,

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 14why, one step back
14 ProxyType,

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 15why, one step back
15 getCurrentScope,

Immer 7Michel Weststratebefore adoption467ea5d

lines 16–17why, one step back
16 die,
17 markChanged,

fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf

line 18why, one step back
18 objectTraps,

fix: `getOwnPropertyDescriptors` is not available in Internet Explorer and Hermes. Fixes #626Michel Weststratebefore adoptionc7a47e2

lines 19–20why, one step back
19 ownKeys,
20 getOwnPropertyDescriptors

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 21–46why, one step back
21} from "../internal"
2223type ES5State = ES5ArrayState | ES5ObjectState2425export function enableES5() {26 function willFinalizeES5_(27 scope: ImmerScope,28 result: any,29 isReplaced: boolean30 ) {31 if (!isReplaced) {32 if (scope.patches_) {33 markChangesRecursively(scope.drafts_![0])34 }35 // This is faster when we don't care about which attributes changed.36 markChangesSweep(scope.drafts_)37 }38 // When a child draft is returned, look for changes.39 else if (40 isDraft(result) &&41 (result[DRAFT_STATE] as ES5State).scope_ === scope42 ) {43 markChangesSweep(scope.drafts_)44 }45 }46

Immer 7Michel Weststratebefore adoption467ea5d

lines 47–49why, one step back
47 function createES5Draft(isArray: boolean, base: any) {
48 if (isArray) {49 const draft = new Array(base.length)

fix: out of range assignments were broken in ES5 mode. Fixes #638Michel Weststratebefore adoption0fe9132

lines 50–51why, one step back
50 for (let i = 0; i < base.length; i++)
51 Object.defineProperty(draft, "" + i, proxyProperty(i, true))

Immer 7Michel Weststratebefore adoption467ea5d

lines 52–53why, one step back
52 return draft
53 } else {

fix: out of range assignments were broken in ES5 mode. Fixes #638Michel Weststratebefore adoption0fe9132

lines 54–63why, one step back
54 const descriptors = getOwnPropertyDescriptors(base)
55 delete descriptors[DRAFT_STATE as any]56 const keys = ownKeys(descriptors)57 for (let i = 0; i < keys.length; i++) {58 const key: any = keys[i]59 descriptors[key] = proxyProperty(60 key,61 isArray || !!descriptors[key].enumerable62 )63 }

Immer 7Michel Weststratebefore adoption467ea5d

lines 64–67why, one step back
64 return Object.create(Object.getPrototypeOf(base), descriptors)
65 }66 }67

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 68–72why, one step back
68 function createES5Proxy_<T>(
69 base: T,70 parent?: ImmerState71 ): Drafted<T, ES5ObjectState | ES5ArrayState> {72 const isArray = Array.isArray(base)

Immer 7Michel Weststratebefore adoption467ea5d

line 73why, one step back
73 const draft = createES5Draft(isArray, base)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 74–75why, one step back
74
75 const state: ES5ObjectState | ES5ArrayState = {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 76why, one step back
76 type_: isArray ? ProxyType.ES5Array : (ProxyType.ES5Object as any),

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 77–81why, one step back
77 scope_: parent ? parent.scope_ : getCurrentScope(),
78 modified_: false,79 finalized_: false,80 assigned_: {},81 parent_: parent,

Immer 7Michel Weststratebefore adoption467ea5d

line 82why, one step back
82 // base is the object we are drafting

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 83why, one step back
83 base_: base,

Immer 7Michel Weststratebefore adoption467ea5d

line 84why, one step back
84 // draft is the draft object itself, that traps all reads and reads from either the base (if unmodified) or copy (if modified)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 85–105why, one step back
85 draft_: draft,
86 copy_: null,87 revoked_: false,88 isManual_: false89 }9091 Object.defineProperty(draft, DRAFT_STATE, {92 value: state,93 // enumerable: false <- the default94 writable: true95 })96 return draft97 }9899 // property descriptors are recycled to make sure we don't create a get and set closure per property,100 // but share them all instead101 const descriptors: {[prop: string]: PropertyDescriptor} = {}102103 function proxyProperty(104 prop: string | number,105 enumerable: boolean

Immer 7Michel Weststratebefore adoption467ea5d

line 106why, one step back
106 ): PropertyDescriptor {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 107–111why, one step back
107 let desc = descriptors[prop]
108 if (desc) {109 desc.enumerable = enumerable110 } else {111 descriptors[prop] = desc = {

Immer 7Michel Weststratebefore adoption467ea5d

line 112why, one step back
112 configurable: true,

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 113–114why, one step back
113 enumerable,
114 get(this: any) {

Immer 7Michel Weststratebefore adoption467ea5d

lines 115–118why, one step back
115 const state = this[DRAFT_STATE]
116 if (__DEV__) assertUnrevoked(state)117 // @ts-ignore118 return objectTraps.get(state, prop)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 119–120why, one step back
119 },
120 set(this: any, value) {

Immer 7Michel Weststratebefore adoption467ea5d

lines 121–124why, one step back
121 const state = this[DRAFT_STATE]
122 if (__DEV__) assertUnrevoked(state)123 // @ts-ignore124 objectTraps.set(state, prop, value)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 125–127why, one step back
125 }
126 }127 }

Immer 7Michel Weststratebefore adoption467ea5d

line 128why, one step back
128 return desc

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 129–140why, one step back
129 }
130131 // This looks expensive, but only proxies are visited, and only objects without known changes are scanned.132 function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {133 // The natural order of drafts in the `scope` array is based on when they134 // were accessed. By processing drafts in reverse natural order, we have a135 // better chance of processing leaf nodes first. When a leaf node is known to136 // have changed, we can avoid any traversal of its ancestor nodes.137 for (let i = drafts.length - 1; i >= 0; i--) {138 const state: ES5State = drafts[i][DRAFT_STATE]139 if (!state.modified_) {140 switch (state.type_) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 141why, one step back
141 case ProxyType.ES5Array:

Immer 7Michel Weststratebefore adoption467ea5d

line 142why, one step back
142 if (hasArrayChanges(state)) markChanged(state)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 143why, one step back
143 break

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 144why, one step back
144 case ProxyType.ES5Object:

Immer 7Michel Weststratebefore adoption467ea5d

line 145why, one step back
145 if (hasObjectChanges(state)) markChanged(state)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 146–156why, one step back
146 break
147 }148 }149 }150 }151152 function markChangesRecursively(object: any) {153 if (!object || typeof object !== "object") return154 const state: ES5State | undefined = object[DRAFT_STATE]155 if (!state) return156 const {base_, draft_, assigned_, type_} = state

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 157why, one step back
157 if (type_ === ProxyType.ES5Object) {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 158–166why, one step back
158 // Look for added keys.
159 // probably there is a faster way to detect changes, as sweep + recurse seems to do some160 // unnecessary work.161 // also: probably we can store the information we detect here, to speed up tree finalization!162 each(draft_, key => {163 if ((key as any) === DRAFT_STATE) return164 // The `undefined` check is a fast path for pre-existing keys.165 if ((base_ as any)[key] === undefined && !has(base_, key)) {166 assigned_[key] = true

Immer 7Michel Weststratebefore adoption467ea5d

line 167why, one step back
167 markChanged(state)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 168–177why, one step back
168 } else if (!assigned_[key]) {
169 // Only untouched properties trigger recursion.170 markChangesRecursively(draft_[key])171 }172 })173 // Look for removed keys.174 each(base_, key => {175 // The `undefined` check is a fast path for pre-existing keys.176 if (draft_[key] === undefined && !has(draft_, key)) {177 assigned_[key] = false

Immer 7Michel Weststratebefore adoption467ea5d

line 178why, one step back
178 markChanged(state)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 179–180why, one step back
179 }
180 })

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 181why, one step back
181 } else if (type_ === ProxyType.ES5Array) {

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

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

Immer 7Michel Weststratebefore adoption467ea5d

line 183why, one step back
183 markChanged(state)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 184–197why, one step back
184 assigned_.length = true
185 }186187 if (draft_.length < base_.length) {188 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = false189 } else {190 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = true191 }192193 // Minimum count is enough, the other parts has been processed.194 const min = Math.min(draft_.length, base_.length)195196 for (let i = 0; i < min; i++) {197 // Only untouched indices trigger recursion.

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 198–200why, one step back
198 if (!draft_.hasOwnProperty(i)) {
199 assigned_[i] = true200 }

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 201–210why, one step back
201 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])
202 }203 }204 }205206 function hasObjectChanges(state: ES5ObjectState) {207 const {base_, draft_} = state208209 // Search for added keys and changed keys. Start at the back, because210 // non-numeric keys are ordered by time of definition on the object.

fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf

line 211why, one step back
211 const keys = ownKeys(draft_)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

line 212why, one step back
212 for (let i = keys.length - 1; i >= 0; i--) {

fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf

lines 213–214why, one step back
213 const key: any = keys[i]
214 if (key === DRAFT_STATE) continue

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 215–232why, one step back
215 const baseValue = base_[key]
216 // The `undefined` check is a fast path for pre-existing keys.217 if (baseValue === undefined && !has(base_, key)) {218 return true219 }220 // Once a base key is deleted, future changes go undetected, because its221 // descriptor is erased. This branch detects any missed changes.222 else {223 const value = draft_[key]224 const state: ImmerState = value && value[DRAFT_STATE]225 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {226 return true227 }228 }229 }230231 // At this point, no keys were added or changed.232 // Compare key count to determine if keys were deleted.

fix: Fixed #620: Symbolic properties were not drafted or finalized correctlyMichel Weststratebefore adoption91915cf

lines 233–234why, one step back
233 const baseIsDraft = !!base_[DRAFT_STATE as any]
234 return keys.length !== ownKeys(base_).length + (baseIsDraft ? 0 : 1) // + 1 to correct for DRAFT_STATE

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 235–246why, one step back
235 }
236237 function hasArrayChanges(state: ES5ArrayState) {238 const {draft_} = state239 if (draft_.length !== state.base_.length) return true240 // See #116241 // If we first shorten the length, our array interceptors will be removed.242 // If after that new items are added, result in the same original length,243 // those last items will have no intercepting property.244 // So if there is no own descriptor on the last position, we know that items were removed and added245 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check246 // the last one

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

line 247why, one step back
247 // last descriptor can be not a trap, if the array was extended

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 248–253why, one step back
248 const descriptor = Object.getOwnPropertyDescriptor(
249 draft_,250 draft_.length - 1251 )252 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)253 if (descriptor && !descriptor.get) return true

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 254–257why, one step back
254 // if we miss a property, it has been deleted, so array probobaly changed
255 for (let i = 0; i < draft_.length; i++) {256 if (!draft_.hasOwnProperty(i)) return true257 }

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 258–261why, one step back
258 // For all other cases, we don't have to compare, as they would have been picked up by the index setters
259 return false260 }261

Immer 7Michel Weststratebefore adoption467ea5d

line 262why, one step back
262 function hasChanges_(state: ES5State) {

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 263why, one step back
263 return state.type_ === ProxyType.ES5Object

Immer 7Michel Weststratebefore adoption467ea5d

lines 264–265why, one step back
264 ? hasObjectChanges(state)
265 : hasArrayChanges(state)

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 266–273why, one step back
266 }
267268 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {269 if (state.revoked_) die(3, JSON.stringify(latest(state)))270 }271272 loadPlugin("ES5", {273 createES5Proxy_,

Immer 7Michel Weststratebefore adoption467ea5d

lines 274–275why, one step back
274 willFinalizeES5_,
275 hasChanges_

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590Michel Weststratebefore adoption497d1a0

lines 276–277why, one step back
276 })
277}