Skip to content
Myr

initial solution

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 076fbd6 · Browse files · 2 files+49 added−2 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+49 added−2 removed

  • tests:+12 added−0 removed
  • other:+37 added−2 removed

2 files+49 added−2 removed

modified:__tests__/base.js+12 added
44 unmodified lines
4546 it("should return a copy when modifying stuff", () => {47 const nextState = produce(baseState, s => {48 s.aProp = "hello world"49 debugger50 })51 expect(nextState).not.toBe(baseState)52 expect(baseState.aProp).toBe("hi")53 expect(nextState.aProp).toBe("hello world")
374 unmodified lines
428 })429 }).toThrowError(/not support/)430 })431432 it("should not throw error, see #53", () => {433 const result = produce(434 {arr: [{count: 1}, {count: 2}, {count: 3}]},435 draft => {436 debugger437 draft.arr = draft.arr.filter(item => item.count > 2)438 }439 )440 expect(result.arr[0].count).toEqual(3)441 })442443 afterEach(() => {444 expect(baseState).toBe(origBaseState)445 expect(baseState).toEqual(createBaseState())446 })
26 unmodified lines
modified:src/immer.js+37 added-2 removed
44 unmodified lines
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].source49 return prop === PROXY_STATE || prop in target[0].source50 },51 ownKeys(target) {52 return Reflect.ownKeys(target[0].source)53 },
48 unmodified lines
102 if (!isProxyable(baseState)) throw new Error("the first argument to produce should be a plain object or array, got " + (typeof baseState))103 if (typeof producer !== "function") throw new Error("the second argument to produce should be a function")104 }105 const revocableProxies = []106 // for objects that are created in the producer we don't know anything107 // those could contain proxies anywhere (see #53)108 // we don't want to walk through the entire state tree to know which objects are unknown109 // so, we keep a set of objects that the user visited so that we know that any object outside that need to be investigated further110 const existingEdges = new Set()111112 class State {113 constructor(parent, base) {114 this.modified = false
68 unmodified lines
183 }184185 // creates a proxy for plain objects / arrays186 function createProxy(parentState, base) {187 existingEdges.delete(base)188 const state = new State(parentState, base)189 let proxy190 if (Array.isArray(base)) {191 for (let i = 0; i < base.length; i++) {192 const value = base[i]193 if (value !== null && typeof value === "object")194 existingEdges.add(value)195 }196 // Proxy should be created with an array to make it an array for JS197 // so... here you have it!198 proxy = Proxy.revocable([state], arrayTraps)199 } else {200 for (var key in base) {201 const value = base[key]202 if (value !== null && typeof value === "object")203 existingEdges.add(value)204 }205 proxy = Proxy.revocable(state, objectTraps)206 }207 revocableProxies.push(proxy)208 return proxy.proxy
6 unmodified lines
215 if (state.modified === true) {216 if (Array.isArray(state.base)) return finalizeArray(state)217 return finalizeObject(state)218 } else return state.base219 } else if (220 base !== null &&221 typeof base === "object" &&222 !existingEdges.has(base)223 ) {224 // This object was not part of the original tree, so anywhere deep inside it could be changed objects..225 // TODO optimize: unless we processed all proxies226 // TODO: first check deeply whether there are proxies in there?227 if (Array.isArray(base)) return freeze(base.map(finalize))228 const proto = Object.getPrototypeOf(base)229 if (proto === null || proto === Object.prototype) {230 // object231 const res = Object.assign({}, base)232 Object.keys(res).forEach(prop => {233 // TODO: optimize for loops234 res[prop] = finalize(res[prop])235 })236 return freeze(res)237 }238 }239 return base240 }241
37 unmodified lines
279 if (!value) return false280 if (typeof value !== "object") return false281 if (Array.isArray(value)) return true282 const proto = Object.getPrototypeOf(value)248 return (proto === proto) === null || Object.prototype283 return proto === null || proto === Object.prototype284}285286function freeze(value) {287 // short circuit to achieve 100% code coverage instead of 98%
20 unmodified lines