Skip to content
Myr

es5.ts

This file

Path

src/plugins/es5.ts

266 lines · 7.9 KiB · TypeScript

Last changed by

Some dotsMichel Weststratebefore adoption4e438d0

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–16why, 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,14 ProxyTypeES5Array,15 ProxyTypeES5Object,16 getCurrentScope,

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

lines 17–19why, one step back
17 die,
18 markChanged,19 objectTraps

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 46–48why, one step back
46 function createES5Draft(isArray: boolean, base: any) {
47 // Create a new object / array, where each own property is trapped with an accessor48 const descriptors = Object.getOwnPropertyDescriptors(base)

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

line 49why, one step back
49 // Descriptors we want to skip:

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 50–53why, one step back
50 if (isArray) delete descriptors.length
51 delete descriptors[DRAFT_STATE as any]52 for (let key in descriptors) {53 descriptors[key] = proxyProperty(

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

line 54why, one step back
54 key,

finished accessor props for ES5Michel Weststratebefore adoption1466db3

line 55why, one step back
55 isArray || !!descriptors[key].enumerable

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

lines 56–58why, one step back
56 )
57 }58 if (isArray) {

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 59–61why, one step back
59 const draft = new Array(base.length)
60 Object.defineProperties(draft, descriptors)61 return draft

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

line 62why, one step back
62 } else {

finished accessor props for ES5Michel Weststratebefore adoption1466db3

line 63why, one step back
63 return Object.create(Object.getPrototypeOf(base), descriptors)

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

line 64why, one step back
64 }

finished accessor props for ES5Michel Weststratebefore adoption1466db3

line 65why, one step back
65 }

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 66why, one step back
66

finished accessor props for ES5Michel Weststratebefore adoption1466db3

lines 67–73why, one step back
67 function createES5Proxy_<T>(
68 base: T,69 parent?: ImmerState70 ): Drafted<T, ES5ObjectState | ES5ArrayState> {71 const isArray = Array.isArray(base)72 const draft = createES5Draft(isArray, base)73

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 74–80why, one step back
74 const state: ES5ObjectState | ES5ArrayState = {
75 type_: isArray ? ProxyTypeES5Array : (ProxyTypeES5Object as any),76 scope_: parent ? parent.scope_ : getCurrentScope(),77 modified_: false,78 finalized_: false,79 assigned_: {},80 parent_: parent,

finished accessor props for ES5Michel Weststratebefore adoption1466db3

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

finished accessor props for ES5Michel Weststratebefore adoption1466db3

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 84–104why, one step back
84 draft_: draft,
85 copy_: null,86 revoked_: false,87 isManual_: false88 }8990 Object.defineProperty(draft, DRAFT_STATE, {91 value: state,92 // enumerable: false <- the default93 writable: true94 })95 return draft96 }9798 // property descriptors are recycled to make sure we don't create a get and set closure per property,99 // but share them all instead100 const descriptors: {[prop: string]: PropertyDescriptor} = {}101102 function proxyProperty(103 prop: string | number,104 enumerable: boolean

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Reimplement ES5 to work with descriptors rather than valuesMichel Weststratebefore adoptiond0f3a71

line 127why, one step back
127 return desc

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 142–143why, one step back
142 break
143 case ProxyTypeES5Object:

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 145–165why, one step back
145 break
146 }147 }148 }149 }150151 function markChangesRecursively(object: any) {152 if (!object || typeof object !== "object") return153 const state: ES5State | undefined = object[DRAFT_STATE]154 if (!state) return155 const {base_, draft_, assigned_, type_} = state156 if (type_ === ProxyTypeES5Object) {157 // Look for added keys.158 // probably there is a faster way to detect changes, as sweep + recurse seems to do some159 // unnecessary work.160 // also: probably we can store the information we detect here, to speed up tree finalization!161 each(draft_, key => {162 if ((key as any) === DRAFT_STATE) return163 // The `undefined` check is a fast path for pre-existing keys.164 if ((base_ as any)[key] === undefined && !has(base_, key)) {165 assigned_[key] = true

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

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

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 178–181why, one step back
178 }
179 })180 } else if (type_ === ProxyTypeES5Array) {181 if (hasArrayChanges(state as ES5ArrayState)) {

Code cleanup, bug fixesMichel Weststratebefore adoption0592d3c

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

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 183–250why, one step back
183 assigned_.length = true
184 }185186 if (draft_.length < base_.length) {187 for (let i = draft_.length; i < base_.length; i++) assigned_[i] = false188 } else {189 for (let i = base_.length; i < draft_.length; i++) assigned_[i] = true190 }191192 // Minimum count is enough, the other parts has been processed.193 const min = Math.min(draft_.length, base_.length)194195 for (let i = 0; i < min; i++) {196 // Only untouched indices trigger recursion.197 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])198 }199 }200 }201202 function hasObjectChanges(state: ES5ObjectState) {203 const {base_, draft_} = state204205 // Search for added keys and changed keys. Start at the back, because206 // non-numeric keys are ordered by time of definition on the object.207 const keys = Object.keys(draft_)208 for (let i = keys.length - 1; i >= 0; i--) {209 const key = keys[i]210 const baseValue = base_[key]211 // The `undefined` check is a fast path for pre-existing keys.212 if (baseValue === undefined && !has(base_, key)) {213 return true214 }215 // Once a base key is deleted, future changes go undetected, because its216 // descriptor is erased. This branch detects any missed changes.217 else {218 const value = draft_[key]219 const state: ImmerState = value && value[DRAFT_STATE]220 if (state ? state.base_ !== baseValue : !is(value, baseValue)) {221 return true222 }223 }224 }225226 // At this point, no keys were added or changed.227 // Compare key count to determine if keys were deleted.228 return keys.length !== Object.keys(base_).length229 }230231 function hasArrayChanges(state: ES5ArrayState) {232 const {draft_} = state233 if (draft_.length !== state.base_.length) return true234 // See #116235 // If we first shorten the length, our array interceptors will be removed.236 // If after that new items are added, result in the same original length,237 // those last items will have no intercepting property.238 // So if there is no own descriptor on the last position, we know that items were removed and added239 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check240 // the last one241 const descriptor = Object.getOwnPropertyDescriptor(242 draft_,243 draft_.length - 1244 )245 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)246 if (descriptor && !descriptor.get) return true247 // For all other cases, we don't have to compare, as they would have been picked up by the index setters248 return false249 }250

Made ES5 version workMichel Weststratebefore adoption870f44e

lines 251–256why, one step back
251 function hasChanges_(state: ES5State) {
252 return state.type_ === ProxyTypeES5Object253 ? hasObjectChanges(state)254 : hasArrayChanges(state)255 }256

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 257–262why, one step back
257 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {
258 if (state.revoked_) die(3, JSON.stringify(latest(state)))259 }260261 loadPlugin("ES5", {262 createES5Proxy_,

Made ES5 version workMichel Weststratebefore adoption870f44e

lines 263–264why, one step back
263 willFinalizeES5_,
264 hasChanges_

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 265–266why, one step back
265 })
266}