Skip to content
Myr

es5.js

This file

Path

src/es5.js

467 lines · 13.2 KiB · JavaScript

Last changed by

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

History of this file

Take it

The walk stopped after 200 commits: lines older than 53e9ce8 point at it.

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 1–2why, one step back
1"use strict"
2import {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 3–4why, one step back
3 each,
4 has,

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 5why, one step back
5 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 6–7why, one step back
6 isDraft,
7 isDraftable,

nit: remove old `each` and rename `eachOwn`aleclarsonbefore adoptionc0ea419

line 8why, one step back
8 isEnumerable,

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 9–10why, one step back
9 isMap,
10 isSet,

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 11why, one step back
11 shallowCopy,

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 12–15why, one step back
12 DRAFT_STATE,
13 iterateMapValues,14 makeIterable,15 makeIterateSetValues

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 18why, one step back
18

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 19–26why, one step back
19export function willFinalize(scope, result, isReplaced) {
20 scope.drafts.forEach(draft => {21 draft[DRAFT_STATE].finalizing = true22 })23 if (!isReplaced) {24 if (scope.patches) {25 markChangesRecursively(scope.drafts[0])26 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 27why, one step back
27 // This is faster when we don't care about which attributes changed.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 28why, one step back
28 markChangesSweep(scope.drafts)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 29why, one step back
29 }

fix(es5): throw if a modified child draft is returnedaleclarsonbefore adoptioneabe9db

lines 30–33why, one step back
30 // When a child draft is returned, look for changes.
31 else if (isDraft(result) && result[DRAFT_STATE].scope === scope) {32 markChangesSweep(scope.drafts)33 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 34–35why, one step back
34}
35

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 36why, one step back
36export function createProxy(base, parent) {

feat: limited class supportaleclarsonbefore adoption9877d64

line 37why, one step back
37 const isArray = Array.isArray(base)

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

line 38why, one step back
38 const draft = clonePotentialDraft(base)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 39why, one step back
39

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 40–49why, one step back
40 if (isMap(base)) {
41 proxyMap(draft)42 } else if (isSet(base)) {43 proxySet(draft)44 } else {45 each(draft, prop => {46 proxyProperty(draft, prop, isArray || isEnumerable(base, prop))47 })48 }49

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 50why, one step back
50 // See "proxy.js" for property documentation.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 51why, one step back
51 const scope = parent ? parent.scope : ImmerScope.current

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 52why, one step back
52 const state = {

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 53why, one step back
53 scope,

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 55why, one step back
55 finalizing: false, // es5 only

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 58–59why, one step back
58 parent,
59 base,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 60why, one step back
60 draft,

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

line 61why, one step back
61 drafts: isSet(base) ? new Map() : null,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 62–63why, one step back
62 copy: null,
63 revoke,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 64why, one step back
64 revoked: false // es5 only

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 65why, one step back
65 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 66–67why, one step back
66
67 createHiddenProperty(draft, DRAFT_STATE, state)

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 68why, one step back
68 scope.drafts.push(draft)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 69why, one step back
69 return draft

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 70–71why, one step back
70}
71

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 72–75why, one step back
72function revoke() {
73 this.revoked = true74}75

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 76why, one step back
76function latest(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 77why, one step back
77 return state.copy || state.base

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 78–79why, one step back
78}
79

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 80–91why, one step back
80// Access a property without creating an Immer draft.
81function peek(draft, prop) {82 const state = draft[DRAFT_STATE]83 if (state && !state.finalizing) {84 state.finalizing = true85 const value = draft[prop]86 state.finalizing = false87 return value88 }89 return draft[prop]90}91

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 92why, one step back
92function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 93why, one step back
93 assertUnrevoked(state)

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 94why, one step back
94 const value = peek(latest(state), prop)

fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4

lines 95–97why, one step back
95 if (state.finalizing) return value
96 // Create a draft if the value is unmodified.97 if (value === peek(state.base, prop) && isDraftable(value)) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 100–104why, one step back
100 }
101 return value102}103104function set(state, prop, value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 105why, one step back
105 assertUnrevoked(state)

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 106why, one step back
106 state.assigned[prop] = true

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 107why, one step back
107 if (!state.modified) {

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 108why, one step back
108 if (is(value, peek(latest(state), prop))) return

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 109–122why, one step back
109 markChanged(state)
110 prepareCopy(state)111 }112 state.copy[prop] = value113}114115function markChanged(state) {116 if (!state.modified) {117 state.modified = true118 if (state.parent) markChanged(state.parent)119 }120}121122function prepareCopy(state) {

fix(es5): account for drafts in prepareCopyaleclarsonbefore adoption1e3e425

lines 123–134why, one step back
123 if (!state.copy) state.copy = clonePotentialDraft(state.base)
124}125126function clonePotentialDraft(base) {127 const state = base && base[DRAFT_STATE]128 if (state) {129 state.finalizing = true130 const draft = shallowCopy(state.draft, true)131 state.finalizing = false132 return draft133 }134 return shallowCopy(base)

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 135–136why, one step back
135}
136

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 137–140why, one step back
137// property descriptors are recycled to make sure we don't create a get and set closure per property,
138// but share them all instead139const descriptors = {}140

feat: limited class supportaleclarsonbefore adoption9877d64

lines 141–146why, one step back
141function proxyProperty(draft, prop, enumerable) {
142 let desc = descriptors[prop]143 if (desc) {144 desc.enumerable = enumerable145 } else {146 descriptors[prop] = desc = {

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 148why, one step back
148 enumerable,

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 149why, one step back
149 get() {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 150why, one step back
150 return get(this[DRAFT_STATE], prop)

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 151–152why, one step back
151 },
152 set(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 153why, one step back
153 set(this[DRAFT_STATE], prop, value)

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 154why, one step back
154 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 155–157why, one step back
155 }
156 }157 Object.defineProperty(draft, prop, desc)

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 160–312why, one step back
160function proxyMap(target) {
161 Object.defineProperties(target, mapTraps)162163 if (typeof Symbol !== undefined) {164 Object.defineProperty(165 target,166 Symbol.iterator,167 proxyMethod(iterateMapValues)168 )169 }170}171172const mapTraps = finalizeTraps({173 size: state => latest(state).size,174 get: state => key => {175 const value = latest(state).get(key)176177 if (state.finalizing || state.finalized || !isDraftable(value)) {178 return value179 }180181 if (value !== state.base.get(key)) {182 return value183 }184 const draft = createProxy(value, state)185 prepareCopy(state)186 state.copy.set(key, draft)187 return draft188 },189 set: state => (key, value) => {190 if (latest(state).get(key) !== value) {191 prepareCopy(state)192 markChanged(state)193 state.copy.set(key, value)194 }195 return state.draft196 },197 delete: state => key => {198 prepareCopy(state)199 markChanged(state)200 state.copy.delete(key)201 return false202 },203 clear: state => () => {204 if (!state.copy) {205 prepareCopy(state)206 }207 markChanged(state)208 return state.copy.clear()209 },210 has: state => key => {211 return latest(state).has(key)212 },213 forEach: (state, key, reciever) => cb => {214 latest(state).forEach((value, key, map) => {215 cb(reciever.get(key), key, map)216 })217 },218 keys: state => () => {219 return latest(state).keys()220 },221 values: iterateMapValues,222 entries: iterateMapValues223})224225function proxySet(target) {226 Object.defineProperties(target, setTraps)227228 if (typeof Symbol !== undefined) {229 Object.defineProperty(230 target,231 Symbol.iterator,232 proxyMethod(iterateSetValues)233 )234 }235}236237const iterateSetValues = makeIterateSetValues(createProxy)238239const setTraps = finalizeTraps({240 size: state => {241 return latest(state).size242 },243 add: state => value => {244 if (!latest(state).has(value)) {245 markChanged(state)246 if (!state.copy) {247 prepareCopy(state)248 }249 state.copy.add(value)250 }251 return state.draft252 },253 delete: state => value => {254 markChanged(state)255 if (!state.copy) {256 prepareCopy(state)257 }258 return state.copy.delete(value)259 },260 has: state => key => {261 return latest(state).has(key)262 },263 clear: state => () => {264 markChanged(state)265 if (!state.copy) {266 prepareCopy(state)267 }268 return state.copy.clear()269 },270 keys: iterateSetValues,271 entries: iterateSetValues,272 values: iterateSetValues,273 forEach: state => (cb, thisArg) => {274 const iterator = iterateSetValues(state)()275 let result = iterator.next()276 while (!result.done) {277 cb.call(thisArg, result.value, result.value, state.draft)278 result = iterator.next()279 }280 }281})282283function finalizeTraps(traps) {284 return Object.keys(traps).reduce(function(acc, key) {285 const builder = key === "size" ? proxyAttr : proxyMethod286 acc[key] = builder(traps[key], key)287 return acc288 }, {})289}290291function proxyAttr(fn) {292 return {293 get() {294 const state = this[DRAFT_STATE]295 assertUnrevoked(state)296 return fn(state)297 }298 }299}300301function proxyMethod(trap, key) {302 return {303 get() {304 return (...args) => {305 const state = this[DRAFT_STATE]306 assertUnrevoked(state)307 return trap(state, key, state.draft)(...args)308 }309 }310 }311}312

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 313–314why, one step back
313function assertUnrevoked(state) {
314 if (state.revoked === true)

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 315–316why, one step back
315 throw new Error(
316 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +

refactor: rename "source" helper to "latest"Alec Larsonbefore adoptioneba300f

line 317why, one step back
317 JSON.stringify(latest(state))

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 318–320why, one step back
318 )
319}320

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 321why, one step back
321// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 322why, one step back
322function markChangesSweep(drafts) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 323–326why, one step back
323 // The natural order of drafts in the `scope` array is based on when they
324 // were accessed. By processing drafts in reverse natural order, we have a325 // better chance of processing leaf nodes first. When a leaf node is known to326 // have changed, we can avoid any traversal of its ancestor nodes.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 327–329why, one step back
327 for (let i = drafts.length - 1; i >= 0; i--) {
328 const state = drafts[i][DRAFT_STATE]329 if (!state.modified) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 330–331why, one step back
330 if (Array.isArray(state.base)) {
331 if (hasArrayChanges(state)) markChanged(state)

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 332–338why, one step back
332 } else if (isMap(state.base)) {
333 if (hasMapChanges(state)) markChanged(state)334 } else if (isSet(state.base)) {335 if (hasSetChanges(state)) markChanged(state)336 } else if (hasObjectChanges(state)) {337 markChanged(state)338 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 339–344why, one step back
339 }
340 }341}342343function markChangesRecursively(object) {344 if (!object || typeof object !== "object") return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 345why, one step back
345 const state = object[DRAFT_STATE]

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 347why, one step back
347 const {base, draft, assigned} = state

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 348–349why, one step back
348 if (!Array.isArray(object)) {
349 // Look for added keys.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 350why, one step back
350 Object.keys(draft).forEach(key => {

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 351–356why, one step back
351 // The `undefined` check is a fast path for pre-existing keys.
352 if (base[key] === undefined && !has(base, key)) {353 assigned[key] = true354 markChanged(state)355 } else if (!assigned[key]) {356 // Only untouched properties trigger recursion.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 357why, one step back
357 markChangesRecursively(draft[key])

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 358–362why, one step back
358 }
359 })360 // Look for removed keys.361 Object.keys(base).forEach(key => {362 // The `undefined` check is a fast path for pre-existing keys.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 363why, one step back
363 if (draft[key] === undefined && !has(draft, key)) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 364–370why, one step back
364 assigned[key] = false
365 markChanged(state)366 }367 })368 } else if (hasArrayChanges(state)) {369 markChanged(state)370 assigned.length = true

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 371–372why, one step back
371 if (draft.length < base.length) {
372 for (let i = draft.length; i < base.length; i++) assigned[i] = false

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 373why, one step back
373 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 374why, one step back
374 for (let i = base.length; i < draft.length; i++) assigned[i] = true

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 375why, one step back
375 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 376why, one step back
376 for (let i = 0; i < draft.length; i++) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 377why, one step back
377 // Only untouched indices trigger recursion.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 378why, one step back
378 if (assigned[i] === undefined) markChangesRecursively(draft[i])

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 379–383why, one step back
379 }
380 }381}382383function hasObjectChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 384why, one step back
384 const {base, draft} = state

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 385why, one step back
385

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 388why, one step back
388 const keys = Object.keys(draft)

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

lines 390–391why, one step back
390 const key = keys[i]
391 const baseValue = base[key]

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 394–395why, one step back
394 return true
395 }

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

lines 396–404why, one step back
396 // Once a base key is deleted, future changes go undetected, because its
397 // descriptor is erased. This branch detects any missed changes.398 else {399 const value = draft[key]400 const state = value && value[DRAFT_STATE]401 if (state ? state.base !== baseValue : !is(value, baseValue)) {402 return true403 }404 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 405–406why, one step back
405 }
406

fix(es5): delete then set an existing propertyAlec Larsonbefore adoption6963420

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 409–412why, one step back
409 return keys.length !== Object.keys(base).length
410}411412function hasArrayChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 413–414why, one step back
413 const {draft} = state
414 if (draft.length !== state.base.length) return true

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 422why, one step back
422 const descriptor = Object.getOwnPropertyDescriptor(draft, draft.length - 1)

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 427–458why, one step back
427}
428429function hasMapChanges(state) {430 const {base, draft} = state431432 if (base.size !== draft.size) return true433434 // IE11 supports only forEach iteration435 let hasChanges = false436 draft.forEach(function(value, key) {437 if (!hasChanges) {438 hasChanges = isDraftable(value)439 ? value.modified440 : value !== base.get(key)441 }442 })443 return hasChanges444}445446function hasSetChanges(state) {447 const {base, draft} = state448449 if (base.size !== draft.size) return true450451 // IE11 supports only forEach iteration452 let hasChanges = false453 draft.forEach(function(value, key) {454 if (!hasChanges) {455 hasChanges = isDraftable(value) ? value.modified : !base.has(key)456 }457 })458 return hasChanges

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 459–467why, one step back
459}
460461function createHiddenProperty(target, prop, value) {462 Object.defineProperty(target, prop, {463 value: value,464 enumerable: false,465 writable: true466 })467}