proxy.js
This file
Path
src/proxy.js
165 lines · 4.7 KiB · JavaScript
Last changed by
feat: support async recipesaleclarsonbefore adoption5fee518
History of this fileTake it
The walk stopped after 200 commits: lines older than 2cb5496 point at it.
Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 1–2why, one step back1"use strict"2import {fix: always use `assign` polyfillaleclarsonbefore adoption241a648
lines 3–4why, one step back3 assign,4 each,Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
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,Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 9why, one step back9 shallowCopy,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 10why, one step back10 DRAFT_STATEMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 11why, one step back11} from "./common"feat: support async recipesaleclarsonbefore adoption5fee518
line 12why, one step back12import {ImmerScope} from "./scope"Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 13why, one step back13feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 14–17why, one step back14// Do nothing before being finalized.15export function willFinalize() {}1617export function createDraft(base, parent) {feat: support async recipesaleclarsonbefore adoption5fee518
line 18why, one step back18 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.feat: support async recipesaleclarsonbefore adoption5fee518
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–48why, one step back40 }4142 const {revoke, proxy} = Array.isArray(base)43 ? Proxy.revocable([state], arrayTraps)44 : Proxy.revocable(state, objectTraps)4546 state.draft = proxy47 state.revoke = revoke48feat: support async recipesaleclarsonbefore adoption5fee518
line 49why, one step back49 scope.drafts.push(proxy)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 50–52why, one step back50 return proxy51}52Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 53–63why, one step back53const objectTraps = {54 get,55 has(target, prop) {56 return prop in source(target)57 },58 ownKeys(target) {59 return Reflect.ownKeys(source(target))60 },61 set,62 deleteProperty,63 getOwnPropertyDescriptor,feat: limited class supportaleclarsonbefore adoption9877d64
lines 64–66why, one step back64 defineProperty() {65 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore66 },fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8
lines 67–69why, one step back67 getPrototypeOf(target) {68 return Object.getPrototypeOf(target.base)69 },Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 70why, one step back70 setPrototypeOf() {feat: limited class supportaleclarsonbefore adoption9877d64
line 71why, one step back71 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignoreMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 72–81why, one step back72 }73}7475const arrayTraps = {}76each(objectTraps, (key, fn) => {77 arrayTraps[key] = function() {78 arguments[0] = arguments[0][0]79 return fn.apply(this, arguments)80 }81})Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
line 82why, one step back82arrayTraps.deleteProperty = function(state, prop) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 83–85why, one step back83 if (isNaN(parseInt(prop))) {84 throw new Error("Immer only supports deleting array indices") // prettier-ignore85 }Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 86–88why, one step back86 return objectTraps.deleteProperty.call(this, state[0], prop)87}88arrayTraps.set = function(state, prop, value) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 89–91why, one step back89 if (prop !== "length" && isNaN(parseInt(prop))) {90 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore91 }Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 92–93why, one step back92 return objectTraps.set.call(this, state[0], prop, value)93}Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 94–95why, one step back9495function source(state) {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 96why, one step back96 return state.copy || state.baseMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 97–99why, one step back97}9899function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 100why, one step back100 if (prop === DRAFT_STATE) return statefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 101–111why, one step back101 let {drafts} = state102103 // Check for existing draft in unmodified state.104 if (!state.modified && has(drafts, prop)) {105 return drafts[prop]106 }107108 const value = source(state)[prop]109 if (state.finalized || !isDraftable(value)) return value110111 // Check for existing draft in modified state.Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 112why, one step back112 if (state.modified) {feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 113–116why, one step back113 // Assigned values are never drafted. This catches any drafts we created, too.114 if (value !== state.base[prop]) return value115 // Store drafts on the copy (when one exists).116 drafts = state.copyMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 117why, one step back117 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 118–119why, one step back118119 return (drafts[prop] = createDraft(value, state))Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 120–123why, one step back120}121122function set(state, prop, value) {123 if (!state.modified) {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 124–125why, one step back124 // Optimize based on value's truthiness. Truthy values are guaranteed to125 // never be undefined, so we can avoid the `in` operator. Lastly, truthyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 126why, one step back126 // values may be drafts, but falsy values are never drafts.perf: proxy assignaleclarsonbefore adoption53e9ce8
line 127why, one step back127 const isUnchanged = valuefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 128why, one step back128 ? is(state.base[prop], value) || value === state.drafts[prop]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 129–130why, one step back129 : is(state.base[prop], value) && prop in state.base130 if (isUnchanged) return trueMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 131–132why, one step back131 markChanged(state)132 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 133why, one step back133 state.assigned[prop] = trueMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 134–138why, one step back134 state.copy[prop] = value135 return true136}137138function deleteProperty(state, prop) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
lines 139–144why, one step back139 // The `undefined` check is a fast path for pre-existing keys.140 if (state.base[prop] !== undefined || prop in state.base) {141 state.assigned[prop] = false142 markChanged(state)143 }144 if (state.copy) delete state.copy[prop]Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 145–148why, one step back145 return true146}147148function getOwnPropertyDescriptor(state, prop) {fix: property descriptor edge casesaleclarsonbefore adoptionafaa737
lines 149–155why, one step back149 const owner = source(state)150 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)151 if (desc) {152 desc.writable = true153 desc.configurable = !Array.isArray(owner) || prop !== "length"154 }155 return descMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 156–160why, one step back156}157158function markChanged(state) {159 if (!state.modified) {160 state.modified = truefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 161–162why, one step back161 state.copy = assign(shallowCopy(state.base), state.drafts)162 state.drafts = nullMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 163–165why, one step back163 if (state.parent) markChanged(state.parent)164 }165}