Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

168 lines · 4.9 KiB · JavaScript

Last changed by

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

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–2why, one step back
1"use strict"
2import {

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

lines 3–4why, one step back
3 assign,
4 each,

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

line 5why, one step back
5 has,

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

line 6why, one step back
6 is,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 7–8why, one step back
7 isDraftable,
8 isDraft,

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

line 9why, one step back
9 shallowCopy,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 10why, one step back
10 DRAFT_STATE

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

line 11why, one step back
11} from "./common"

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 12why, one step back
12import {ImmerScope} from "./scope"

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

line 13why, one step back
13

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 14–16why, one step back
14// Do nothing before being finalized.
15export function willFinalize() {}16

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 17–18why, one step back
17export function createProxy(base, parent) {
18 const scope = parent ? parent.scope : ImmerScope.current

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 19why, one step back
19 const state = {

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 20why, one step back
20 // Track which produce call this is associated with.

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 21why, one step back
21 scope,

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 22–28why, one step back
22 // 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 back
29 parent,

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 31why, one step back
31 base,

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 40–42why, one step back
40 }
4142 const {revoke, proxy} = Array.isArray(base)

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

lines 43–45why, one step back
43 ? // [state] is used for arrays, to make sure the proxy is array-ish and not violate invariants,
44 // although state itself is an object45 Proxy.revocable([state], arrayTraps)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 46–50why, one step back
46 : Proxy.revocable(state, objectTraps)
4748 state.draft = proxy49 state.revoke = revoke50

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 51why, one step back
51 scope.drafts.push(proxy)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 52–54why, one step back
52 return proxy
53}54

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

lines 55–65why, one step back
55const objectTraps = {
56 get,57 has(target, prop) {58 return prop in source(target)59 },60 ownKeys(target) {61 return Reflect.ownKeys(source(target))62 },63 set,64 deleteProperty,65 getOwnPropertyDescriptor,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 66–68why, one step back
66 defineProperty() {
67 throw new Error("Object.defineProperty() cannot be used on an Immer draft") // prettier-ignore68 },

fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 73why, one step back
73 throw new Error("Object.setPrototypeOf() cannot be used on an Immer draft") // prettier-ignore

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

lines 74–83why, one step back
74 }
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

line 84why, one step back
84arrayTraps.deleteProperty = function(state, prop) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 85–87why, one step back
85 if (isNaN(parseInt(prop))) {
86 throw new Error("Immer only supports deleting array indices") // prettier-ignore87 }

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 88–90why, one step back
88 return objectTraps.deleteProperty.call(this, state[0], prop)
89}90arrayTraps.set = function(state, prop, value) {

feat: limited class supportaleclarsonbefore adoption9877d64

lines 91–93why, one step back
91 if (prop !== "length" && isNaN(parseInt(prop))) {
92 throw new Error("Immer only supports setting array indices and the 'length' property") // prettier-ignore93 }

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 94–95why, one step back
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

line 96why, one step back
96

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 97why, one step back
97// returns the object we should be reading the current value from, which is base, until some change has been made

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

line 98why, one step back
98function source(state) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

lines 100–102why, one step back
100}
101102function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 104–114why, one step back
104 let {drafts} = state
105106 // Check for existing draft in unmodified state.107 if (!state.modified && has(drafts, prop)) {108 return drafts[prop]109 }110111 const value = source(state)[prop]112 if (state.finalized || !isDraftable(value)) return value113114 // Check for existing draft in modified state.

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

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

line 120why, one step back
120 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

line 121why, one step back
121

Merge branch 'async-recipes'Michel Weststratebefore adoptionff210d8

line 122why, one step back
122 return (drafts[prop] = createProxy(value, state))

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

lines 123–126why, one step back
123}
124125function set(state, prop, value) {126 if (!state.modified) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

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

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

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

lines 137–141why, one step back
137 state.copy[prop] = value
138 return true139}140141function deleteProperty(state, prop) {

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

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

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

lines 148–151why, one step back
148 return true
149}150151function getOwnPropertyDescriptor(state, prop) {

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

lines 152–158why, one step back
152 const owner = source(state)
153 const desc = Reflect.getOwnPropertyDescriptor(owner, prop)154 if (desc) {155 desc.writable = true156 desc.configurable = !Array.isArray(owner) || prop !== "length"157 }158 return desc

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

lines 159–163why, one step back
159}
160161function markChanged(state) {162 if (!state.modified) {163 state.modified = true

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

lines 166–168why, one step back
166 if (state.parent) markChanged(state.parent)
167 }168}