Skip to content
Myr

leaner implementation

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by Michel Weststrate · parent d20adea · Browse files · 1 file+15 added−47 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+15 added−47 removed

  • other:+15 added−47 removed

1 file+15 added−47 removed

modified:src/immer.js+15 added-47 removed
9 unmodified lines
10let autoFreeze = true11let revocableProxies = null1213const objectTraps = {14 get(target, prop) {15 if (prop === PROXY_STATE) return target16 return get(target, prop)17 },14 get,15 has(target, prop) {16 return prop in source(target)17 },18 ownKeys(target) {19 return Reflect.ownKeys(source(target))20 },24 set(target, prop, value) {25 set(target, prop, value) // TODO: eliminate these closures26 return true27 },28 deleteProperty(target, prop) {29 deleteProp(target, prop) // TODO: eliminate these closures30 return true31 },32 getOwnPropertyDescriptor(target, prop) {33 return getOwnPropertyDescriptor(target, prop)34 },35 defineProperty(target, property, descriptor) {36 defineProperty(target, property, descriptor)37 },21 set,22 deleteProperty,23 getOwnPropertyDescriptor,24 defineProperty,25 setPrototypeOf() {26 throw new Error("Don't even try this...")27 }28}2943const arrayTraps = {44 get(target, prop) {45 if (prop === PROXY_STATE) return target[0]46 return get(target[0], prop)47 },48 has(target, prop) {49 return prop === PROXY_STATE || prop in source(target[0])50 },51 ownKeys(target) {52 return Reflect.ownKeys(source(target[0]))53 },54 set(target, prop, value) {55 set(target[0], prop, value)56 return true57 },58 deleteProperty(target, prop) {59 deleteProp(target[0], prop)60 return true61 },62 getOwnPropertyDescriptor(target, prop) {63 return getOwnPropertyDescriptor(target[0], prop)64 },65 defineProperty(target, property, descriptor) {66 defineProperty(target[0], property, descriptor)67 },68 setPrototypeOf() {69 throw new Error("Don't even try this...")30const arrayTraps = {}31for (let key in objectTraps) {32 arrayTraps[key] = function() {33 arguments[0] = arguments[0][0]34 return objectTraps[key].apply(this, arguments)35 }36}3738class State {
11 unmodified lines
50 return state.modified === true ? state.copy : state.base51}5253function get(state, prop) {54 if (prop === PROXY_STATE) return state55 if (state.modified) {56 const value = state.copy[prop]57 if (!isProxy(value) && isProxyable(value))58 return (state.copy[prop] = createProxy(state, value))
12 unmodified lines
71 if (72 (prop in state.base && Object.is(state.base[prop], value)) ||73 (prop in state.proxies && state.proxies[prop] === value)74 )109 return75 return true76 markChanged(state)77 }78 state.copy[prop] = value79 return true80}81115function deleteProp(state, prop) {82function deleteProperty(state, prop) {83 markChanged(state)84 delete state.copy[prop]85 return true86}8788function getOwnPropertyDescriptor(state, prop) {89 const owner = state.modified
171 unmodified lines