Skip to content
Myr

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

2 files+111 added−49 removed

modified:__tests__/base.js+5 added-4 removed
7 unmodified lines
8 beforeEach(() => {9 origBaseState = baseState = createBaseState()10 })1112 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 })1617 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 lines
3132 expect(warning).toHaveBeenCalledTimes(4)33 })3435 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 lines
42 // structural sharing?43 expect(nextState.nested).toBe(baseState.nested)44 })4546 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 lines
modified:immer-es5.js+106 added-45 removed
5 unmodified lines
6 * @property {any} proxy7 * @property {Function} revoke8 */910// TODO: rename to PROXY_TARGET11const IMMER_PROXY = Symbol("immer-proxy") // TODO: create per closure, to avoid sharing proxies between multiple immer version1213// This property indicates that the current object is cloned for another object,14// to make sure the proxy of a frozen object is writeable15const CLONE_TARGET = Symbol("immer-clone-target")16const CHANGED_STATE = Symbol("immer-changed-state")17const CHANGED_METHOD = Symbol("immer-changed")1819let autoFreeze = true2021/**
68 unmodified lines
90 }9192 // 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 }99100 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 }138139 // 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 lines
155 return false156 }157158 // 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 }165143 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 }174175 function finalizeArray(thing) {176 if (!hasChanges(thing)) return thing
17 unmodified lines
194 // 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}200201/**202 * Revoke all the proxies stored in the revocableProxies map
12 unmodified lines
215 return proto === Object.prototype || proto === null216}217218function isProxy(value) {197 return !!value && !!value[IMMER_PROXY]219 return !!(value && value[IMMER_PROXY])220}221222function freeze(value) {223 if (autoFreeze) {
1 unmodified line
225 }226 return value227}228229function createHiddenProperty(target, prop, value) {230 Object.defineProperty(target, prop, {231 value: value,232 enumerable: false,233 writable: true,234 })235}236237function shallowEqual(objA, objB) {238 if (Object.is(objA, objB)) {239 return true240 }241242 if (243 typeof objA !== "object" ||244 objA === null ||245 typeof objB !== "object" ||246 objB === null247 ) {248 return false249 }250251 const keysA = Object.keys(objA)252 const keysB = Object.keys(objB)253254 if (keysA.length !== keysB.length) {255 return false256 }257258 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 }266267 return true268}269270/**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 lines
278function setAutoFreeze(enableAutoFreeze) {279 autoFreeze = enableAutoFreeze280}281219Object.defineProperty(exports, "__esModule", {220 value: true,221})282createHiddenProperty(exports, "__esModule", true)283module.exports.default = immer284module.exports.setAutoFreeze = setAutoFreeze