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
Provenance
- Commit
e2eb0a8- Tree
99a1d6e- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
ed6dfcc
3 files changed+165 added−0 removed
added:__tests__/manual.js+148 added
1+"use strict"2+import {setUseProxies, createDraft, finishDraft, produce} from "../src/index"3+4+runTests("proxy", true)5+runTests("es5", false)6+7+function runTests(name, useProxies) {8+ describe("manual - " + name, () => {9+ setUseProxies(useProxies)10+11+ 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+ })22+23+ it("should support manual drafts", () => {24+ const state = [{}, {}, {}]25+26+ const draft = createDraft(state)27+ draft.forEach((item, index) => {28+ item.index = index29+ })30+31+ const result = finishDraft(draft)32+33+ expect(result).not.toBe(state)34+ expect(result).toEqual([{index: 0}, {index: 1}, {index: 2}])35+ expect(state).toEqual([{}, {}, {}])36+ })37+38+ it("cannot modify after finish", () => {39+ const state = {a: 1}40+41+ 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+ })48+49+ it("should support patches drafts", () => {50+ const state = {a: 1}51+52+ const draft = createDraft(state)53+ draft.a = 254+ draft.b = 355+56+ const patches = []57+ const result = finishDraft(draft, (p, ip) => {58+ patches.push(p, ip)59+ })60+61+ 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+ })89+90+ it("should handle multiple create draft calls", () => {91+ const state = {a: 1}92+93+ const draft = createDraft(state)94+ draft.a = 295+96+ const draft2 = createDraft(state)97+ draft2.b = 398+99+ const result = finishDraft(draft)100+101+ expect(result).not.toBe(state)102+ expect(result).toEqual({a: 2})103+104+ draft2.a = 4105+ const result2 = finishDraft(draft2)106+ expect(result2).not.toBe(result)107+ expect(result2).toEqual({a: 4, b: 3})108+ })109+110+ it("combines with produce - 1", () => {111+ const state = {a: 1}112+113+ 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+ })123+124+ it("combines with produce - 2", () => {125+ const state = {a: 1}126+127+ 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 lines85 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 linesmodified:src/index.js+4 added
44 unmodified lines4546474849505152535455565 unmodified lines44 unmodified lines * This function is a producer, which means copy-on-write is in effect. */export const applyPatches = immer.applyPatches.bind(immer)
export const createDraft = immer.createPublicDraft.bind(immer)
export const finishDraft = immer.finishPublicDraft.bind(immer)
export { original, isDraft, isDraftable,5 unmodified lines