proxy.js
This file
Path
src/proxy.js
183 lines · 5.4 KiB · JavaScript
Last changed by
chore: comments and formattingAlec Larsonbefore adoptionff96173
History of this fileTake 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 back1"use strict"2import {fix: always use `assign` polyfillaleclarsonbefore adoption241a648
lines 3–4why, one step back3 assign,4 each,fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 5why, one step back5 has,fix: always use `assign` polyfillaleclarsonbefore adoption241a648
line 6why, one step back6 is,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 7–8why, one step back7 isDraftable,8 isDraft,fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 9why, one step back9 shallowCopy,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 10why, one step back10 DRAFT_STATEfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 11why, one step back11} from "./common"Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 12why, one step back12import {ImmerScope} from "./scope"fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 13why, one step back13feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 14–16why, one step back14// Do nothing before being finalized.15export function willFinalize() {}16Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 17–18why, one step back17export function createProxy(base, parent) {18 const scope = parent ? parent.scope : ImmerScope.currentfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 19why, one step back19 const state = {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 20why, one step back20 // Track which produce call this is associated with.Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 21why, one step back21 scope,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 22–28why, one step back22 // 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.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 29why, one step back29 parent,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 30why, one step back30 // The base state.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 31why, one step back31 base,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 32–36why, one step back32 // The base proxy.33 draft: null,34 // Any property proxies.35 drafts: {},36 // The base copy with any updated values.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 37why, one step back37 copy: null,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 38–39why, one step back38 // Called by the `produce` function.39 revoke: nullfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 40–42why, one step back40 }4142 const {revoke, proxy} = Array.isArray(base)Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 43–45why, one step back43 ? // [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)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 46–50why, one step back46 : Proxy.revocable(state, objectTraps)4748 state.draft = proxy49 state.revoke = revoke50Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 51why, one step back51 scope.drafts.push(proxy)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 52–54why, one step back52 return proxy53}54fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 55–65why, one step back55const objectTraps = {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,feat: limited class supportaleclarsonbefore adoption9877d64
lines 66–68why, one step back66 defineProperty() {67 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore68 },fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8
lines 69–71why, one step back69 getPrototypeOf(target) {70 return Object.getPrototypeOf(target.base)71 },fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 72why, one step back72 setPrototypeOf() {feat: limited class supportaleclarsonbefore adoption9877d64
line 73why, one step back73 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignorefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 74–84why, one step back74 }75}7677const arrayTraps = {}78each(objectTraps, (key, fn) => {79 arrayTraps[key] = function() {80 arguments[0] = arguments[0][0]81 return fn.apply(this, arguments)82 }83})84arrayTraps.deleteProperty = function(state, prop) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 85–87why, one step back85 if (isNaN(parseInt(prop))) {86 throw new Error("Immer only supports deleting array indices") // prettier-ignore87 }fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 88–90why, one step back88 return objectTraps.deleteProperty.call(this, state[0], prop)89}90arrayTraps.set = function(state, prop, value) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 91–93why, one step back91 if (prop !== "length" && isNaN(parseInt(prop))) {92 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore93 }fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 94–96why, one step back94 return objectTraps.set.call(this, state[0], prop, value)95}96Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 97why, one step back97// returns the object we should be reading the current value from, which is base, until some change has been madefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 98why, one step back98function source(state) {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 99why, one step back99 return state.copy || state.basefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 100–101why, one step back100}101fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
lines 102–111why, one step back102// Access a property without creating an Immer draft.103function peek(draft, prop) {104 const state = draft[DRAFT_STATE]105 const desc = Reflect.getOwnPropertyDescriptor(106 state ? source(state) : draft,107 prop108 )109 return desc && desc.value110}111fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 112why, one step back112function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 113why, one step back113 if (prop === DRAFT_STATE) return statefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 114–121why, one step back114 let {drafts} = state115116 // Check for existing draft in unmodified state.117 if (!state.modified && has(drafts, prop)) {118 return drafts[prop]119 }120121 const value = source(state)[prop]chore: comments and formattingAlec Larsonbefore adoptionff96173
lines 122–124why, one step back122 if (state.finalized || !isDraftable(value)) {123 return value124 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 125–126why, one step back125126 // Check for existing draft in modified state.fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 127why, one step back127 if (state.modified) {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 128why, one step back128 // Assigned values are never drafted. This catches any drafts we created, too.fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
line 129why, one step back129 if (value !== peek(state.base, prop)) return valuefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 130–131why, one step back130 // Store drafts on the copy (when one exists).131 drafts = state.copyfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 132why, one step back132 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 133why, one step back133Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 134why, one step back134 return (drafts[prop] = createProxy(value, state))fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 135–138why, one step back135}136137function set(state, prop, value) {138 if (!state.modified) {fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
line 139why, one step back139 const baseValue = peek(state.base, prop)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 140–141why, one step back140 // Optimize based on value's truthiness. Truthy values are guaranteed to141 // never be undefined, so we can avoid the `in` operator. Lastly, truthyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 142why, one step back142 // values may be drafts, but falsy values are never drafts.perf: proxy assignaleclarsonbefore adoption53e9ce8
line 143why, one step back143 const isUnchanged = valuefix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
lines 144–145why, one step back144 ? is(baseValue, value) || value === state.drafts[prop]145 : is(baseValue, value) && prop in state.baseperf: proxy assignaleclarsonbefore adoption53e9ce8
line 146why, one step back146 if (isUnchanged) return truefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 147–148why, one step back147 markChanged(state)148 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 149why, one step back149 state.assigned[prop] = truefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 150–154why, one step back150 state.copy[prop] = value151 return true152}153154function deleteProperty(state, prop) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
line 155why, one step back155 // The `undefined` check is a fast path for pre-existing keys.fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
line 156why, one step back156 if (peek(state.base, prop) !== undefined || prop in state.base) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
lines 157–160why, one step back157 state.assigned[prop] = false158 markChanged(state)159 }160 if (state.copy) delete state.copy[prop]fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 161–163why, one step back161 return true162}163chore: comments and formattingAlec Larsonbefore adoptionff96173
lines 164–165why, one step back164// Note: We never coerce `desc.value` into an Immer draft, because we can't make165// the same guarantee in ES5 mode.fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 166why, one step back166function getOwnPropertyDescriptor(state, prop) {fix: property descriptor edge casesaleclarsonbefore adoptionafaa737
lines 167–173why, one step back167 const owner = source(state)168 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)169 if (desc) {170 desc.writable = true171 desc.configurable = !Array.isArray(owner) || prop !== "length"172 }173 return descfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 174–178why, one step back174}175176function markChanged(state) {177 if (!state.modified) {178 state.modified = truefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 179–180why, one step back179 state.copy = assign(shallowCopy(state.base), state.drafts)180 state.drafts = nullfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 181–183why, one step back181 if (state.parent) markChanged(state.parent)182 }183}