Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

186 lines · 4.9 KiB · JavaScript

Last changed by

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

History of this file

Take it

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

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 3–10why, one step back
3 assign,
4 each,5 has,6 is,7 isDraftable,8 isDraft,9 shallowCopy,10 DRAFT_STATE

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

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

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 13why, one step back
13

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 14–16why, one step back
14// Do nothing before being finalized.
15export function willFinalize() {}16

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 18–40why, one step back
18 const scope = parent ? parent.scope : ImmerScope.current
19 const state = {20 // Track which produce call this is associated with.21 scope,22 // True for both shallow and deep changes.23 modified: false,24 // Used during finalization.25 finalized: false,26 // Track which properties have been assigned (true) or deleted (false).27 assigned: {},28 // The parent draft state.29 parent,30 // The base state.31 base,32 // The base proxy.33 draft: null,34 // Any property proxies.35 drafts: {},36 // The base copy with any updated values.37 copy: null,38 // Called by the `produce` function.39 revoke: null40 }

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 41why, one step back
41

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 42–46why, one step back
42 const {revoke, proxy} = Array.isArray(base)
43 ? // [state] is used for arrays, to make sure the proxy is array-ish and not violate invariants,44 // although state itself is an object45 Proxy.revocable([state], arrayTraps)46 : Proxy.revocable(state, objectTraps)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 47why, one step back
47

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 48–49why, one step back
48 state.draft = proxy
49 state.revoke = revoke

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 50why, one step back
50

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 51–52why, one step back
51 scope.drafts.push(proxy)
52 return proxy

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 55why, one step back
55const objectTraps = {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 56–74why, one step back
56 get,
57 has(target, prop) {58 return prop in source(target)59 },60 ownKeys(target) {61 return Reflect.ownKeys(source(target))62 },63 set,64 deleteProperty,65 getOwnPropertyDescriptor,66 defineProperty() {67 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore68 },69 getPrototypeOf(target) {70 return Object.getPrototypeOf(target.base)71 },72 setPrototypeOf() {73 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore74 }

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 75–78why, one step back
75}
7677const arrayTraps = {}78each(objectTraps, (key, fn) => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 79–82why, one step back
79 arrayTraps[key] = function() {
80 arguments[0] = arguments[0][0]81 return fn.apply(this, arguments)82 }

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 83–84why, one step back
83})
84arrayTraps.deleteProperty = function(state, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 85–88why, one step back
85 if (isNaN(parseInt(prop))) {
86 throw new Error("Immer only supports deleting array indices") // prettier-ignore87 }88 return objectTraps.deleteProperty.call(this, state[0], prop)

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 89–90why, one step back
89}
90arrayTraps.set = function(state, prop, value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 91–94why, one step back
91 if (prop !== "length" && isNaN(parseInt(prop))) {
92 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore93 }94 return objectTraps.set.call(this, state[0], prop, value)

feat: `Map` supportkeenondrumsbefore adoption8276a6c

lines 95–96why, one step back
95}
96

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 97–98why, one step back
97// returns the object we should be reading the current value from, which is base, until some change has been made
98function source(state) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 100why, one step back
100}

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 101why, one step back
101

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 102–103why, one step back
102// Access a property without creating an Immer draft.
103function peek(draft, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 104–109why, one step back
104 const state = draft[DRAFT_STATE]
105 const desc = Reflect.getOwnPropertyDescriptor(106 state ? source(state) : draft,107 prop108 )109 return desc && desc.value

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

lines 110–111why, one step back
110}
111

fix: revert Map supportAlec Larsonbefore adoption47529d3

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 113–114why, one step back
113 if (prop === DRAFT_STATE) return state
114 let {drafts} = state

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 115why, one step back
115

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 116–119why, one step back
116 // Check for existing draft in unmodified state.
117 if (!state.modified && has(drafts, prop)) {118 return drafts[prop]119 }

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 120why, one step back
120

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 121–124why, one step back
121 const value = source(state)[prop]
122 if (state.finalized || !isDraftable(value)) {123 return value124 }

feat: `Map` supportkeenondrumsbefore adoption8276a6c

line 125why, one step back
125

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 126–132why, one step back
126 // Check for existing draft in modified state.
127 if (state.modified) {128 // Assigned values are never drafted. This catches any drafts we created, too.129 if (value !== peek(state.base, prop)) return value130 // Store drafts on the copy (when one exists).131 drafts = state.copy132 }

feat: `Map` supportkeenondrumsbefore adoption8276a6c

line 133why, one step back
133

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 134why, one step back
134 return (drafts[prop] = createProxy(value, state))

chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc

line 135why, one step back
135}

feat: `Map` supportkeenondrumsbefore adoption8276a6c

line 136why, one step back
136

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 137why, one step back
137function set(state, prop, value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 138–151why, one step back
138 if (!state.modified) {
139 const baseValue = peek(state.base, prop)140 // Optimize based on value's truthiness. Truthy values are guaranteed to141 // never be undefined, so we can avoid the `in` operator. Lastly, truthy142 // values may be drafts, but falsy values are never drafts.143 const isUnchanged = value144 ? is(baseValue, value) || value === state.drafts[prop]145 : is(baseValue, value) && prop in state.base146 if (isUnchanged) return true147 markChanged(state)148 }149 state.assigned[prop] = true150 state.copy[prop] = value151 return true

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 152–153why, one step back
152}
153

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 154why, one step back
154function deleteProperty(state, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 155–158why, one step back
155 // The `undefined` check is a fast path for pre-existing keys.
156 if (peek(state.base, prop) !== undefined || prop in state.base) {157 state.assigned[prop] = false158 markChanged(state)

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 159–161why, one step back
159 } else if (state.assigned[prop]) {
160 // if an originally not assigned property was deleted161 delete state.assigned[prop]

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 162–164why, one step back
162 }
163 if (state.copy) delete state.copy[prop]164 return true

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 165–166why, one step back
165}
166

fix: revert Map supportAlec Larsonbefore adoption47529d3

lines 167–169why, one step back
167// Note: We never coerce `desc.value` into an Immer draft, because we can't make
168// the same guarantee in ES5 mode.169function getOwnPropertyDescriptor(state, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 170–176why, one step back
170 const owner = source(state)
171 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)172 if (desc) {173 desc.writable = true174 desc.configurable = !Array.isArray(owner) || prop !== "length"175 }176 return desc

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

lines 177–179why, one step back
177}
178179function markChanged(state) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 180–185why, one step back
180 if (!state.modified) {
181 state.modified = true182 state.copy = assign(shallowCopy(state.base), state.drafts)183 state.drafts = null184 if (state.parent) markChanged(state.parent)185 }

fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2

line 186why, one step back
186}