proxy.js
This file
Path
src/proxy.js
162 lines · 4.7 KiB · JavaScript
Last changed by
feat: add `Immer` classaleclarsonbefore adoptionecd04dc
History of this fileTake it
The walk stopped after 200 commits: lines older than 3a9d6d1 point at it.
Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
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,Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
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,Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 11why, one step back11 shallowCopy,feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 12why, one step back12 DRAFT_STATEImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
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]Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 18why, one step back18feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 19–47why, one step back19// Do nothing before being finalized.20export function willFinalize() {}2122export function createDraft(base, parent) {23 if (isDraft(base)) throw new Error("This should never happen. Please report: https://github.com/mweststrate/immer/issues/new") // prettier-ignore2425 const state = {26 modified: false, // this tree is modified (either this object or one of it's children)27 assigned: {}, // true: value was assigned to these props, false: was removed28 parent,29 base,30 draft: null, // the root proxy31 drafts: {}, // proxied properties32 copy: null,33 revoke: null,34 finalized: false35 }3637 const {revoke, proxy} = Array.isArray(base)38 ? Proxy.revocable([state], arrayTraps)39 : Proxy.revocable(state, objectTraps)4041 state.draft = proxy42 state.revoke = revoke4344 currentScope().push(state)45 return proxy46}47Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 48–60why, one step back48const objectTraps = {49 get,50 has(target, prop) {51 return prop in source(target)52 },53 ownKeys(target) {54 return Reflect.ownKeys(source(target))55 },56 set,57 deleteProperty,58 getOwnPropertyDescriptor,59 defineProperty,60 setPrototypeOf() {Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 61why, one step back61 throw new Error("Immer does not support `setPrototypeOf()`.")Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 62–71why, one step back62 }63}6465const arrayTraps = {}66each(objectTraps, (key, fn) => {67 arrayTraps[key] = function() {68 arguments[0] = arguments[0][0]69 return fn.apply(this, arguments)70 }71})Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c
lines 72–86why, one step back72arrayTraps.deleteProperty = function(state, prop) {73 if (isNaN(parseInt(prop)))74 throw new Error(75 "Immer does not support deleting properties from arrays: " + prop76 )77 return objectTraps.deleteProperty.call(this, state[0], prop)78}79arrayTraps.set = function(state, prop, value) {80 if (prop !== "length" && isNaN(parseInt(prop)))81 throw new Error(82 "Immer does not support setting non-numeric properties on arrays: " +83 prop84 )85 return objectTraps.set.call(this, state[0], prop, value)86}Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 87–92why, one step back8788function source(state) {89 return state.modified === true ? state.copy : state.base90}9192function get(state, prop) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 93why, one step back93 if (prop === DRAFT_STATE) return stateImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 94–95why, one step back94 if (state.modified) {95 const value = state.copy[prop]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 96why, one step back96 if (value === state.base[prop] && isDraftable(value))Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 97–98why, one step back97 // only create proxy if it is not yet a proxy, and not a new object98 // (new objects don't need proxying, they will be processed in finalize anyway)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 99why, one step back99 return (state.copy[prop] = createDraft(value, state))Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 100–101why, one step back100 return value101 } else {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 102why, one step back102 if (has(state.drafts, prop)) return state.drafts[prop]Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
line 103why, one step back103 const value = state.base[prop]feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 104–105why, one step back104 if (!isDraft(value) && isDraftable(value))105 return (state.drafts[prop] = createDraft(value, state))Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 106–111why, one step back106 return value107 }108}109110function set(state, prop, value) {111 if (!state.modified) {perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 112–113why, one step back112 // Optimize based on value's truthiness. Truthy values are guaranteed to113 // never be undefined, so we can avoid the `in` operator. Lastly, truthyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 114why, one step back114 // values may be drafts, but falsy values are never drafts.perf: proxy assignaleclarsonbefore adoption53e9ce8
line 115why, one step back115 const isUnchanged = valuefeat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 116why, one step back116 ? is(state.base[prop], value) || value === state.drafts[prop]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 117–118why, one step back117 : is(state.base[prop], value) && prop in state.base118 if (isUnchanged) return trueImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 119–120why, one step back119 markChanged(state)120 }perf: proxy assignaleclarsonbefore adoption53e9ce8
line 121why, one step back121 state.assigned[prop] = trueImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 122–126why, one step back122 state.copy[prop] = value123 return true124}125126function deleteProperty(state, prop) {perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
lines 127–132why, one step back127 // The `undefined` check is a fast path for pre-existing keys.128 if (state.base[prop] !== undefined || prop in state.base) {129 state.assigned[prop] = false130 markChanged(state)131 }132 if (state.copy) delete state.copy[prop]Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 133–138why, one step back133 return true134}135136function getOwnPropertyDescriptor(state, prop) {137 const owner = state.modified138 ? state.copyfeat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 139–140why, one step back139 : has(state.drafts, prop)140 ? state.draftsperf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252
line 141why, one step back141 : state.baseImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 142–149why, one step back142 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)143 if (descriptor && !(Array.isArray(owner) && prop === "length"))144 descriptor.configurable = true145 return descriptor146}147148function defineProperty() {149 throw new Error(Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb
line 150why, one step back150 "Immer does not support defining properties on draft objects."Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 151–157why, one step back151 )152}153154function markChanged(state) {155 if (!state.modified) {156 state.modified = true157 state.copy = shallowCopy(state.base)feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 158–159why, one step back158 // copy the drafts over the base-copy159 assign(state.copy, state.drafts) // yup that works for arrays as wellImmer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1
lines 160–162why, one step back160 if (state.parent) markChanged(state.parent)161 }162}