proxy.js
This file
Path
src/proxy.js
289 lines · 8.5 KiB · JavaScript
Last changed by
feat: `Map` supportkeenondrumsbefore adoption8276a6c
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,feat: `Map` supportkeenondrumsbefore adoption8276a6c
line 9why, one step back9 isMap,fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 10why, one step back10 shallowCopy,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11 DRAFT_STATEfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 12why, one step back12} from "./common"Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 13why, one step back13import {ImmerScope} from "./scope"fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 14why, one step back14feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 15–17why, one step back15// Do nothing before being finalized.16export function willFinalize() {}17Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
lines 18–19why, one step back18export function createProxy(base, parent) {19 const scope = parent ? parent.scope : ImmerScope.currentfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 20why, one step back20 const state = {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 21why, one step back21 // Track which produce call this is associated with.Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 22why, one step back22 scope,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 23–29why, one step back23 // True for both shallow and deep changes.24 modified: false,25 // Used during finalization.26 finalized: false,27 // Track which properties have been assigned (true) or deleted (false).28 assigned: {},29 // The parent draft state.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 30why, one step back30 parent,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 31why, one step back31 // The base state.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 32why, one step back32 base,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 33–37why, one step back33 // The base proxy.34 draft: null,35 // Any property proxies.36 drafts: {},37 // The base copy with any updated values.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 38why, one step back38 copy: null,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 39–40why, one step back39 // Called by the `produce` function.40 revoke: nullfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 41–42why, one step back41 }42feat: `Map` supportkeenondrumsbefore adoption8276a6c
lines 43–50why, one step back43 let proxyTarget = state44 let traps = objectTraps45 if (Array.isArray(base)) {46 proxyTarget = [state]47 traps = arrayTraps48 } else if (isMap(base)) {49 traps = mapTraps50 }feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 51why, one step back51feat: `Map` supportkeenondrumsbefore adoption8276a6c
lines 52–53why, one step back52 const {revoke, proxy} = Proxy.revocable(proxyTarget, traps)53feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 54–56why, one step back54 state.draft = proxy55 state.revoke = revoke56Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 57why, one step back57 scope.drafts.push(proxy)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 58–60why, one step back58 return proxy59}60fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 61–71why, one step back61const objectTraps = {62 get,63 has(target, prop) {64 return prop in source(target)65 },66 ownKeys(target) {67 return Reflect.ownKeys(source(target))68 },69 set,70 deleteProperty,71 getOwnPropertyDescriptor,feat: limited class supportaleclarsonbefore adoption9877d64
lines 72–74why, one step back72 defineProperty() {73 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore74 },fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8
lines 75–77why, one step back75 getPrototypeOf(target) {76 return Object.getPrototypeOf(target.base)77 },fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 78why, one step back78 setPrototypeOf() {feat: limited class supportaleclarsonbefore adoption9877d64
line 79why, one step back79 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 80–90why, one step back80 }81}8283const arrayTraps = {}84each(objectTraps, (key, fn) => {85 arrayTraps[key] = function() {86 arguments[0] = arguments[0][0]87 return fn.apply(this, arguments)88 }89})90arrayTraps.deleteProperty = function(state, prop) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 91–93why, one step back91 if (isNaN(parseInt(prop))) {92 throw new Error("Immer only supports deleting array indices") // prettier-ignore93 }fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 94–96why, one step back94 return objectTraps.deleteProperty.call(this, state[0], prop)95}96arrayTraps.set = function(state, prop, value) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 97–99why, one step back97 if (prop !== "length" && isNaN(parseInt(prop))) {98 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore99 }fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 100why, one step back100 return objectTraps.set.call(this, state[0], prop, value)feat: `Map` supportkeenondrumsbefore adoption8276a6c
lines 101–200why, one step back101}102103const mapTraps = {104 get(state, prop, receiver) {105 if (prop === DRAFT_STATE) return state106 if (prop === "size") return source(state)[prop]107 if (prop === "has") {108 const stateCurrent = source(state)109 return stateCurrent[prop].bind(stateCurrent)110 }111 if (prop === "set") {112 return function(key, value) {113 markChanged(state)114 state.assigned[key] = true115 return state.copy.set(key, value)116 }117 }118 if (prop === "delete") {119 return function(key) {120 markChanged(state)121 state.assigned[key] = false122 return state.copy.delete(key)123 }124 }125 if (prop === "clear") {126 return function() {127 markChanged(state)128 state.assigned = {}129 for (const key of source(state).keys()) {130 state.assigned[key] = false131 }132 return state.copy.clear()133 }134 }135 if (prop === "forEach") {136 return function(cb) {137 return source(state).forEach((_, key, map) => {138 const value = receiver.get(key)139 cb(value, key, map)140 }, this)141 }142 }143 if (prop === "get") {144 return function(key) {145 if (!state.modified && has(state.drafts, key)) {146 return state.drafts[key]147 }148 if (state.modified && state.copy.has(key)) {149 return state.copy.get(key)150 }151152 const value = source(state).get(key)153154 if (state.finalized || !isDraftable(value)) {155 return value156 }157158 const valueProxied = createProxy(value, state)159 if (!state.modified) {160 state.drafts[key] = valueProxied161 } else {162 state.copy.set(key, valueProxied)163 }164 return valueProxied165 }166 }167 if (168 prop === Symbol.iterator ||169 prop === "entries" ||170 prop === "values"171 ) {172 let getYieldable = (key, value) => [key, value]173 if (prop === "values") {174 getYieldable = (key, value) => value175 }176 return function*() {177 const iterator = source(state)[Symbol.iterator]()178 let result = iterator.next()179 while (!result.done) {180 const [key] = result.value181 const value = receiver.get(key)182 yield getYieldable(key, value)183 result = iterator.next()184 }185 }186 }187 if (prop === "keys") {188 return function*() {189 const iterator = source(state).keys()190 let result = iterator.next()191 while (!result.done) {192 const key = result.value193 yield key194 result = iterator.next()195 }196 }197 }198199 return Reflect.get(state, prop, receiver)200 }fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 201–202why, one step back201}202Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 203why, one step back203// 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 204why, one step back204function source(state) {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 205why, one step back205 return state.copy || state.basefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 206–207why, one step back206}207fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
lines 208–217why, one step back208// Access a property without creating an Immer draft.209function peek(draft, prop) {210 const state = draft[DRAFT_STATE]211 const desc = Reflect.getOwnPropertyDescriptor(212 state ? source(state) : draft,213 prop214 )215 return desc && desc.value216}217fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 218why, one step back218function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 219why, one step back219 if (prop === DRAFT_STATE) return statefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 220–227why, one step back220 let {drafts} = state221222 // Check for existing draft in unmodified state.223 if (!state.modified && has(drafts, prop)) {224 return drafts[prop]225 }226227 const value = source(state)[prop]chore: comments and formattingAlec Larsonbefore adoptionff96173
lines 228–230why, one step back228 if (state.finalized || !isDraftable(value)) {229 return value230 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 231–232why, one step back231232 // Check for existing draft in modified state.fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 233why, one step back233 if (state.modified) {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 234why, one step back234 // Assigned values are never drafted. This catches any drafts we created, too.fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
line 235why, one step back235 if (value !== peek(state.base, prop)) return valuefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 236–237why, one step back236 // Store drafts on the copy (when one exists).237 drafts = state.copyfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 238why, one step back238 }feat: passing drafts to producealeclarsonbefore adoption9c53415
line 239why, one step back239Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8
line 240why, one step back240 return (drafts[prop] = createProxy(value, state))fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 241–244why, one step back241}242243function set(state, prop, value) {244 if (!state.modified) {fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
line 245why, one step back245 const baseValue = peek(state.base, prop)perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 246–247why, one step back246 // Optimize based on value's truthiness. Truthy values are guaranteed to247 // never be undefined, so we can avoid the `in` operator. Lastly, truthyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 248why, one step back248 // values may be drafts, but falsy values are never drafts.perf: proxy assignaleclarsonbefore adoption53e9ce8
line 249why, one step back249 const isUnchanged = valuefix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
lines 250–251why, one step back250 ? is(baseValue, value) || value === state.drafts[prop]251 : is(baseValue, value) && prop in state.baseperf: proxy assignaleclarsonbefore adoption53e9ce8
line 252why, one step back252 if (isUnchanged) return truefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 253–254why, one step back253 markChanged(state)254 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 255why, one step back255 state.assigned[prop] = truefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 256–260why, one step back256 state.copy[prop] = value257 return true258}259260function deleteProperty(state, prop) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
line 261why, one step back261 // The `undefined` check is a fast path for pre-existing keys.fix: unnecessary draft creationAlec Larsonbefore adoptionec00ee4
line 262why, one step back262 if (peek(state.base, prop) !== undefined || prop in state.base) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
lines 263–266why, one step back263 state.assigned[prop] = false264 markChanged(state)265 }266 if (state.copy) delete state.copy[prop]fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 267–269why, one step back267 return true268}269chore: comments and formattingAlec Larsonbefore adoptionff96173
lines 270–271why, one step back270// Note: We never coerce `desc.value` into an Immer draft, because we can't make271// the same guarantee in ES5 mode.fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 272why, one step back272function getOwnPropertyDescriptor(state, prop) {fix: property descriptor edge casesaleclarsonbefore adoptionafaa737
lines 273–279why, one step back273 const owner = source(state)274 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)275 if (desc) {276 desc.writable = true277 desc.configurable = !Array.isArray(owner) || prop !== "length"278 }279 return descfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 280–284why, one step back280}281282function markChanged(state) {283 if (!state.modified) {284 state.modified = truefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 285–286why, one step back285 state.copy = assign(shallowCopy(state.base), state.drafts)286 state.drafts = nullfix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 287–289why, one step back287 if (state.parent) markChanged(state.parent)288 }289}