Skip to content
Myr

Immer now changes the context (`this`) of producer functions by default to the draft state

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 c8b61f6 · Browse files · 5 files+28 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+28 added−9 removed

  • tests:+20 added−1 removed
  • other:+8 added−8 removed

5 files+28 added−9 removed

modified:__tests__/base.js+20 added-1 removed
701 unmodified lines
702 )703 })704 })705706 it("in should work", () => {706 it("'in' should work", () => {707 produce(createBaseState(), draft => {708 expect("anArray" in draft).toBe(true)709 expect(Reflect.has(draft, "anArray")).toBe(true)710
11 unmodified lines
722 expect(Reflect.has(draft.anArray, "17")).toBe(false)723 })724 })725726 it("'this' should work - 1", () => {727 const base = {x: 3}728 const next1 = produce(base, function() {729 this.x = 4730 })731 expect(next1).not.toBe(base)732 expect(next1.x).toBe(4)733 })734735 it("'this' should work - 1", () => {736 const base = {x: 3}737 const incrementor = produce(function() {738 this.x = 4739 })740 const next1 = incrementor(base)741 expect(next1).not.toBe(base)742 expect(next1.x).toBe(4)743 })744745 afterEach(() => {746 expect(baseState).toBe(origBaseState)747 expect(baseState).toEqual(createBaseState())748 })
21 unmodified lines
modified:src/es5.js+1 added-1 removed
modified:src/immer.d.ts+5 added-5 removed
10 unmodified lines
11 * @returns The next state: a new state, or the current state if nothing was modified12 */13export default function<S = any>(14 currentState: S,15 recipe: (draftState: S) => void15 recipe: (this: S, draftState: S) => void16): S17// curried invocations18export default function<S = any, A = any, B = any, C = any>(19 recipe: (draftState: S, a: A, b: B, c: C) => void19 recipe: (this: S, draftState: S, a: A, b: B, c: C) => void20): (currentState: S, a: A, b: B, c: C) => S21export default function<S = any, A = any, B = any>(22 recipe: (draftState: S, a: A, b: B) => void22 recipe: (this: S, draftState: S, a: A, b: B) => void23): (currentState: S, a: A, b: B) => S24export default function<S = any, A = any>(25 recipe: (draftState: S, a: A) => void25 recipe: (this: S, draftState: S, a: A) => void26): (currentState: S) => S27export default function<S = any>(28 recipe: (draftState: S, ...extraArgs: any[]) => void28 recipe: (this: S, draftState: S, ...extraArgs: any[]) => void29): (currentState: S, ...extraArgs: any[]) => S3031/**32 * Automatically freezes any state trees generated by immer.
11 unmodified lines
modified:src/index.js+1 added-1 removed
22 unmodified lines
23 return function() {24 const args = arguments25 return produce(args[0], draft => {26 args[0] = draft // blegh!27 producer.apply(null, args)27 producer.apply(draft, args)28 })29 }30 }31
11 unmodified lines
modified:src/proxy.js+1 added-1 removed
134 unmodified lines
135 try {136 // create proxy for root137 const rootClone = createProxy(undefined, baseState)138 // execute the thunk139 verifyReturnValue(producer(rootClone))139 verifyReturnValue(producer.call(rootClone, rootClone))140 // and finalize the modified proxy141 const res = finalize(rootClone)142 // revoke all proxies143 each(proxies, (_, p) => p.revoke())
5 unmodified lines