Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

165 lines · 4.7 KiB · JavaScript

Last changed by

feat: support async recipesaleclarsonbefore adoption5fee518

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"

feat: support async recipesaleclarsonbefore adoption5fee518

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–17why, one step back
14// Do nothing before being finalized.
15export function willFinalize() {}1617export function createDraft(base, parent) {

feat: support async recipesaleclarsonbefore adoption5fee518

line 18why, one step back
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.

feat: support async recipesaleclarsonbefore adoption5fee518

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–48why, one step back
40 }
4142 const {revoke, proxy} = Array.isArray(base)43 ? Proxy.revocable([state], arrayTraps)44 : Proxy.revocable(state, objectTraps)4546 state.draft = proxy47 state.revoke = revoke48

feat: support async recipesaleclarsonbefore adoption5fee518

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 50–52why, one step back
50 return proxy
51}52

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

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

fix: Object.getPrototypeOf with a draftaleclarsonbefore adoptiond5d07e8

lines 67–69why, one step back
67 getPrototypeOf(target) {
68 return Object.getPrototypeOf(target.base)69 },

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

line 70why, one step back
70 setPrototypeOf() {

feat: limited class supportaleclarsonbefore adoption9877d64

line 71why, one step back
71 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 72–81why, one step back
72 }
73}7475const arrayTraps = {}76each(objectTraps, (key, fn) => {77 arrayTraps[key] = function() {78 arguments[0] = arguments[0][0]79 return fn.apply(this, arguments)80 }81})

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

lines 92–93why, one step back
92 return objectTraps.set.call(this, state[0], prop, value)
93}

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

lines 94–95why, one step back
94
95function source(state) {

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

lines 101–111why, one step back
101 let {drafts} = state
102103 // Check for existing draft in unmodified state.104 if (!state.modified && has(drafts, prop)) {105 return drafts[prop]106 }107108 const value = source(state)[prop]109 if (state.finalized || !isDraftable(value)) return value110111 // Check for existing draft in modified state.

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

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

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

line 117why, one step back
117 }

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

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

lines 131–132why, one step back
131 markChanged(state)
132 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

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

lines 134–138why, one step back
134 state.copy[prop] = value
135 return true136}137138function deleteProperty(state, prop) {

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

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

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

lines 145–148why, one step back
145 return true
146}147148function getOwnPropertyDescriptor(state, prop) {

fix: property descriptor edge casesaleclarsonbefore adoptionafaa737

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

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

lines 156–160why, one step back
156}
157158function markChanged(state) {159 if (!state.modified) {160 state.modified = true

feat: passing drafts to producealeclarsonbefore adoption9c53415

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

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

lines 163–165why, one step back
163 if (state.parent) markChanged(state.parent)
164 }165}