proxy.js
This file
Path
src/proxy.js
289 lines · 8.5 KiB · JavaScript
Last changed by
feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
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` drafts and patcheskeenondrumsbefore adoption30db0f3
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` drafts and patcheskeenondrumsbefore adoption30db0f3
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` drafts and patcheskeenondrumsbefore adoption30db0f3
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
line 61why, one step back61const objectTraps = {fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 62–64why, one step back62 get,63 has(target, prop) {64 return prop in source(target)chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc
line 65why, one step back65 },fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 66–67why, one step back66 ownKeys(target) {67 return Reflect.ownKeys(source(target))fix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
line 68why, one step back68 },fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 69–71why, one step back69 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: revert Map supportAlec Larsonbefore adoption47529d3
lines 75–76why, one step back75 getPrototypeOf(target) {76 return Object.getPrototypeOf(target.base)fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8
line 77why, one step back77 },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` drafts and patcheskeenondrumsbefore adoption30db0f3
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 }feat: `Map` supportkeenondrumsbefore adoption8276a6c
lines 201–202why, one step back201}202fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 203–206why, one step back203// returns the object we should be reading the current value from, which is base, until some change has been made204function source(state) {205 return state.copy || state.base206}chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc
line 207why, one step back207fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 208–215why, 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.valuechore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc
lines 216–217why, one step back216}217fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 218–220why, one step back218function get(state, prop) {219 if (prop === DRAFT_STATE) return state220 let {drafts} = statechore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc
line 221why, one step back221fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 222–225why, one step back222 // Check for existing draft in unmodified state.223 if (!state.modified && has(drafts, prop)) {224 return drafts[prop]225 }chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc
line 226why, one step back226fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 227–230why, one step back227 const value = source(state)[prop]228 if (state.finalized || !isDraftable(value)) {229 return value230 }feat: `Map` supportkeenondrumsbefore adoption8276a6c
line 231why, one step back231fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 232–238why, one step back232 // Check for existing draft in modified state.233 if (state.modified) {234 // Assigned values are never drafted. This catches any drafts we created, too.235 if (value !== peek(state.base, prop)) return value236 // Store drafts on the copy (when one exists).237 drafts = state.copy238 }feat: `Map` supportkeenondrumsbefore adoption8276a6c
line 239why, one step back239fix: revert Map supportAlec Larsonbefore adoption47529d3
line 240why, one step back240 return (drafts[prop] = createProxy(value, state))chore: reorganize src/proxy.jsAlec Larsonbefore adoptione809ffc
line 241why, one step back241}feat: `Map` supportkeenondrumsbefore adoption8276a6c
line 242why, one step back242fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 243–253why, one step back243function set(state, prop, value) {244 if (!state.modified) {245 const baseValue = peek(state.base, prop)246 // Optimize based on value's truthiness. Truthy values are guaranteed to247 // never be undefined, so we can avoid the `in` operator. Lastly, truthy248 // values may be drafts, but falsy values are never drafts.249 const isUnchanged = value250 ? is(baseValue, value) || value === state.drafts[prop]251 : is(baseValue, value) && prop in state.base252 if (isUnchanged) return true253 markChanged(state)feat: `Map` supportkeenondrumsbefore adoption8276a6c
line 254why, one step back254 }fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 255–257why, one step back255 state.assigned[prop] = true256 state.copy[prop] = value257 return truefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 258–259why, one step back258}259fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 260–267why, one step back260function deleteProperty(state, prop) {261 // The `undefined` check is a fast path for pre-existing keys.262 if (peek(state.base, prop) !== undefined || prop in state.base) {263 state.assigned[prop] = false264 markChanged(state)265 }266 if (state.copy) delete state.copy[prop]267 return truefix: use proxies only if Reflect exists (#227)Alec Larsonbefore adoption1f93af2
lines 268–269why, one step back268}269fix: revert Map supportAlec Larsonbefore adoption47529d3
lines 270–279why, one step back270// Note: We never coerce `desc.value` into an Immer draft, because we can't make271// the same guarantee in ES5 mode.272function getOwnPropertyDescriptor(state, prop) {273 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}