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
Provenance
- Commit
3a9d6d1- Tree
65373be- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@gmail.com>- Parent
c8b61f6
5 files changed+28 added−9 removed
modified:__tests__/base.js+20 added-1 removed
701 unmodified lines702 )703 })704 })705 706- 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 lines722 expect(Reflect.has(draft.anArray, "17")).toBe(false)723 })724 })725 726+ 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+ })734+735+ 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+ })744+745 afterEach(() => {746 expect(baseState).toBe(origBaseState)747 expect(baseState).toEqual(createBaseState())748 })21 unmodified linesmodified:src/es5.js+1 added-1 removed
136 unmodified linesExpand all13713813914014114114214314414546 unmodified lines136 unmodified linesExpand all try { // create proxy for root const rootClone = createProxy(undefined, baseState) // execute the thunk verifyReturnValue(producer(rootClone)) verifyReturnValue(producer.call(rootClone, rootClone)) // and finalize the modified proxy each(states, (_, state) => { state.finalizing = true })46 unmodified linesmodified:src/immer.d.ts+5 added-5 removed
10 unmodified lines11 * @returns The next state: a new state, or the current state if nothing was modified12 */13 export default function<S = any>(14 currentState: S,15- recipe: (draftState: S) => void15+ recipe: (this: S, draftState: S) => void16 ): S17 // curried invocations18 export 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) => S21 export 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) => S24 export default function<S = any, A = any>(25- recipe: (draftState: S, a: A) => void25+ recipe: (this: S, draftState: S, a: A) => void26 ): (currentState: S) => S27 export default function<S = any>(28- recipe: (draftState: S, ...extraArgs: any[]) => void28+ recipe: (this: S, draftState: S, ...extraArgs: any[]) => void29 ): (currentState: S, ...extraArgs: any[]) => S30 31 /**32 * Automatically freezes any state trees generated by immer.11 unmodified linesmodified:src/index.js+1 added-1 removed
22 unmodified lines23 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 linesmodified:src/proxy.js+1 added-1 removed
134 unmodified lines135 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