proxy.js
This file
Path
src/proxy.js
178 lines · 4.8 KiB · JavaScript
Last changed by
fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8
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–68why, 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,68 defineProperty,fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8
lines 69–71why, one step back69 getPrototypeOf(target) {70 return Object.getPrototypeOf(target.base)71 },Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 72why, one step back72 setPrototypeOf() {Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 73why, one step back73 throw new Error("Immer does not support `setPrototypeOf()`.")Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 74–83why, 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})Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 84–98why, one step back84arrayTraps.deleteProperty = function(state, prop) {85 if (isNaN(parseInt(prop)))86 throw new Error(87 "Immer does not support deleting properties from arrays: " + prop88 )89 return objectTraps.deleteProperty.call(this, state[0], prop)90}91arrayTraps.set = function(state, prop, value) {92 if (prop !== "length" && isNaN(parseInt(prop)))93 throw new Error(94 "Immer does not support setting non-numeric properties on arrays: " +95 prop96 )97 return objectTraps.set.call(this, state[0], prop, value)98}Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 99–100why, one step back99100function source(state) {feat: passing drafts to producealeclarsonbefore adoption9c53415
line 101why, one step back101 return state.copy || state.baseMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 102–104why, one step back102}103104function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 105why, one step back105 if (prop === DRAFT_STATE) return statefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 106–116why, one step back106 let {drafts} = state107108 // Check for existing draft in unmodified state.109 if (!state.modified && has(drafts, prop)) {110 return drafts[prop]111 }112113 const value = source(state)[prop]114 if (state.finalized || !isDraftable(value)) return value115116 // Check for existing draft in modified state.Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 117why, one step back117 if (state.modified) {feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 118–121why, one step back118 // Assigned values are never drafted. This catches any drafts we created, too.119 if (value !== state.base[prop]) return value120 // Store drafts on the copy (when one exists).121 drafts = state.copyMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
line 122why, one step back122 }feat: passing drafts to producealeclarsonbefore adoption9c53415
lines 123–124why, one step back123124 return (drafts[prop] = createDraft(value, state))Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 125–128why, one step back125}126127function set(state, prop, value) {128 if (!state.modified) {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 129–130why, one step back129 // Optimize based on value's truthiness. Truthy values are guaranteed to130 // never be undefined, so we can avoid the `in` operator. Lastly, truthyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 131why, one step back131 // values may be drafts, but falsy values are never drafts.perf: proxy assignaleclarsonbefore adoption53e9ce8
line 132why, one step back132 const isUnchanged = valuefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 133why, one step back133 ? is(state.base[prop], value) || value === state.drafts[prop]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 134–135why, one step back134 : is(state.base[prop], value) && prop in state.base135 if (isUnchanged) return trueMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 136–137why, one step back136 markChanged(state)137 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 138why, one step back138 state.assigned[prop] = trueMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 139–143why, one step back139 state.copy[prop] = value140 return true141}142143function deleteProperty(state, prop) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
lines 144–149why, one step back144 // The `undefined` check is a fast path for pre-existing keys.145 if (state.base[prop] !== undefined || prop in state.base) {146 state.assigned[prop] = false147 markChanged(state)148 }149 if (state.copy) delete state.copy[prop]Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 150–155why, one step back150 return true151}152153function getOwnPropertyDescriptor(state, prop) {154 const owner = state.modified155 ? state.copyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 156–157why, one step back156 : has(state.drafts, prop)157 ? state.draftsperf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
line 158why, one step back158 : state.baseMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 159–166why, one step back159 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)160 if (descriptor && !(Array.isArray(owner) && prop === "length"))161 descriptor.configurable = true162 return descriptor163}164165function defineProperty() {166 throw new Error(Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 167why, one step back167 "Immer does not support defining properties on draft objects."Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 168–173why, one step back168 )169}170171function markChanged(state) {172 if (!state.modified) {173 state.modified = truefeat: passing drafts to producealeclarsonbefore adoption9c53415
lines 174–175why, one step back174 state.copy = assign(shallowCopy(state.base), state.drafts)175 state.drafts = nullMerge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496
lines 176–178why, one step back176 if (state.parent) markChanged(state.parent)177 }178}