Fixed remaining failing tests
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 82ac66c · Browse files · 1 file·+18 added−19 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+18 added−19 removed
- other:+18 added−19 removed
Provenance
- Commit
4e7f44a- Tree
72daa7d- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
82ac66c
1 file changed+18 added−19 removed
modified:immer.js+18 added-19 removed
30 unmodified lines31 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified32 * @returns {any} a new state, or the base state if nothing was modified33 */34 function immer(baseState, thunk) {35+ const revocableProxies = []36+37 class State {38 // /** @type {boolean} */39 // modified40 // /** @type {State} */81 unmodified lines122 // TOOD: move to state123 const owner = target.modified124 ? target.copy125 : prop in target.proxies ? target.proxies : target.base124- return Reflect.getOwnPropertyDescriptor(owner, prop)126+ const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)127+ if (descriptor) descriptor.configurable = true // XXX: is this really needed?128+ return descriptor129 },130 defineProperty(target, property, descriptor) {131 target.markChanged()132 Object.defineProperty(target.copy, property, descriptor)25 unmodified lines158 delete target[0].copy[property]159 return true160 },161 getOwnPropertyDescriptor(target_, prop) {162+ // TOOD: move to state163 const target = target_[0]164 const owner = target.modified165 ? target.copy166 : prop in target.proxies ? target.proxies : target.base162- return Reflect.getOwnPropertyDescriptor(owner, prop)167+ const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)168+ if (descriptor) descriptor.configurable = true // XXX: is this really needed?169+ return descriptor170 },171 defineProperty(target, property, descriptor) {172 target[0].markChanged()173 Object.defineProperty(target[0].copy, property, descriptor)6 unmodified lines180 181 // creates a proxy for plain objects / arrays182 function createProxy(parentState, base) {183 const state = new State(parentState, base)184+ let proxy185 if (Array.isArray(base)) {186 // Proxy should be created with an array to make it an array for JS187 // so... here you have it!180- return new Proxy([state], arrayTraps)188+ proxy = Proxy.revocable([state], arrayTraps)189 } else {182- return new Proxy(state, objectTraps)190+ proxy = Proxy.revocable(state, objectTraps)191 }192+ revocableProxies.push(proxy)193+ return proxy.proxy194 }195 196 // given a base object, returns it if unmodified, or return the changed cloned if modified197 function finalize(base) {31 unmodified lines229 !Object.is(maybeVoidReturn, undefined) &&230 console.warn(231 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`232 )223- // console.log(`proxies: ${revocableProxies.size}, copies: ${copies.size}`)224- // revoke all proxies225- // TODO: revoke(revocableProxies)233 // and finalize the modified proxy227- return finalize(rootClone)234+ const res = finalize(rootClone)235+ // revoke all proxies236+ revocableProxies.forEach(p => p.revoke())237+ return res238 }239 230-/**231- * Revoke all the proxies stored in the revocableProxies map232- *233- * @param {Map<Object,RevocableProxy>} revocableProxies234- */235-function revoke(revocableProxies) {236- for (var revocableProxy of revocableProxies.values()) {237- revocableProxy.revoke()238- }239-}240-240 function isPlainObject(value) {241 if (value === null || typeof value !== "object") return false242 const proto = Object.getPrototypeOf(value)243 return proto === Object.prototype || proto === null36 unmodified lines