Skip to content
Myr

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 file

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

fix: always use `assign` polyfillaleclarsonbefore adoption241a648

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

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

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,

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

line 11why, one step back
11 shallowCopy,

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 12why, one step back
12 DRAFT_STATE

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

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]

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

line 18why, one step back
18

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 19–47why, one step back
19// 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}47

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

lines 48–60why, one step back
48const 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 back
61 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 back
62 }
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 back
72arrayTraps.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 back
87
88function source(state) {89 return state.modified === true ? state.copy : state.base90}9192function get(state, prop) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

lines 94–95why, one step back
94 if (state.modified) {
95 const value = state.copy[prop]

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 96why, one step back
96 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 back
97 // only create proxy if it is not yet a proxy, and not a new object
98 // (new objects don't need proxying, they will be processed in finalize anyway)

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 99why, one step back
99 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 back
100 return value
101 } else {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 102why, one step back
102 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 back
103 const value = state.base[prop]

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 104–105why, one step back
104 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 back
106 return value
107 }108}109110function set(state, prop, value) {111 if (!state.modified) {

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

lines 119–120why, one step back
119 markChanged(state)
120 }

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

lines 122–126why, one step back
122 state.copy[prop] = value
123 return true124}125126function deleteProperty(state, prop) {

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

lines 127–132why, one step back
127 // 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 back
133 return true
134}135136function getOwnPropertyDescriptor(state, prop) {137 const owner = state.modified138 ? state.copy

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

perf(proxy): deletion of an undeclared propertyaleclarsonbefore adoptionc0ce252

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

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

lines 142–149why, one step back
142 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 back
150 "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 back
151 )
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 back
158 // copy the drafts over the base-copy
159 assign(state.copy, state.drafts) // yup that works for arrays as well

Immer now changes the context (`this`) of producer functions by default to the draft stateMichel Weststratebefore adoption3a9d6d1

lines 160–162why, one step back
160 if (state.parent) markChanged(state.parent)
161 }162}