Skip to content
Myr

es5.ts

This file

Path

src/es5.ts

309 lines · 8.4 KiB · TypeScript

Last changed by

Processed bunch of todosMichel Weststratebefore adoption00425e1

History of this file

Take it

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 13–14why, one step back
13 latest,
14 createHiddenProperty

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

added set and iteror stuffMichel Weststratebefore adoption08f4682

line 16why, one step back
16

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 18–26why, one step back
18import {
19 ImmerState,20 Drafted,21 AnyObject,22 AnyMap,23 Objectish,24 ImmerBaseState,25 AnyArray26} from "./types"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 27why, one step back
27

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 28why, one step back
28interface ES5BaseState extends ImmerBaseState {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 29why, one step back
29 finalizing: boolean

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 30why, one step back
30 assigned: {[key: string]: any}

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 31why, one step back
31 parent?: ImmerState

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 32why, one step back
32 revoke(): void

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 33–35why, one step back
33 revoked: boolean
34}35

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 36–45why, one step back
36export interface ES5ObjectState extends ES5BaseState {
37 type: "es5_object"38 draft: Drafted<AnyObject, ES5ObjectState>39 base: AnyObject40 copy: AnyObject | null41}4243export interface ES5ArrayState extends ES5BaseState {44 type: "es5_array"45 draft: Drafted<AnyObject, ES5ArrayState>

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 46–47why, one step back
46 base: AnyArray
47 copy: AnyArray | null

Introduced proper state typesMichel Weststratebefore adoption3842f85

lines 48–51why, one step back
48}
4950type ES5State = ES5ArrayState | ES5ObjectState51

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

line 74why, one step back
74 base: T,

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 75why, one step back
75 parent?: ImmerState

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 76why, one step back
76): Drafted<T, ES5ObjectState | ES5ArrayState> {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 77–79why, one step back
77 const isArray = Array.isArray(base)
78 const draft = clonePotentialDraft(base)79

added set and iteror stuffMichel Weststratebefore adoption08f4682

lines 80–82why, one step back
80 each(draft, prop => {
81 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))82 })

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 83why, one step back
83

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 86why, one step back
86 scope: parent ? parent.scope : ImmerScope.current!,

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 87why, one step back
87 modified: false,

Processed bunch of todosMichel Weststratebefore adoption00425e1

line 88why, one step back
88 finalizing: false,

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 89why, one step back
89 finalized: false,

clean up create proxyMichel Weststratebefore adoptionc182c1e

line 90why, one step back
90 assigned: {},

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 91–95why, one step back
91 parent,
92 base,93 draft,94 copy: null,95 revoke,

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 96–97why, one step back
96 revoked: false,
97 isManual: false

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 98–103why, one step back
98 }
99100 createHiddenProperty(draft, DRAFT_STATE, state)101 return draft102}103

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 105–108why, one step back
105 this.revoked = true
106}107108// Access a property without creating an Immer draft.

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 109why, one step back
109function peek(draft: Drafted, prop: PropertyKey) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 110–119why, one step back
110 const state = draft[DRAFT_STATE]
111 if (state && !state.finalizing) {112 state.finalizing = true113 const value = draft[prop]114 state.finalizing = false115 return value116 }117 return draft[prop]118}119

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 120why, one step back
120function get(state: ES5State, prop: string | number) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 121–126why, one step back
121 assertUnrevoked(state)
122 const value = peek(latest(state), prop)123 if (state.finalizing) return value124 // Create a draft if the value is unmodified.125 if (value === peek(state.base, prop) && isDraftable(value)) {126 prepareCopy(state)

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 128why, one step back
128 return (state.copy![prop] = state.scope.immer.createProxy(value, state))

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 129–132why, one step back
129 }
130 return value131}132

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 133why, one step back
133function set(state: ES5State, prop: string | number, value: any) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 134–137why, one step back
134 assertUnrevoked(state)
135 state.assigned[prop] = true136 if (!state.modified) {137 if (is(value, peek(latest(state), prop))) return

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 139–140why, one step back
139 prepareCopy(state)
140 }

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Introduced proper state typesMichel Weststratebefore adoption3842f85

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 143–144why, one step back
143}
144

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 149–151why, one step back
149 }
150}151

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 153–155why, one step back
153 if (!state.copy) state.copy = clonePotentialDraft(state.base)
154}155

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 156–157why, one step back
156function clonePotentialDraft(base: Objectish) {
157 const state = base && (base as any)[DRAFT_STATE]

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 158–168why, one step back
158 if (state) {
159 state.finalizing = true160 const draft = shallowCopy(state.draft, true)161 state.finalizing = false162 return draft163 }164 return shallowCopy(base)165}166167// property descriptors are recycled to make sure we don't create a get and set closure per property,168// but share them all instead

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 169why, one step back
169const descriptors: {[prop: string]: PropertyDescriptor} = {}

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 170why, one step back
170

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 171–175why, one step back
171function proxyProperty(
172 draft: Drafted<any, ES5State>,173 prop: string | number,174 enumerable: boolean175) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 176–182why, one step back
176 let desc = descriptors[prop]
177 if (desc) {178 desc.enumerable = enumerable179 } else {180 descriptors[prop] = desc = {181 configurable: true,182 enumerable,

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 183why, one step back
183 get(this: any) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 184–185why, one step back
184 return get(this[DRAFT_STATE], prop)
185 },

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 186why, one step back
186 set(this: any, value) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 187–193why, one step back
187 set(this[DRAFT_STATE], prop, value)
188 }189 }190 }191 Object.defineProperty(draft, prop, desc)192}193

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 194why, one step back
194export function assertUnrevoked(state: ES5State) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 195–202why, one step back
195 if (state.revoked === true)
196 throw new Error(197 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +198 JSON.stringify(latest(state))199 )200}201202// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 203why, one step back
203function markChangesSweep(drafts: Drafted<any, ImmerState>[]) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 204–210why, one step back
204 // The natural order of drafts in the `scope` array is based on when they
205 // were accessed. By processing drafts in reverse natural order, we have a206 // better chance of processing leaf nodes first. When a leaf node is known to207 // have changed, we can avoid any traversal of its ancestor nodes.208 for (let i = drafts.length - 1; i >= 0; i--) {209 const state = drafts[i][DRAFT_STATE]210 if (!state.modified) {

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 211–217why, one step back
211 switch (state.type) {
212 case "es5_array":213 if (hasArrayChanges(state)) markChangedES5(state)214 break215 case "es5_object":216 if (hasObjectChanges(state)) markChangedES5(state)217 break

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 218–222why, one step back
218 }
219 }220 }221}222

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 223why, one step back
223function markChangesRecursively(object: any) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 224–226why, one step back
224 if (!object || typeof object !== "object") return
225 const state = object[DRAFT_STATE]226 if (!state) return

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 227–228why, one step back
227 const {base, draft, assigned, type} = state
228 if (type === "es5_object") {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 230–232why, one step back
230 // TODO: looks quite duplicate to hasObjectChanges
231 each(draft, key => {232 if ((key as any) === DRAFT_STATE) return

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 233–235why, one step back
233 // The `undefined` check is a fast path for pre-existing keys.
234 if (base[key] === undefined && !has(base, key)) {235 assigned[key] = true

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 237–242why, one step back
237 } else if (!assigned[key]) {
238 // Only untouched properties trigger recursion.239 markChangesRecursively(draft[key])240 }241 })242 // Look for removed keys.

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 243why, one step back
243 each(base, key => {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 244–246why, one step back
244 // The `undefined` check is a fast path for pre-existing keys.
245 if (draft[key] === undefined && !has(draft, key)) {246 assigned[key] = false

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 248–249why, one step back
248 }
249 })

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 250why, one step back
250 } else if (type === "es5_array" && hasArrayChanges(state)) {

Stop abusing modules as exportsMichel Weststratebefore adoption7b0ad53

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 252–264why, one step back
252 assigned.length = true
253 if (draft.length < base.length) {254 for (let i = draft.length; i < base.length; i++) assigned[i] = false255 } else {256 for (let i = base.length; i < draft.length; i++) assigned[i] = true257 }258 for (let i = 0; i < draft.length; i++) {259 // Only untouched indices trigger recursion.260 if (assigned[i] === undefined) markChangesRecursively(draft[i])261 }262 }263}264

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 266–293why, one step back
266 const {base, draft} = state
267268 // Search for added keys and changed keys. Start at the back, because269 // non-numeric keys are ordered by time of definition on the object.270 const keys = Object.keys(draft)271 for (let i = keys.length - 1; i >= 0; i--) {272 const key = keys[i]273 const baseValue = base[key]274 // The `undefined` check is a fast path for pre-existing keys.275 if (baseValue === undefined && !has(base, key)) {276 return true277 }278 // Once a base key is deleted, future changes go undetected, because its279 // descriptor is erased. This branch detects any missed changes.280 else {281 const value = draft[key]282 const state = value && value[DRAFT_STATE]283 if (state ? state.base !== baseValue : !is(value, baseValue)) {284 return true285 }286 }287 }288289 // At this point, no keys were added or changed.290 // Compare key count to determine if keys were deleted.291 return keys.length !== Object.keys(base).length292}293

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 295–309why, one step back
295 const {draft} = state
296 if (draft.length !== state.base.length) return true297 // See #116298 // If we first shorten the length, our array interceptors will be removed.299 // If after that new items are added, result in the same original length,300 // those last items will have no intercepting property.301 // So if there is no own descriptor on the last position, we know that items were removed and added302 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check303 // the last one304 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)305 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)306 if (descriptor && !descriptor.get) return true307 // For all other cases, we don't have to compare, as they would have been picked up by the index setters308 return false309}