Skip to content
Myr

es5.js

This file

Path

src/es5.js

218 lines · 7.0 KiB · JavaScript

Last changed by

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

History of this file

Take it

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

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 1–4why, one step back
1"use strict"
2// @ts-check34import {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 5–6why, one step back
5 each,
6 has,

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

line 7why, one step back
7 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 8–9why, one step back
8 isDraft,
9 isDraftable,

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

line 10why, one step back
10 shallowCopy,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 11why, one step back
11 DRAFT_STATE

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 12–15why, one step back
12} from "./common"
1314const descriptors = {}15

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 16–38why, one step back
16// For nested produce calls:
17export const scopes = []18export const currentScope = () => scopes[scopes.length - 1]1920export function willFinalize(result, baseDraft, needPatches) {21 const scope = currentScope()22 scope.forEach(state => (state.finalizing = true))23 if (result === undefined || result === baseDraft) {24 if (needPatches) markChangesRecursively(baseDraft)25 // This is faster when we don't care about which attributes changed.26 markChangesSweep(scope)27 }28}2930export function createDraft(base, parent) {31 if (isDraft(base)) throw new Error("This should never happen. Please report: https://github.com/mweststrate/immer/issues/new") // prettier-ignore3233 const draft = shallowCopy(base)34 each(base, prop => {35 Object.defineProperty(draft, "" + prop, createPropertyProxy("" + prop))36 })3738 const state = {

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 40–41why, one step back
40 finalizing: false,
41 finalized: false,

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 42why, one step back
42 assigned: {}, // true: value was assigned to these props, false: was removed

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 43–44why, one step back
43 parent,
44 base,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 45–48why, one step back
45 draft,
46 copy: null,47 revoke,48 revoked: false

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

line 49why, one step back
49 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 50–53why, one step back
50
51 createHiddenProperty(draft, DRAFT_STATE, state)52 currentScope().push(state)53 return draft

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 54–55why, one step back
54}
55

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 56–59why, one step back
56function revoke() {
57 this.revoked = true58}59

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

line 60why, one step back
60function source(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 62–64why, one step back
62}
6364function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

line 66why, one step back
66 const value = source(state)[prop]

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 67–68why, one step back
67 // Drafts are only created for proxyable values that exist in the base state.
68 if (!state.finalizing && value === state.base[prop] && isDraftable(value)) {

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 71–75why, one step back
71 }
72 return value73}7475function set(state, prop, value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 77why, one step back
77 state.assigned[prop] = true // optimization; skip this if there is no listener

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 78–93why, one step back
78 if (!state.modified) {
79 if (is(source(state)[prop], value)) return80 markChanged(state)81 prepareCopy(state)82 }83 state.copy[prop] = value84}8586function markChanged(state) {87 if (!state.modified) {88 state.modified = true89 if (state.parent) markChanged(state.parent)90 }91}9293function prepareCopy(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 94why, one step back
94 if (!state.copy) state.copy = shallowCopy(state.base)

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 95–103why, one step back
95}
9697function createPropertyProxy(prop) {98 return (99 descriptors[prop] ||100 (descriptors[prop] = {101 configurable: true,102 enumerable: true,103 get() {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 105–106why, one step back
105 },
106 set(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 108–112why, one step back
108 }
109 })110 )111}112

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

line 115why, one step back
115 throw new Error(

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 116–117why, one step back
116 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
117 JSON.stringify(state.copy || state.base)

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 118–120why, one step back
118 )
119}120

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 121–128why, one step back
121// This looks expensive, but only proxies are visited, and only objects without known changes are scanned.
122function markChangesSweep(scope) {123 // The natural order of drafts in the `scope` array is based on when they124 // were accessed. By processing drafts in reverse natural order, we have a125 // better chance of processing leaf nodes first. When a leaf node is known to126 // have changed, we can avoid any traversal of its ancestor nodes.127 for (let i = scope.length - 1; i >= 0; i--) {128 const state = scope[i]

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 129–136why, one step back
129 if (state.modified === false) {
130 if (Array.isArray(state.base)) {131 if (hasArrayChanges(state)) markChanged(state)132 } else if (hasObjectChanges(state)) markChanged(state)133 }134 }135}136

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 137–138why, one step back
137function markChangesRecursively(object) {
138 if (!object || typeof object !== "object") return

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 145–150why, one step back
145 // The `undefined` check is a fast path for pre-existing keys.
146 if (base[key] === undefined && !has(base, key)) {147 assigned[key] = true148 markChanged(state)149 } else if (!assigned[key]) {150 // Only untouched properties trigger recursion.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 152why, one step back
152 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 153why, one step back
153 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 154–156why, one step back
154 // Look for removed keys.
155 Object.keys(base).forEach(key => {156 // The `undefined` check is a fast path for pre-existing keys.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 158–160why, one step back
158 assigned[key] = false
159 markChanged(state)160 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 161why, one step back
161 })

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

lines 162–164why, one step back
162 } else if (hasArrayChanges(state)) {
163 markChanged(state)164 assigned.length = true

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 167why, one step back
167 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 169why, one step back
169 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: es5 markChangesRecursivelyaleclarsonbefore adoption4dd6070

line 173why, one step back
173 }

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 174–176why, one step back
174 }
175}176

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

line 177why, one step back
177function hasObjectChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 179–181why, one step back
179
180 // Search for added keys. Start at the back, because non-numeric keys181 // are ordered by time of definition on the object.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

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

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

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

nit: simplify hasObjectChanges loopaleclarsonbefore adoption3c7f374

line 185why, one step back
185 if (base[keys[i]] === undefined && !has(base, keys[i])) {

Merge pull request #249 from aleclarson/patch1Michel Weststratebefore adoption5360e6c

lines 186–192why, one step back
186 return true
187 }188 }189190 // Since no keys have been added, we can compare lengths to know if an191 // object has been deleted.192 return keys.length !== Object.keys(base).length

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 193–195why, one step back
193}
194195function hasArrayChanges(state) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df

lines 198–202why, one step back
198 // See #116
199 // If we first shorten the length, our array interceptors will be removed.200 // If after that new items are added, result in the same original length,201 // those last items will have no intercepting property.202 // So if there is no own descriptor on the last position, we know that items were removed and added

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

lines 203–204why, one step back
203 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check
204 // the last one

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Fixed #117 returned proxies not being finalized when only a subset was returnedMichel Weststratebefore adoption8e61145

line 206why, one step back
206 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)

Fixed #116, failed to detect array changes when made shorter, then longerMichel Weststratebefore adoption76d33df

lines 207–209why, one step back
207 if (descriptor && !descriptor.get) return true
208 // For all other cases, we don't have to compare, as they would have been picked up by the index setters209 return false

Merge pull request #106 from asamiller/patch-1benbraoubefore adoption5281004

lines 210–218why, one step back
210}
211212function createHiddenProperty(target, prop, value) {213 Object.defineProperty(target, prop, {214 value: value,215 enumerable: false,216 writable: true217 })218}