Skip to content
Myr

feat: createDraft/finishDraft functions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #302

authored by Michel Weststrate · committed by aleclarson · parent ed6dfcc · Browse files · 3 files+165 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+165 added−0 removed

  • tests:+148 added−0 removed
  • other:+17 added−0 removed

3 files+165 added−0 removed

added:__tests__/manual.js+148 added
1"use strict"2import {setUseProxies, createDraft, finishDraft, produce} from "../src/index"34runTests("proxy", true)5runTests("es5", false)67function runTests(name, useProxies) {8 describe("manual - " + name, () => {9 setUseProxies(useProxies)1011 it("should check arguments", () => {12 expect(() => createDraft(3)).toThrow(13 "argument to createDraft should be a plain"14 )15 expect(() => createDraft(new Buffer([]))).toThrow(16 "argument to createDraft should be a plain"17 )18 expect(() => finishDraft({})).toThrow(19 "First argument to finishDraft should be "20 )21 })2223 it("should support manual drafts", () => {24 const state = [{}, {}, {}]2526 const draft = createDraft(state)27 draft.forEach((item, index) => {28 item.index = index29 })3031 const result = finishDraft(draft)3233 expect(result).not.toBe(state)34 expect(result).toEqual([{index: 0}, {index: 1}, {index: 2}])35 expect(state).toEqual([{}, {}, {}])36 })3738 it("cannot modify after finish", () => {39 const state = {a: 1}4041 const draft = createDraft(state)42 draft.a = 243 expect(finishDraft(draft)).toEqual({a: 2})44 expect(() => {45 draft.a = 346 }).toThrow("Cannot use a proxy that has been revoked")47 })4849 it("should support patches drafts", () => {50 const state = {a: 1}5152 const draft = createDraft(state)53 draft.a = 254 draft.b = 35556 const patches = []57 const result = finishDraft(draft, (p, ip) => {58 patches.push(p, ip)59 })6061 expect(result).not.toBe(state)62 expect(result).toEqual({a: 2, b: 3})63 expect(patches).toEqual([64 [65 {66 op: "replace",67 path: ["a"],68 value: 269 },70 {71 op: "add",72 path: ["b"],73 value: 374 }75 ],76 [77 {78 op: "replace",79 path: ["a"],80 value: 181 },82 {83 op: "remove",84 path: ["b"]85 }86 ]87 ])88 })8990 it("should handle multiple create draft calls", () => {91 const state = {a: 1}9293 const draft = createDraft(state)94 draft.a = 29596 const draft2 = createDraft(state)97 draft2.b = 39899 const result = finishDraft(draft)100101 expect(result).not.toBe(state)102 expect(result).toEqual({a: 2})103104 draft2.a = 4105 const result2 = finishDraft(draft2)106 expect(result2).not.toBe(result)107 expect(result2).toEqual({a: 4, b: 3})108 })109110 it("combines with produce - 1", () => {111 const state = {a: 1}112113 const draft = createDraft(state)114 draft.a = 2115 const res1 = produce(draft, d => {116 d.b = 3117 })118 draft.b = 4119 const res2 = finishDraft(draft)120 expect(res1).toEqual({a: 2, b: 3})121 expect(res2).toEqual({a: 2, b: 4})122 })123124 it("combines with produce - 2", () => {125 const state = {a: 1}126127 const res1 = produce(state, draft => {128 draft.b = 3129 const draft2 = createDraft(draft)130 draft.c = 4131 draft2.d = 5132 const res2 = finishDraft(draft2)133 expect(res2).toEqual({134 a: 1,135 b: 3,136 d: 5137 })138 draft.d = 2139 })140 expect(res1).toEqual({141 a: 1,142 b: 3,143 c: 4,144 d: 2145 })146 })147 })148}
modified:src/immer.js+13 added
84 unmodified lines
85 if (result === undefined) return base86 return result !== NOTHING ? result : undefined87 }88 }89 createPublicDraft(value) {90 if (!isDraftable(value)) throw new Error("First argument to createDraft should be a plain-, or immerable object, or array.") // prettier-ignore91 const scope = ImmerScope.enter()92 const draft = this.createDraft(value)93 scope.leave()94 return draft95 }96 finishPublicDraft(draft, patchListener) {97 if (!isDraft(draft)) throw new Error("First argument to finishDraft should be an object created with createDraft.") // prettier-ignore98 const {scope} = draft[DRAFT_STATE]99 scope.usePatches(patchListener)100 return this.processResult(undefined, scope)101 }102 setAutoFreeze(value) {103 this.autoFreeze = value104 }105 setUseProxies(value) {
170 unmodified lines
modified:src/index.js+4 added