Skip to content
Myr

proxy.js

This file

Path

src/proxy.js

166 lines · 4.1 KiB · JavaScript

Last changed by

Fixed finalizing arrayiruca3before adoption4202907

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

lines 4–8why, one step back
4import {
5 isProxyable,6 isProxy,7 freeze,8 PROXY_STATE,

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

lines 9–12why, one step back
9 finalize,
10 shallowCopy,11 verifyReturnValue,12 each

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

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

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

line 14why, one step back
14

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

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

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

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

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

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

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

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

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

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

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

line 39why, one step back
39 }

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

line 40why, one step back
40})

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

lines 41–110why, one step back
41
42function createState(parent, base) {43 return {44 modified: false,45 finalized: false,46 parent,47 base,48 copy: undefined,49 proxies: {}50 }51}5253function source(state) {54 return state.modified === true ? state.copy : state.base55}5657function get(state, prop) {58 if (prop === PROXY_STATE) return state59 if (state.modified) {60 const value = state.copy[prop]61 if (!isProxy(value) && isProxyable(value))62 return (state.copy[prop] = createProxy(state, value))63 return value64 } else {65 if (prop in state.proxies) return state.proxies[prop]66 const value = state.base[prop]67 if (!isProxy(value) && isProxyable(value))68 return (state.proxies[prop] = createProxy(state, value))69 return value70 }71}7273function set(state, prop, value) {74 if (!state.modified) {75 if (76 (prop in state.base && Object.is(state.base[prop], value)) ||77 (prop in state.proxies && state.proxies[prop] === value)78 )79 return true80 markChanged(state)81 }82 state.copy[prop] = value83 return true84}8586function deleteProperty(state, prop) {87 markChanged(state)88 delete state.copy[prop]89 return true90}9192function getOwnPropertyDescriptor(state, prop) {93 const owner = state.modified94 ? state.copy95 : prop in state.proxies ? state.proxies : state.base96 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)97 if (descriptor && !(Array.isArray(owner) && prop === "length"))98 descriptor.configurable = true99 return descriptor100}101102function defineProperty() {103 throw new Error(104 "Immer does currently not support defining properties on draft objects"105 )106}107108function markChanged(state) {109 if (!state.modified) {110 state.modified = true

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

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

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

lines 113–120why, one step back
113 Object.assign(state.copy, state.proxies) // yup that works for arrays as well
114 if (state.parent) markChanged(state.parent)115 }116}117118// creates a proxy for plain objects / arrays119function createProxy(parentState, base) {120 const state = createState(parentState, base)

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

lines 121–124why, one step back
121 const proxy = Array.isArray(base)
122 ? Proxy.revocable([state], arrayTraps)123 : Proxy.revocable(state, objectTraps)124 proxies.push(proxy)

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

lines 125–127why, one step back
125 return proxy.proxy
126}127

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

line 128why, one step back
128export function finalizeObject(state) {

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

lines 129–130why, one step back
129 const copy = state.copy
130 const base = state.base

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

lines 131–133why, one step back
131 each(copy, (prop, value) => {
132 if (value !== base[prop]) copy[prop] = finalize(value)133 })

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

lines 134–136why, one step back
134 return freeze(copy)
135}136

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

line 137why, one step back
137export function finalizeArray(state) {

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

lines 138–139why, one step back
138 const copy = state.copy
139 const base = state.base

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

line 140why, one step back
140 each(copy, (i, value) => {

Fixed finalizing arrayiruca3before adoption4202907

lines 141–145why, one step back
141 if (value !== base[i]) {
142 copy[i] = finalize(value)143 } else {144 copy[i] = finalize(copy[i])145 }

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

line 146why, one step back
146 })

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

lines 147–150why, one step back
147 return freeze(copy)
148}149150export function produceProxy(baseState, producer) {

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

lines 151–152why, one step back
151 const previousProxies = proxies
152 proxies = []

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

lines 153–156why, one step back
153 try {
154 // create proxy for root155 const rootClone = createProxy(undefined, baseState)156 // execute the thunk

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

line 157why, one step back
157 verifyReturnValue(producer(rootClone))

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

lines 158–160why, one step back
158 // and finalize the modified proxy
159 const res = finalize(rootClone)160 // revoke all proxies

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

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

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

lines 162–163why, one step back
162 return res
163 } finally {

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

line 164why, one step back
164 proxies = previousProxies

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

lines 165–166why, one step back
165 }
166}