Faster, simpler implementation
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 d618d28 · Browse files · 2 files·+69 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+69 added−49 removed
- tests:+51 added−9 removed
- other:+18 added−40 removed
Provenance
- Commit
c420f0d- Tree
7a1f170- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
d618d28
2 files changed+69 added−49 removed
modified:__tests__/base.js+51 added-9 removed
44 unmodified lines45 46 it("should return a copy when modifying stuff", () => {47 const nextState = produce(baseState, s => {48 s.aProp = "hello world"49- debugger49 })50 expect(nextState).not.toBe(baseState)51 expect(baseState.aProp).toBe("hi")52 expect(nextState.aProp).toBe("hello world")374 unmodified lines427 })428 }).toThrowError(/not support/)429 })430 432- 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- )431+ it("should not throw error, see #53 - 1", () => {432+ const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}433+ const result = produce(base, draft => {434+ draft.arr = draft.arr.filter(item => item.count > 2)435+ })436 expect(result.arr[0].count).toEqual(3)437+ expect(result).toEqual({438+ arr: [{count: 3}]439+ })440+ expect(result.arr[0]).toBe(base.arr[2])441 })442 443+ it("should not throw error, see #53 - 2", () => {444+ const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}445+ const result = produce(base, draft => {446+ draft.newArr = draft.arr.filter(item => item.count > 2)447+ })448+ expect(result.newArr[0].count).toEqual(3)449+ expect(result.arr).toBe(base.arr)450+ expect(result).toEqual({451+ arr: [452+ {453+ count: 1454+ },455+ {456+ count: 2457+ },458+ {459+ count: 3460+ }461+ ],462+ newArr: [463+ {464+ count: 3465+ }466+ ]467+ })468+ expect(result.newArr[0]).toBe(base.arr[2]) // isn't this weird? not sure469+ expect(result.arr[2]).toBe(base.arr[2]) // isn't this weird? not sure470+ })471+472+ it("should not throw error, see #53 - 3", () => {473+ const base = {arr: [{count: 1}, {count: 2}, {count: 3}]}474+ const result = produce(base, draft => {475+ draft.newArr = draft.arr.filter(item => item.count > 2)476+ delete draft.arr477+ })478+ expect(result.newArr[0].count).toEqual(3)479+ expect(result).toEqual({480+ newArr: [{count: 3}]481+ })482+ expect(result.newArr[0]).toBe(base.arr[2])483+ })484+485 afterEach(() => {486 expect(baseState).toBe(origBaseState)487 expect(baseState).toEqual(createBaseState())488 })26 unmodified linesmodified:src/immer.js+18 added-40 removed
101 unmodified lines102 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()106 107 class State {108 constructor(parent, base) {109 this.modified = false68 unmodified lines178 }179 180 // creates a proxy for plain objects / arrays181 function createProxy(parentState, base) {187- existingEdges.delete(base)182 const state = new State(parentState, base)183 let proxy184 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!185 proxy = Proxy.revocable([state], arrayTraps)186 } else {200- for (var key in base) {201- const value = base[key]202- if (value !== null && typeof value === "object")203- existingEdges.add(value)204- }187 proxy = Proxy.revocable(state, objectTraps)188 }189 revocableProxies.push(proxy)190 return proxy.proxy6 unmodified lines197 if (state.modified === true) {198 if (Array.isArray(state.base)) return finalizeArray(state)199 return finalizeObject(state)200 } 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))201+ } else if (base !== null && typeof base === "object") {202+ // If finalize is called on an object that was not a proxy, it means that it is an object that was not there in the original203+ // tree and it could contain proxies at arbitrarily places. Let's find and finalize them as well204+ if (Array.isArray(base)) {205+ for (let i = 0; i < base.length; i++)206+ base[i] = finalize(base[i])207+ return freeze(base)208+ }209 const proto = Object.getPrototypeOf(base)210 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)211+ for (let key in base) base[key] = finalize(base[key])212+ return freeze(base)213 }214 }215 return base216 }217 218 function finalizeObject(state) {219 const copy = state.copy244- Object.keys(copy).forEach(prop => {245- copy[prop] = finalize(copy[prop])246- })220+ const base = state.base221+ for (var prop in copy) {222+ if (copy[prop] !== base[prop]) copy[prop] = finalize(copy[prop])223+ }224 return freeze(copy)225 }226 227 function finalizeArray(state) {228 const copy = state.copy252- copy.forEach((value, index) => {253- copy[index] = finalize(copy[index])254- })229+ const base = state.base230+ for (let i = 0; i < copy.length; i++) {231+ if (copy[i] !== base[i]) copy[i] = finalize(copy[i])232+ }233 return freeze(copy)234 }235 236 // create proxy for root49 unmodified lines