WIP
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 a3924fb · Browse files · 2 files·+111 added−49 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+111 added−49 removed
- tests:+5 added−4 removed
- other:+106 added−45 removed
Provenance
- Commit
f890f96- Tree
16ce677- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
a3924fb
2 files changed+111 added−49 removed
modified:__tests__/base.js+5 added-4 removed
7 unmodified lines8 beforeEach(() => {9 origBaseState = baseState = createBaseState()10 })11 12- it("should return the original without modifications", () => {12+ it.only("should return the original without modifications", () => {13 const nextState = immer(baseState, () => {})14 expect(nextState).toBe(baseState)15 })16 17 it("should return the original without modifications when reading stuff", () => {18 const nextState = immer(baseState, s => {19 expect(s.aProp).toBe("hi")20- expect(s.anObject.nested).toEqual({yummie: true})20+ expect(s.anObject.nested).toMatchObject({yummie: true})21 })22 expect(nextState).toBe(baseState)23 })24 6 unmodified lines31 32 expect(warning).toHaveBeenCalledTimes(4)33 })34 35- it("should return a copy when modifying stuff", () => {35+ it.only("should return a copy when modifying stuff", () => {36 const nextState = immer(baseState, s => {37 s.aProp = "hello world"38 })39 expect(nextState).not.toBe(baseState)2 unmodified lines42 // structural sharing?43 expect(nextState.nested).toBe(baseState.nested)44 })45 46- it("deep change bubbles up", () => {46+ it.only("deep change bubbles up", () => {47+ debugger48 const nextState = immer(baseState, s => {49 s.anObject.nested.yummie = false50 })51 expect(nextState).not.toBe(baseState)195 unmodified linesmodified:immer-es5.js+106 added-45 removed
5 unmodified lines6 * @property {any} proxy7 * @property {Function} revoke8 */9 10+// TODO: rename to PROXY_TARGET11 const IMMER_PROXY = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version12 13 // This property indicates that the current object is cloned for another object,14 // to make sure the proxy of a frozen object is writeable15 const CLONE_TARGET = Symbol("immer-clone-target")16+const CHANGED_STATE = Symbol("immer-changed-state")17+const CHANGED_METHOD = Symbol("immer-changed")18 19 let autoFreeze = true20 21 /**68 unmodified lines90 }91 92 // creates a proxy for plain objects / arrays93 function createProxy(base) {91- if (isPlainObject(base) || Array.isArray(base)) {92- if (isProxy(base)) return base // avoid double wrapping93- if (revocableProxies.has(base))94- return revocableProxies.get(base).proxy95- let proxyTarget96- // special case, if the base tree is frozen, we cannot modify it's proxy it in strict mode, clone first.97- if (Object.isFrozen(base)) {98- proxyTarget = Array.isArray(base)99- ? base.slice()100- : Object.assign({}, base)101- Object.defineProperty(proxyTarget, CLONE_TARGET, {102- enumerable: false,103- value: base,104- configurable: true,105- })106- } else {107- proxyTarget = base108- }109- // create the proxy110- const revocableProxy = Proxy.revocable(proxyTarget, objectTraps)111- revocableProxies.set(base, revocableProxy)112- return revocableProxy.proxy113- }94+ if (isProxy(base)) return base95+ if (isPlainObject(base)) return createObjectProxy(base)96+ if (Array.isArray(base)) return createObjectProxy(base) // TODO: create Array proxy?97 return base98 }99 100+ function createObjectProxy(base) {101+ // TODO: optimize, reuse property accessors102+ const proxy = {}103+ createHiddenProperty(proxy, IMMER_PROXY, base)104+ createHiddenProperty(proxy, CHANGED_STATE, false)105+ createHiddenProperty(proxy, CHANGED_METHOD, function() {106+ if (this[CHANGED_STATE] === true) return true107+ // TODO: first check child properties, that might be cheaper...108+ return !shallowEqual(Object.keys(base), Object.keys(this))109+ })110+ Object.keys(base).forEach(prop => {111+ Object.defineProperty(proxy, prop, {112+ configurable: true,113+ enumerable: true,114+ get() {115+ // const target = this[PROXY_TARGET]116+ // return createProxy(getCurrentSource(target)[prop])117+ return createProxy(base[prop])118+ },119+ set(value) {120+ this[CHANGED_STATE] = true121+ Object.defineProperty(this, prop, {122+ enumerable: true,123+ writable: true,124+ configurable: true,125+ value: value,126+ })127+ },128+ })129+ })130+ revocableProxies.set(base, {131+ proxy,132+ revoke: () => {133+ // TODO: set unreadable state, reuse closure134+ },135+ })136+ return proxy137+ }138+139 // checks if the given base object has modifications, either because it is modified, or140 // because one of it's children is119- function hasChanges(base) {120- const proxy = revocableProxies.get(base)121- if (!proxy) return false // nobody did read this object122- if (copies.has(base)) return true // a copy was created, so there are changes123- // look deeper141+ function hasChanges(proxy) {142+ if (!isProxy(proxy)) return false143+ const base = proxy[IMMER_PROXY]144+ if (proxy[CHANGED_METHOD](base)) return true // some property was modified145+ // look deeper, this object was not modified, but maybe some of its children are146 const keys = Object.keys(base)147 for (let i = 0; i < keys.length; i++) {126- const value = base[keys[i]]148+ const value = proxy[keys[i]] // TODO: fix, this does convert value to a proxy?!149 if (150 (Array.isArray(value) || isPlainObject(value)) &&151 hasChanges(value)152 )2 unmodified lines155 return false156 }157 158 // given a base object, returns it if unmodified, or return the changed cloned if modified137- function finalize(base) {138- if (isPlainObject(base)) return finalizeObject(base)139- if (Array.isArray(base)) return finalizeArray(base)140- return base159+ function finalize(thing) {160+ if (!isProxy(thing)) return thing161+ if (isPlainObject(thing)) return finalizeObject(thing)162+ if (Array.isArray(thing)) return finalizeArray(thing)163+ return thing164 }165 143- function finalizeObject(thing) {144- if (!hasChanges(thing)) return thing145- const copy = getOrCreateCopy(thing) // TODO: getOrCreate is weird here..146- Object.keys(copy).forEach(prop => {147- copy[prop] = finalize(copy[prop])166+ function finalizeObject(proxy) {167+ if (!hasChanges(proxy)) return proxy[IMMER_PROXY] // return the original target168+ Object.keys(proxy).forEach(prop => {169+ proxy[prop] = finalize(proxy[prop])170 })149- delete copy[CLONE_TARGET]150- return freeze(copy)171+ delete proxy[CLONE_TARGET]172+ return freeze(proxy)173 }174 175 function finalizeArray(thing) {176 if (!hasChanges(thing)) return thing17 unmodified lines194 // console.log(`proxies: ${revocableProxies.size}, copies: ${copies.size}`)195 // revoke all proxies196 revoke(revocableProxies)197 // and finalize the modified proxy176- return finalize(baseState)198+ return finalize(rootClone)199 }200 201 /**202 * Revoke all the proxies stored in the revocableProxies map12 unmodified lines215 return proto === Object.prototype || proto === null216 }217 218 function isProxy(value) {197- return !!value && !!value[IMMER_PROXY]219+ return !!(value && value[IMMER_PROXY])220 }221 222 function freeze(value) {223 if (autoFreeze) {1 unmodified line225 }226 return value227 }228 229+function createHiddenProperty(target, prop, value) {230+ Object.defineProperty(target, prop, {231+ value: value,232+ enumerable: false,233+ writable: true,234+ })235+}236+237+function shallowEqual(objA, objB) {238+ if (Object.is(objA, objB)) {239+ return true240+ }241+242+ if (243+ typeof objA !== "object" ||244+ objA === null ||245+ typeof objB !== "object" ||246+ objB === null247+ ) {248+ return false249+ }250+251+ const keysA = Object.keys(objA)252+ const keysB = Object.keys(objB)253+254+ if (keysA.length !== keysB.length) {255+ return false256+ }257+258+ for (let i = 0; i < keysA.length; i++) {259+ if (260+ !hasOwnProperty.call(objB, keysA[i]) ||261+ !Object.is(objA[keysA[i]], objB[keysA[i]])262+ ) {263+ return false264+ }265+ }266+267+ return true268+}269+270 /**271 * Automatically freezes any state trees generated by immer.272 * This protects against accidental modifications of the state tree outside of an immer function.273 * This comes with a performance impact, so it is recommended to disable this option in production.4 unmodified lines278 function setAutoFreeze(enableAutoFreeze) {279 autoFreeze = enableAutoFreeze280 }281 219-Object.defineProperty(exports, "__esModule", {220- value: true,221-})282+createHiddenProperty(exports, "__esModule", true)283 module.exports.default = immer284 module.exports.setAutoFreeze = setAutoFreeze