Skip to content
Myr

Code cleanup

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 24b5d42 · Browse files · 1 file+86 added−102 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+86 added−102 removed

  • other:+86 added−102 removed

1 file+86 added−102 removed

modified:immer.js+86 added-102 removed
4 unmodified lines
5 throw new Error(6 "Immer requires `Proxy` to be available, but it seems to be not available on your platform. Consider requiring immer '\"immer/es5\"' instead."7 )89/**10 * @typedef {Object} RevocableProxy11 * @property {any} proxy12 * @property {Function} revoke13 */1415const IS_PROXY = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version9const PROXY_STATE = Symbol("immer-proxy-state") // TODO: create per closure, to avoid sharing proxies between multiple immer version10let autoFreeze = true1118// This property indicates that the current object is cloned for another object,19// to make sure the proxy of a frozen object is writeable20const CLONE_TARGET = Symbol("immer-clone-target")12const objectTraps = {13 get(target, prop) {14 if (prop === PROXY_STATE) return target15 return target.get(prop)16 },17 has(target, prop) {18 return prop in target.source19 },20 ownKeys(target) {21 return Reflect.ownKeys(target.source)22 },23 set(target, prop, value) {24 target.set(prop, value)25 return true26 },27 deleteProperty(target, prop) {28 target.deleteProp(prop)29 return true30 },31 getOwnPropertyDescriptor(target, prop) {32 return target.getOwnPropertyDescriptor(prop)33 },34 defineProperty(target, property, descriptor) {35 target.defineProperty(property, descriptor)36 return true37 },38 setPrototypeOf() {39 throw new Error("Don't even try this...")40 }41}4222let autoFreeze = true43const arrayTraps = {44 get(target, prop) {45 if (prop === PROXY_STATE) return target[0]46 return target[0].get(prop)47 },48 has(target, prop) {49 return prop in target[0].source50 },51 ownKeys(target) {52 return Reflect.ownKeys(target[0].source)53 },54 set(target, prop, value) {55 target[0].set(prop, value)56 return true57 },58 deleteProperty(target, prop) {59 target[0].deleteProp(prop)60 return true61 },62 getOwnPropertyDescriptor(target, prop) {63 return target[0].getOwnPropertyDescriptor(prop)64 },65 defineProperty(target, property, descriptor) {66 target[0].defineProperty(property, descriptor)67 return true68 },69 setPrototypeOf() {70 throw new Error("Don't even try this...")71 }72}7374/**75 * Immer takes a state, and runs a function against it.76 * That function can freely mutate the state, as it will create copies-on-write.
46 unmodified lines
123 }124 this.copy[prop] = value125 }126127 deleteProp(prop) {128 this.markChanged()129 delete this.copy[prop]130 }131132 getOwnPropertyDescriptor(prop) {133 const owner = this.modified134 ? this.copy135 : prop in this.proxies ? this.proxies : this.base136 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)137 if (descriptor) descriptor.configurable = true // XXX: is this really needed?138 return descriptor139 }140141 defineProperty(property, descriptor) {142 this.markChanged()143 Object.defineProperty(this.copy, property, descriptor)144 }145146 markChanged() {147 if (!this.modified) {148 this.modified = true149 this.copy = Array.isArray(this.base) // TODO: eliminate those isArray checks?
4 unmodified lines
154 }155 }156 }15789 const objectTraps = {90 get(target, prop) {91 if (prop === IS_PROXY) return true92 if (prop === PROXY_STATE) return target93 return target.get(prop)94 },95 has(target, prop) {96 return prop in target.source97 },98 ownKeys(target) {99 return Reflect.ownKeys(target.source)100 },101 set(target, prop, value) {102 target.set(prop, value)103 return true104 },105 deleteProperty(target, property) {106 target.markChanged()107 delete target.copy[property]108 return true109 },110 getOwnPropertyDescriptor(target, prop) {111 // TOOD: move to state112 const owner = target.modified113 ? target.copy114 : prop in target.proxies ? target.proxies : target.base115 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)116 if (descriptor) descriptor.configurable = true // XXX: is this really needed?117 return descriptor118 },119 defineProperty(target, property, descriptor) {120 target.markChanged()121 Object.defineProperty(target.copy, property, descriptor)122 return true123 },124 setPrototypeOf() {125 throw new Error("Don't even try this...")126 }127 }128129 const arrayTraps = {130 get(target, prop) {131 if (prop === IS_PROXY) return true132 if (prop === PROXY_STATE) return target[0]133 return target[0].get(prop)134 },135 has(target, prop) {136 return prop in target[0].source137 },138 ownKeys(target) {139 return Reflect.ownKeys(target[0].source)140 },141 set(target, prop, value) {142 target[0].set(prop, value)143 return true144 },145 deleteProperty(target, property) {146 target[0].markChanged()147 delete target[0].copy[property]148 return true149 },150 getOwnPropertyDescriptor(target_, prop) {151 // TOOD: move to state152 const target = target_[0]153 const owner = target.modified154 ? target.copy155 : prop in target.proxies ? target.proxies : target.base156 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)157 if (descriptor) descriptor.configurable = true // XXX: is this really needed?158 return descriptor159 },160 defineProperty(target, property, descriptor) {161 target[0].markChanged()162 Object.defineProperty(target[0].copy, property, descriptor)163 return true164 },165 setPrototypeOf() {166 throw new Error("Don't even try this...")167 }168 }169158 // creates a proxy for plain objects / arrays159 function createProxy(parentState, base) {160 const state = new State(parentState, base)161 let proxy
12 unmodified lines
174 function finalize(base) {175 if (isProxy(base)) {176 const state = base[PROXY_STATE]177 if (state.modified === true) {190 if (isPlainObject(state.base)) return finalizeObject(state)178 if (Array.isArray(state.base)) return finalizeArray(state)179 return finalizeObject(state)180 } else return state.base181 }182 return base183 }184185 function finalizeObject(state) {186 const copy = state.copy187 Object.keys(copy).forEach(prop => {200 copy[prop] = finalize(copy[prop]) // TODO: make sure no new proxies are generated!188 copy[prop] = finalize(copy[prop])189 })190 return freeze(copy)191 }192
20 unmodified lines
213 revocableProxies.forEach(p => p.revoke())214 return res215}216229function isPlainObject(value) {230 if (value === null || typeof value !== "object") return false231 const proto = Object.getPrototypeOf(value)232 return proto === Object.prototype || proto === null233}234217function isProxy(value) {236 return !!value && !!value[IS_PROXY]218 return !!value && !!value[PROXY_STATE]219}220221function isProxyable(value) {222 if (!value) return false223 if (typeof value !== "object") return false242 return Array.isArray(value) || isPlainObject(value)224 if (Array.isArray(value)) return true225 const proto = Object.getPrototypeOf(value)226 return (proto === proto) === null || Object.prototype227}228229function freeze(value) {230 if (autoFreeze) {
22 unmodified lines