proxy.js
This file
Path
src/proxy.js
169 lines · 4.7 KiB · JavaScript
Last changed by
fix: property descriptor edge casesaleclarsonbefore adoptionafaa737
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–4why, one step back1"use strict"2// @ts-check34import {fix: always use `assign` polyfillaleclarsonbefore adoption241a648
lines 5–6why, one step back5 assign,6 each,Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 7why, one step back7 has,fix: always use `assign` polyfillaleclarsonbefore adoption241a648
line 8why, one step back8 is,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 9–10why, one step back9 isDraftable,10 isDraft,Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 11why, one step back11 shallowCopy,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 12why, one step back12 DRAFT_STATEMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 13–14why, one step back13} from "./common"14feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 15–17why, one step back15// For nested produce calls:16export const scopes = []17export const currentScope = () => scopes[scopes.length - 1]Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 18why, one step back18feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 19–23why, one step back19// Do nothing before being finalized.20export function willFinalize() {}2122export function createDraft(base, parent) {23 const state = {feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 24–32why, one step back24 // Track which produce call this is associated with.25 scope: parent ? parent.scope : currentScope(),26 // True for both shallow and deep changes.27 modified: false,28 // Used during finalization.29 finalized: false,30 // Track which properties have been assigned (true) or deleted (false).31 assigned: {},32 // The parent draft state.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 33why, one step back33 parent,feat: passing drafts to producealeclarsonbefore adoption9c53415
line 34why, one step back34 // The base state.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 35why, one step back35 base,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 36–40why, one step back36 // The base proxy.37 draft: null,38 // Any property proxies.39 drafts: {},40 // The base copy with any updated values.feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 41why, one step back41 copy: null,feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 42–43why, one step back42 // Called by the `produce` function.43 revoke: nullfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 44–52why, one step back44 }4546 const {revoke, proxy} = Array.isArray(base)47 ? Proxy.revocable([state], arrayTraps)48 : Proxy.revocable(state, objectTraps)4950 state.draft = proxy51 state.revoke = revoke52feat: passing drafts to producealeclarsonbefore adoption9c53415
line 53why, one step back53 state.scope.push(state)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 54–56why, one step back54 return proxy55}56Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 57–67why, one step back57const objectTraps = {58 get,59 has(target, prop) {60 return prop in source(target)61 },62 ownKeys(target) {63 return Reflect.ownKeys(source(target))64 },65 set,66 deleteProperty,67 getOwnPropertyDescriptor,feat: limited class supportaleclarsonbefore adoption9877d64
lines 68–70why, one step back68 defineProperty() {69 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore70 },fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8
lines 71–73why, one step back71 getPrototypeOf(target) {72 return Object.getPrototypeOf(target.base)73 },Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 74why, one step back74 setPrototypeOf() {feat: limited class supportaleclarsonbefore adoption9877d64
line 75why, one step back75 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 76–85why, one step back76 }77}7879const arrayTraps = {}80each(objectTraps, (key, fn) => {81 arrayTraps[key] = function() {82 arguments[0] = arguments[0][0]83 return fn.apply(this, arguments)84 }85})Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
line 86why, one step back86arrayTraps.deleteProperty = function(state, prop) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 87–89why, one step back87 if (isNaN(parseInt(prop))) {88 throw new Error("Immer only supports deleting array indices") // prettier-ignore89 }Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 90–92why, one step back90 return objectTraps.deleteProperty.call(this, state[0], prop)91}92arrayTraps.set = function(state, prop, value) {feat: limited class supportaleclarsonbefore adoption9877d64
lines 93–95why, one step back93 if (prop !== "length" && isNaN(parseInt(prop))) {94 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore95 }Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 96–97why, one step back96 return objectTraps.set.call(this, state[0], prop, value)97}Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 98–99why, one step back9899function source(state) {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 100why, one step back100 return state.copy || state.baseMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 101–103why, one step back101}102103function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 104why, one step back104 if (prop === DRAFT_STATE) return statefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 105–115why, one step back105 let {drafts} = state106107 // Check for existing draft in unmodified state.108 if (!state.modified && has(drafts, prop)) {109 return drafts[prop]110 }111112 const value = source(state)[prop]113 if (state.finalized || !isDraftable(value)) return value114115 // Check for existing draft in modified state.Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 116why, one step back116 if (state.modified) {feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 117–120why, one step back117 // Assigned values are never drafted. This catches any drafts we created, too.118 if (value !== state.base[prop]) return value119 // Store drafts on the copy (when one exists).120 drafts = state.copyMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 121why, one step back121 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 122–123why, one step back122123 return (drafts[prop] = createDraft(value, state))Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 124–127why, one step back124}125126function set(state, prop, value) {127 if (!state.modified) {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 128–129why, one step back128 // Optimize based on value's truthiness. Truthy values are guaranteed to129 // never be undefined, so we can avoid the `in` operator. Lastly, truthyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 130why, one step back130 // values may be drafts, but falsy values are never drafts.perf: proxy assignaleclarsonbefore adoption53e9ce8
line 131why, one step back131 const isUnchanged = valuefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 132why, one step back132 ? is(state.base[prop], value) || value === state.drafts[prop]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 133–134why, one step back133 : is(state.base[prop], value) && prop in state.base134 if (isUnchanged) return trueMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 135–136why, one step back135 markChanged(state)136 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 137why, one step back137 state.assigned[prop] = trueMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 138–142why, one step back138 state.copy[prop] = value139 return true140}141142function deleteProperty(state, prop) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
lines 143–148why, one step back143 // The `undefined` check is a fast path for pre-existing keys.144 if (state.base[prop] !== undefined || prop in state.base) {145 state.assigned[prop] = false146 markChanged(state)147 }148 if (state.copy) delete state.copy[prop]Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 149–152why, one step back149 return true150}151152function getOwnPropertyDescriptor(state, prop) {fix: property descriptor edge casesaleclarsonbefore adoptionafaa737
lines 153–159why, one step back153 const owner = source(state)154 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)155 if (desc) {156 desc.writable = true157 desc.configurable = !Array.isArray(owner) || prop !== "length"158 }159 return descMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 160–164why, one step back160}161162function markChanged(state) {163 if (!state.modified) {164 state.modified = truefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 165–166why, one step back165 state.copy = assign(shallowCopy(state.base), state.drafts)166 state.drafts = nullMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 167–169why, one step back167 if (state.parent) markChanged(state.parent)168 }169}