Allmost feature complete!
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 cab47bb · Browse files · 2 files·+38 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+38 added−20 removed
- tests:+3 added−3 removed
- other:+35 added−17 removed
Provenance
- Commit
82ac66c- Tree
3bd9611- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
cab47bb
2 files changed+38 added−20 removed
modified:__tests__/base.js+3 added-3 removed
2 unmodified lines3 import * as immerEs5 from "../es5"4 import deepFreeze from "deep-freeze"5 6 runBaseTest("proxy (no freeze)", immerProxy, false)7-// runBaseTest("proxy (autofreeze)", immerProxy, true)8-// runBaseTest("es5 (no freeze)", immerEs5, false)9-// runBaseTest("es5 (autofreeze)", immerEs5, true)7+runBaseTest("proxy (autofreeze)", immerProxy, true)8+runBaseTest("es5 (no freeze)", immerEs5, false)9+runBaseTest("es5 (autofreeze)", immerEs5, true)10 11 function runBaseTest(name, lib, freeze) {12 describe(`base functionality - ${name}`, () => {13 const immer = lib.default339 unmodified linesmodified:immer.js+35 added-17 removed
43 unmodified lines44 // /** @type {any} */45 // proxies46 47 constructor(parent, base) {48- this.modified48+ this.modified = false49 this.parent = parent50 this.base = base51+ this.copy = undefined52 this.proxies = {}53 }54 55 get source() {56 return this.modified === true ? this.copy : this.base57 }58 59 get(prop) {60+ if (this.modified) {61+ const value = this.copy[prop]62+ if (!isProxy(value) && isProxyable(value))63+ return (this.copy[prop] = createProxy(this, value))64+ return value59- const proxy = this.proxies[prop]60- if (proxy) return proxy61- const value = this.source[prop]62- if (!isProxy(value) && isProxyable(value))63- return (this.proxies[prop] = createProxy(this, value))64- return value65+ } else {66+ if (prop in this.proxies) return this.proxies[prop]67+ const value = this.base[prop]68+ if (!isProxy(value) && isProxyable(value))69+ return (this.proxies[prop] = createProxy(this, value))70+ return value71+ }72 }73 74 set(prop, value) {75 if (!this.modified) {69- if (this.proxies[prop] === value || this.base[prop] === value)76+ if (77+ (prop in this.base && this.base[prop] === value) ||78+ (prop in this.proxies && this.proxies[prop] === value)79+ )80 return81 this.markChanged()82 }73- if (isProxy(value)) this.proxies[prop] = value83 this.copy[prop] = value84 }85 86 markChanged() {87 if (!this.modified) {88 this.modified = true80- this.copy = Array.isArray(this.base)89+ this.copy = Array.isArray(this.base) // TODO: eliminate those isArray checks?90 ? this.base.slice()91 : Object.assign({}, this.base)92+ Object.assign(this.copy, this.proxies) // yup that works for arrays as well93 if (this.parent) this.parent.markChanged()94 }95 }96 }19 unmodified lines116 delete target.copy[property]117 return true118 },119 getOwnPropertyDescriptor(target, prop) {120+ // TOOD: move to state121+ const owner = target.modified122+ ? target.copy123+ : prop in target.proxies ? target.proxies : target.base110- return Reflect.getOwnPropertyDescriptor(target.source, prop)124+ return Reflect.getOwnPropertyDescriptor(owner, prop)125 },126 defineProperty(target, property, descriptor) {127 target.markChanged()128 Object.defineProperty(target.copy, property, descriptor)24 unmodified lines153 target[0].markChanged()154 delete target[0].copy[property]155 return true156 },143- getOwnPropertyDescriptor(target, prop) {144- return Reflect.getOwnPropertyDescriptor(target[0].source, prop)157+ getOwnPropertyDescriptor(target_, prop) {158+ const target = target_[0]159+ const owner = target.modified160+ ? target.copy161+ : prop in target.proxies ? target.proxies : target.base162+ return Reflect.getOwnPropertyDescriptor(owner, prop)163 },164 defineProperty(target, property, descriptor) {165 target[0].markChanged()166 Object.defineProperty(target[0].copy, property, descriptor)20 unmodified lines187 function finalize(base) {188 if (isProxy(base)) {189 const state = base[PROXY_STATE]190 if (state.modified === true) {173- if (isPlainObject(base)) return finalizeObject(state)174- if (Array.isArray(base)) return finalizeArray(state)191+ if (isPlainObject(state.base)) return finalizeObject(state)192+ if (Array.isArray(state.base)) return finalizeArray(state)193 } else return state.base194 }195 return base196 }197 198 function finalizeObject(state) {199 const copy = state.copy200 Object.keys(copy).forEach(prop => {183- copy[prop] = finalize(copy[prop])201+ copy[prop] = finalize(copy[prop]) // TODO: make sure no new proxies are generated!202 })203 return freeze(copy)204 }205 188- function finalizeArray(thing) {206+ function finalizeArray(state) {207 const copy = state.copy208 copy.forEach((value, index) => {209 copy[index] = finalize(copy[index])210 })70 unmodified lines