Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

148 lines · 3.8 KiB · JavaScript

Last changed by

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

History of this file

Take it

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 1–3why, one step back
1"use strict"
2// @ts-check3

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

line 4why, one step back
4import {

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

line 5why, one step back
5 is,

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 6why, one step back
6 has,

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

lines 7–10why, one step back
7 isProxyable,
8 isProxy,9 freeze,10 PROXY_STATE,

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 11–14why, one step back
11 finalize,
12 shallowCopy,13 verifyReturnValue,14 each

Fixed #66: Incorrectly trying to modify a frozen objectMichel Weststratebefore adoption0834d8a

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

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 16why, one step back
16

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 17why, one step back
17let proxies = null

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 18–36why, one step back
18
19const objectTraps = {20 get,21 has(target, prop) {22 return prop in source(target)23 },24 ownKeys(target) {25 return Reflect.ownKeys(source(target))26 },27 set,28 deleteProperty,29 getOwnPropertyDescriptor,30 defineProperty,31 setPrototypeOf() {32 throw new Error("Don't even try this...")33 }34}3536const arrayTraps = {}

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 37why, one step back
37each(objectTraps, (key, fn) => {

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 38–39why, one step back
38 arrayTraps[key] = function() {
39 arguments[0] = arguments[0][0]

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 40why, one step back
40 return fn.apply(this, arguments)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 41why, one step back
41 }

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 42why, one step back
42})

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 43–62why, one step back
43
44function createState(parent, base) {45 return {46 modified: false,47 finalized: false,48 parent,49 base,50 copy: undefined,51 proxies: {}52 }53}5455function source(state) {56 return state.modified === true ? state.copy : state.base57}5859function get(state, prop) {60 if (prop === PROXY_STATE) return state61 if (state.modified) {62 const value = state.copy[prop]

Merge branch 'iruca3-fix/finalize'Michel Weststratebefore adoption425f092

lines 63–65why, one step back
63 if (value === state.base[prop] && isProxyable(value))
64 // only create proxy if it is not yet a proxy, and not a new object65 // (new objects don't need proxying, they will be processed in finalize anyway)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 66–68why, one step back
66 return (state.copy[prop] = createProxy(state, value))
67 return value68 } else {

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 69why, one step back
69 if (has(state.proxies, prop)) return state.proxies[prop]

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 70–79why, one step back
70 const value = state.base[prop]
71 if (!isProxy(value) && isProxyable(value))72 return (state.proxies[prop] = createProxy(state, value))73 return value74 }75}7677function set(state, prop, value) {78 if (!state.modified) {79 if (

Added polyfill for Object.is, fixes #77Michel Weststratebefore adoptioncf0b1e1

line 80why, one step back
80 (prop in state.base && is(state.base[prop], value)) ||

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 81why, one step back
81 (has(state.proxies, prop) && state.proxies[prop] === value)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 82–98why, one step back
82 )
83 return true84 markChanged(state)85 }86 state.copy[prop] = value87 return true88}8990function deleteProperty(state, prop) {91 markChanged(state)92 delete state.copy[prop]93 return true94}9596function getOwnPropertyDescriptor(state, prop) {97 const owner = state.modified98 ? state.copy

`in` -> `Object.hasOwnProperty`Michel Weststratebefore adoption18eb760

line 99why, one step back
99 : has(state.proxies, prop) ? state.proxies : state.base

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 100–114why, one step back
100 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)
101 if (descriptor && !(Array.isArray(owner) && prop === "length"))102 descriptor.configurable = true103 return descriptor104}105106function defineProperty() {107 throw new Error(108 "Immer does currently not support defining properties on draft objects"109 )110}111112function markChanged(state) {113 if (!state.modified) {114 state.modified = true

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 115–116why, one step back
115 state.copy = shallowCopy(state.base)
116 // copy the proxies over the base-copy

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 117–124why, one step back
117 Object.assign(state.copy, state.proxies) // yup that works for arrays as well
118 if (state.parent) markChanged(state.parent)119 }120}121122// creates a proxy for plain objects / arrays123function createProxy(parentState, base) {124 const state = createState(parentState, base)

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 125–128why, one step back
125 const proxy = Array.isArray(base)
126 ? Proxy.revocable([state], arrayTraps)127 : Proxy.revocable(state, objectTraps)128 proxies.push(proxy)

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 129–132why, one step back
129 return proxy.proxy
130}131132export function produceProxy(baseState, producer) {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

lines 133–134why, one step back
133 const previousProxies = proxies
134 proxies = []

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 135–138why, one step back
135 try {
136 // create proxy for root137 const rootClone = createProxy(undefined, baseState)138 // execute the thunk

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

line 139why, one step back
139 verifyReturnValue(producer.call(rootClone, rootClone))

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 140–142why, one step back
140 // and finalize the modified proxy
141 const res = finalize(rootClone)142 // revoke all proxies

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 143why, one step back
143 each(proxies, (_, p) => p.revoke())

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 144–145why, one step back
144 return res
145 } finally {

Merge pull request #69 from mweststrate/feature/fast-pruneMichel Weststratebefore adoptione28bf2a

line 146why, one step back
146 proxies = previousProxies

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

lines 147–148why, one step back
147 }
148}