Skip to content
Myr

es5.ts

This file

Path

src/es5.ts

367 lines · 10.0 KiB · TypeScript

Last changed by

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

History of this file

Take it

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 1–13why, one step back
1"use strict"
2import {3 each,4 has,5 is,6 isDraft,7 isDraftable,8 isEnumerable,9 isMap,10 isSet,11 hasSymbol,12 shallowCopy,13 DRAFT_STATE,

map unification WIPMichel Weststratebefore adoption9bcefb6

line 14why, one step back
14 latest

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 15why, one step back
15} from "./common"

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 16–18why, one step back
16import {proxyMap, hasMapChanges} from "./map"
17import {proxySet, hasSetChanges} from "./set"18

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 19why, one step back
19import {ImmerScope} from "./scope"

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 20why, one step back
20import {ImmerState, Drafted, AnyObject, AnyMap} from "./types"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 21–22why, one step back
21
22// TODO: merge with ImmerState?

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 23why, one step back
23interface ES5BaseState {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 24–27why, one step back
24 scope: ImmerScope
25 modified: boolean26 finalizing: boolean27 finalized: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 28–30why, one step back
28 assigned: {[key: string]: any}
29 parent: ImmerState30 drafts: AnyMap | null // TODO: don't use map?

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 31–34why, one step back
31 revoke()
32 revoked: boolean33}34

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 35–50why, one step back
35export interface ES5ObjectState extends ES5BaseState {
36 type: "es5_object"37 draft: Drafted<AnyObject, ES5ObjectState>38 base: AnyObject39 copy: AnyObject | null40}4142export interface ES5ArrayState extends ES5BaseState {43 type: "es5_array"44 draft: Drafted<AnyObject, ES5ArrayState>45 base: AnyObject46 copy: AnyObject | null47}4849type ES5State = ES5ArrayState | ES5ObjectState50

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

line 51why, one step back
51export function willFinalizeES5(

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 52–71why, one step back
52 scope: ImmerScope,
53 result: any,54 isReplaced: boolean55) {56 scope.drafts!.forEach(draft => {57 draft[DRAFT_STATE].finalizing = true58 })59 if (!isReplaced) {60 if (scope.patches) {61 markChangesRecursively(scope.drafts![0])62 }63 // This is faster when we don't care about which attributes changed.64 markChangesSweep(scope.drafts)65 }66 // When a child draft is returned, look for changes.67 else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {68 markChangesSweep(scope.drafts)69 }70}71

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

line 72why, one step back
72export function createES5Proxy<T>(

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 73–75why, one step back
73 base: T,
74 parent: ImmerState75): Drafted<any, ES5ObjectState | ES5ArrayState> {

fixed most testsMichel Weststratebefore adoption385c0a4

lines 76–79why, one step back
76 if (!base || typeof base !== "object" || !isDraftable(base)) {
77 // TODO: || isDraft ?78 return base as any // TODO: fix79 }

basic map tests succeedingMichel Weststratebefore adoption735c402

lines 80–85why, one step back
80 const scope = parent ? parent.scope : ImmerScope.current!
81 if (isMap(base)) {82 const draft = proxyMap(base, parent) as any // TODO: typefix83 scope.drafts.push(draft)84 return draft85 }

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 86–90why, one step back
86 if (isSet(base)) {
87 const draft = proxySet(base, parent) as any // TODO: typefix88 scope.drafts.push(draft)89 return draft90 }

basic map tests succeedingMichel Weststratebefore adoption735c402

line 91why, one step back
91

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 92–94why, one step back
92 const isArray = Array.isArray(base)
93 const draft = clonePotentialDraft(base)94

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 95–97why, one step back
95 each(draft, prop => {
96 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))97 })

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 98why, one step back
98

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 99–100why, one step back
99 const state: ES5ObjectState | ES5ArrayState = {
100 type: isArray ? "es5_array" : ("es5_object" as any),

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 101–119why, one step back
101 scope,
102 modified: false,103 finalizing: false, // es5 only104 finalized: false,105 assigned: isMap(base) ? new Map() : {},106 parent,107 base,108 draft,109 drafts: isSet(base) ? new Map() : null,110 copy: null,111 revoke,112 revoked: false // es5 only113 }114115 createHiddenProperty(draft, DRAFT_STATE, state)116 scope.drafts!.push(draft)117 return draft118}119

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 120why, one step back
120function revoke(this: ES5BaseState) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 121–123why, one step back
121 this.revoked = true
122}123

basic map tests succeedingMichel Weststratebefore adoption735c402

line 124why, one step back
124// TODO: type these methods

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 125–143why, one step back
125// Access a property without creating an Immer draft.
126function peek(draft, prop) {127 const state = draft[DRAFT_STATE]128 if (state && !state.finalizing) {129 state.finalizing = true130 const value = draft[prop]131 state.finalizing = false132 return value133 }134 return draft[prop]135}136137function get(state, prop) {138 assertUnrevoked(state)139 const value = peek(latest(state), prop)140 if (state.finalizing) return value141 // Create a draft if the value is unmodified.142 if (value === peek(state.base, prop) && isDraftable(value)) {143 prepareCopy(state)

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

line 144why, one step back
144 return (state.copy[prop] = createES5Proxy(value, state))

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 145–148why, one step back
145 }
146 return value147}148

basic map tests succeedingMichel Weststratebefore adoption735c402

line 149why, one step back
149function set(state: ES5State, prop, value) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 150–153why, one step back
150 assertUnrevoked(state)
151 state.assigned[prop] = true152 if (!state.modified) {153 if (is(value, peek(latest(state), prop))) return

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 155–156why, one step back
155 prepareCopy(state)
156 }

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 157why, one step back
157 state.copy![prop] = value

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 158–159why, one step back
158}
159

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

line 160why, one step back
160export function markChangedES5(state) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 161–162why, one step back
161 if (!state.modified) {
162 state.modified = true

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

line 163why, one step back
163 if (state.parent) markChangedES5(state.parent)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 164–167why, one step back
164 }
165}166167// TODO: kill export

basic map tests succeedingMichel Weststratebefore adoption735c402

line 168why, one step back
168function prepareCopy(state) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 169–205why, one step back
169 if (!state.copy) state.copy = clonePotentialDraft(state.base)
170}171172function clonePotentialDraft(base) {173 const state = base && base[DRAFT_STATE]174 if (state) {175 state.finalizing = true176 const draft = shallowCopy(state.draft, true)177 state.finalizing = false178 return draft179 }180 return shallowCopy(base)181}182183// property descriptors are recycled to make sure we don't create a get and set closure per property,184// but share them all instead185const descriptors = {}186187function proxyProperty(draft, prop, enumerable) {188 let desc = descriptors[prop]189 if (desc) {190 desc.enumerable = enumerable191 } else {192 descriptors[prop] = desc = {193 configurable: true,194 enumerable,195 get() {196 return get(this[DRAFT_STATE], prop)197 },198 set(value) {199 set(this[DRAFT_STATE], prop, value)200 }201 }202 }203 Object.defineProperty(draft, prop, desc)204}205

map unification WIPMichel Weststratebefore adoption9bcefb6

line 206why, one step back
206export function assertUnrevoked(state) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 207–223why, one step back
207 if (state.revoked === true)
208 throw new Error(209 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +210 JSON.stringify(latest(state))211 )212}213214// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.215function markChangesSweep(drafts) {216 // The natural order of drafts in the `scope` array is based on when they217 // were accessed. By processing drafts in reverse natural order, we have a218 // better chance of processing leaf nodes first. When a leaf node is known to219 // have changed, we can avoid any traversal of its ancestor nodes.220 for (let i = drafts.length - 1; i >= 0; i--) {221 const state = drafts[i][DRAFT_STATE]222 if (!state.modified) {223 if (Array.isArray(state.base)) {

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 225why, one step back
225 } else if (isMap(state.base)) {

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

line 226why, one step back
226 if (hasMapChanges(state)) markChangedES5(state)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 227why, one step back
227 } else if (isSet(state.base)) {

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

line 228why, one step back
228 if (hasSetChanges(state)) markChangedES5(state)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 229why, one step back
229 } else if (hasObjectChanges(state)) {

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 231–235why, one step back
231 }
232 }233 }234}235

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 236–237why, one step back
236// TODO: refactor this to work per object-type
237// TODO: Set / Map shouldn't be ES specific

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 238–242why, one step back
238function markChangesRecursively(object) {
239 if (!object || typeof object !== "object") return240 const state = object[DRAFT_STATE]241 if (!state) return242 const {base, draft, assigned} = state

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 243–244why, one step back
243 if (isSet(object)) {
244 if (hasSetChanges(state)) {

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 246–255why, one step back
246 object.forEach(v => {
247 markChangesRecursively(v)248 })249 }250 } else if (isMap(object)) {251 // if (hasMapChanges(object)) {252 object.forEach((value, key) => {253 if (assigned && base.get(key) === undefined && !has(base, key)) {254 // TODO: this code seems invalid for Maps!255 assigned.set(key, true)

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 257–270why, one step back
257 } else if (!assigned || !assigned.get(key)) {
258 // TODO: === false?259 // Only untouched properties trigger recursion.260 markChangesRecursively(draft.get(key))261 }262 })263 // Look for removed keys.264 // TODO: is this code needed?265 // TODO: use each?266 if (assigned)267 base.forEach((value, key) => {268 // The `undefined` check is a fast path for pre-existing keys.269 if (draft.get(key) === undefined && !has(draft, key)) {270 assigned.set(key, false)

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 272–275why, one step back
272 }
273 })274 // }275 } else if (!Array.isArray(object)) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 276why, one step back
276 // Look for added keys.

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 277why, one step back
277 // TODO: use each?

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 278–280why, one step back
278 Object.keys(draft).forEach(key => {
279 // The `undefined` check is a fast path for pre-existing keys.280 if (base[key] === undefined && !has(base, key)) {

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 281why, one step back
281 // TODO: this code seems invalid for Maps!

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 282why, one step back
282 assigned[key] = true

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 285why, one step back
285 // TODO: === false ?

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 286–290why, one step back
286 // Only untouched properties trigger recursion.
287 markChangesRecursively(draft[key])288 }289 })290 // Look for removed keys.

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 291–292why, one step back
291 // TODO: is this code needed?
292 // TODO: use each?

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 293–296why, one step back
293 Object.keys(base).forEach(key => {
294 // The `undefined` check is a fast path for pre-existing keys.295 if (draft[key] === undefined && !has(draft, key)) {296 assigned[key] = false

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 298–300why, one step back
298 }
299 })300 } else if (hasArrayChanges(state)) {

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 302–367why, one step back
302 assigned.length = true
303 if (draft.length < base.length) {304 for (let i = draft.length; i < base.length; i++) assigned[i] = false305 } else {306 for (let i = base.length; i < draft.length; i++) assigned[i] = true307 }308 for (let i = 0; i < draft.length; i++) {309 // Only untouched indices trigger recursion.310 if (assigned[i] === undefined) markChangesRecursively(draft[i])311 }312 }313}314315function hasObjectChanges(state) {316 const {base, draft} = state317318 // Search for added keys and changed keys. Start at the back, because319 // non-numeric keys are ordered by time of definition on the object.320 const keys = Object.keys(draft)321 for (let i = keys.length - 1; i >= 0; i--) {322 const key = keys[i]323 const baseValue = base[key]324 // The `undefined` check is a fast path for pre-existing keys.325 if (baseValue === undefined && !has(base, key)) {326 return true327 }328 // Once a base key is deleted, future changes go undetected, because its329 // descriptor is erased. This branch detects any missed changes.330 else {331 const value = draft[key]332 const state = value && value[DRAFT_STATE]333 if (state ? state.base !== baseValue : !is(value, baseValue)) {334 return true335 }336 }337 }338339 // At this point, no keys were added or changed.340 // Compare key count to determine if keys were deleted.341 return keys.length !== Object.keys(base).length342}343344function hasArrayChanges(state) {345 const {draft} = state346 if (draft.length !== state.base.length) return true347 // See #116348 // If we first shorten the length, our array interceptors will be removed.349 // If after that new items are added, result in the same original length,350 // those last items will have no intercepting property.351 // So if there is no own descriptor on the last position, we know that items were removed and added352 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check353 // the last one354 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)355 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)356 if (descriptor && !descriptor.get) return true357 // For all other cases, we don't have to compare, as they would have been picked up by the index setters358 return false359}360361function createHiddenProperty(target, prop, value) {362 Object.defineProperty(target, prop, {363 value: value,364 enumerable: false,365 writable: true366 })367}