Merge branch 'master' into map-fixes
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 · parents db9b689, 7a8e4e4 · Browse files · 5 files·+45 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+45 added−9 removed
- tests:+36 added−7 removed
- other:+9 added−2 removed
Provenance
- Commit
e4117ff- Tree
5016da8- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent 1
db9b689- Parent 2
7a8e4e4
5 files changed+45 added−9 removed
modified:__tests__/__snapshots__/manual.js.snap+2 added-2 removed
48 unmodified lines49 ],50 ]51 `;52 53-exports[`manual - proxy cannot modify after finish 1`] = `"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? {\\"a\\":2}"`;53+exports[`manual - proxy cannot modify after finish 1`] = `"Cannot perform 'set' on a proxy that has been revoked"`;54 55 exports[`manual - proxy should check arguments 1`] = `"First argument to \`createDraft\` must be a plain object, an array, or an immerable object"`;56 57 exports[`manual - proxy should check arguments 2`] = `"First argument to \`createDraft\` must be a plain object, an array, or an immerable object"`;1 unmodified line59 exports[`manual - proxy should check arguments 3`] = `"First argument to \`finishDraft\` must be a draft returned by \`createDraft\`"`;60 61 exports[`manual - proxy should not finish drafts from produce 1`] = `"First argument to \`finishDraft\` must be a draft returned by \`createDraft\`"`;62 63-exports[`manual - proxy should not finish twice 1`] = `"The given draft is already finalized"`;63+exports[`manual - proxy should not finish twice 1`] = `"Cannot perform 'get' on a proxy that has been revoked"`;64 65 exports[`manual - proxy should support patches drafts 1`] = `66 Array [67 Array [34 unmodified linesmodified:__tests__/curry.js+3 added-1 removed
4 unmodified lines56789910111213141583 unmodified lines4 unmodified linesrunTests("es5", false)
function runTests(name, useProxies) { describe("curry - " + name, () => { beforeAll(() => { setUseProxies(useProxies) setUseProxies(useProxies) })
it("should check arguments", () => { expect(() => produce()).toThrowErrorMatchingSnapshot() expect(() => produce({})).toThrowErrorMatchingSnapshot()83 unmodified linesmodified:__tests__/frozen.js+28 added-3 removed
6 unmodified lines7 runTests("es5", false)8 9 function runTests(name, useProxies) {10 describe("auto freeze - " + name, () => {11+ beforeAll(() => {11- setUseProxies(useProxies)12- setAutoFreeze(true)12+ setUseProxies(useProxies)13+ setAutoFreeze(true)14+ })15 16 it("never freezes the base state", () => {17 const base = {arr: [1], obj: {a: 1}}18 const next = produce(base, draft => {118 unmodified lines137 })138 139 it("will freeze maps", () => {140 const base = new Map()139- debugger141+142 const res = produce(base, draft => {143 draft.set("a", 1)144 })145 expect(() => res.set("b", 2)).toThrow(27 unmodified lines173 174 // In draft, still editable175 expect(produce(res, d => void d.add(2))).not.toBe(res)176 })177+178+ it("Map#get() of frozen object will became draftable", () => {179+ const base = {180+ map: new Map([181+ ["a", new Map([["a", true], ["b", true], ["c", true]])],182+ ["b", new Map([["a", true]])],183+ ["c", new Map([["a", true]])]184+ ])185+ }186+187+ // This will freeze maps188+ const frozen = produce(base, draft => {})189+190+ // https://github.com/immerjs/immer/issues/472191+ produce(frozen, draft => {192+ // if (useProxies) debugger193+ ;["b", "c"].forEach(other => {194+ const m = draft.map.get(other)195+196+ m.delete("a")197+ })198+ })199+ })200 })201 }modified:__tests__/manual.js+3 added-1 removed
10 unmodified lines11 runTests("es5", false)12 13 function runTests(name, useProxies) {14 describe("manual - " + name, () => {15+ beforeAll(() => {15- setUseProxies(useProxies)16+ setUseProxies(useProxies)17+ })18 19 it("should check arguments", () => {20 expect(() => createDraft(3)).toThrowErrorMatchingSnapshot()21 const buf = new Buffer([])117 unmodified linesmodified:src/proxy.js+9 added-2 removed
240 unmodified lines241 const value = receiver.get(key)242 cb.call(thisArg, value, key, map)243 }),244 get: state => key => {245- const drafts = state[state.modified ? "copy" : "drafts"]245+ const drafts = state.modified ? state.copy : state.drafts246+247 if (drafts.has(key)) {247- return drafts.get(key)248+ const value = drafts.get(key)249+250+ if (isDraft(value) || !isDraftable(value)) return value251+252+ const draft = createProxy(value, state)253+ drafts.set(key, draft)254+ return draft255 }256 257 const value = latest(state).get(key)258 if (state.finalized || !isDraftable(value)) {116 unmodified lines