Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

175 lines · 4.8 KiB · JavaScript

Last changed by

feat: passing drafts to producealeclarsonbefore adoption9c53415

History of this file

Take 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 back
1"use strict"
2// @ts-check34import {

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

lines 5–6why, one step back
5 assign,
6 each,

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 7why, one step back
7 has,

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

line 8why, one step back
8 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 9–10why, one step back
9 isDraftable,
10 isDraft,

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 11why, one step back
11 shallowCopy,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 12why, one step back
12 DRAFT_STATE

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 13–14why, one step back
13} from "./common"
14

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 15–17why, one step back
15// 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 back
18

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 19–23why, one step back
19// 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 back
24 // 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 back
33 parent,

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 34why, one step back
34 // The base state.

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 35why, one step back
35 base,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 36–40why, one step back
36 // 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 back
41 copy: null,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 42–43why, one step back
42 // Called by the `produce` function.
43 revoke: null

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 44–52why, one step back
44 }
4546 const {revoke, proxy} = Array.isArray(base)47 ? Proxy.revocable([state], arrayTraps)48 : Proxy.revocable(state, objectTraps)4950 state.draft = proxy51 state.revoke = revoke52

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 53why, one step back
53 state.scope.push(state)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 54–56why, one step back
54 return proxy
55}56

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 57–69why, one step back
57const 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,69 setPrototypeOf() {

Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb

line 70why, one step back
70 throw new Error("Immer does not support `setPrototypeOf()`.")

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 71–80why, one step back
71 }
72}7374const arrayTraps = {}75each(objectTraps, (key, fn) => {76 arrayTraps[key] = function() {77 arguments[0] = arguments[0][0]78 return fn.apply(this, arguments)79 }80})

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 81–95why, one step back
81arrayTraps.deleteProperty = function(state, prop) {
82 if (isNaN(parseInt(prop)))83 throw new Error(84 "Immer does not support deleting properties from arrays: " + prop85 )86 return objectTraps.deleteProperty.call(this, state[0], prop)87}88arrayTraps.set = function(state, prop, value) {89 if (prop !== "length" && isNaN(parseInt(prop)))90 throw new Error(91 "Immer does not support setting non-numeric properties on arrays: " +92 prop93 )94 return objectTraps.set.call(this, state[0], prop, value)95}

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 96–97why, one step back
96
97function source(state) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 98why, one step back
98 return state.copy || state.base

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 99–101why, one step back
99}
100101function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 102why, one step back
102 if (prop === DRAFT_STATE) return state

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 103–113why, one step back
103 let {drafts} = state
104105 // Check for existing draft in unmodified state.106 if (!state.modified && has(drafts, prop)) {107 return drafts[prop]108 }109110 const value = source(state)[prop]111 if (state.finalized || !isDraftable(value)) return value112113 // Check for existing draft in modified state.

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 114why, one step back
114 if (state.modified) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 115–118why, one step back
115 // Assigned values are never drafted. This catches any drafts we created, too.
116 if (value !== state.base[prop]) return value117 // Store drafts on the copy (when one exists).118 drafts = state.copy

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 119why, one step back
119 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 120–121why, one step back
120
121 return (drafts[prop] = createDraft(value, state))

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 122–125why, one step back
122}
123124function set(state, prop, value) {125 if (!state.modified) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 126–127why, one step back
126 // Optimize based on value's truthiness. Truthy values are guaranteed to
127 // never be undefined, so we can avoid the `in` operator. Lastly, truthy

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 128why, one step back
128 // values may be drafts, but falsy values are never drafts.

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 129why, one step back
129 const isUnchanged = value

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 130why, one step back
130 ? is(state.base[prop], value) || value === state.drafts[prop]

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 131–132why, one step back
131 : is(state.base[prop], value) && prop in state.base
132 if (isUnchanged) return true

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 133–134why, one step back
133 markChanged(state)
134 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 135why, one step back
135 state.assigned[prop] = true

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 136–140why, one step back
136 state.copy[prop] = value
137 return true138}139140function deleteProperty(state, prop) {

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

lines 141–146why, one step back
141 // The `undefined` check is a fast path for pre-existing keys.
142 if (state.base[prop] !== undefined || prop in state.base) {143 state.assigned[prop] = false144 markChanged(state)145 }146 if (state.copy) delete state.copy[prop]

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 147–152why, one step back
147 return true
148}149150function getOwnPropertyDescriptor(state, prop) {151 const owner = state.modified152 ? state.copy

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 153–154why, one step back
153 : has(state.drafts, prop)
154 ? state.drafts

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

line 155why, one step back
155 : state.base

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 156–163why, one step back
156 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)
157 if (descriptor && !(Array.isArray(owner) && prop === "length"))158 descriptor.configurable = true159 return descriptor160}161162function defineProperty() {163 throw new Error(

Merge pull request #144 from btnwtn/patch-1Michel Weststratebefore adoptionaaa12eb

line 164why, one step back
164 "Immer does not support defining properties on draft objects."

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 165–170why, one step back
165 )
166}167168function markChanged(state) {169 if (!state.modified) {170 state.modified = true

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 171–172why, one step back
171 state.copy = assign(shallowCopy(state.base), state.drafts)
172 state.drafts = null

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 173–175why, one step back
173 if (state.parent) markChanged(state.parent)
174 }175}