Skip to content

Commit

Permalink
Limit the change to applyPatches
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Mar 10, 2024
1 parent c72a738 commit 677becc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
17 changes: 16 additions & 1 deletion __tests__/produce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
Immutable,
Immer,
enableMapSet,
enablePatches
enablePatches,
produceWithPatches
} from "../src/immer"

enableMapSet()
Expand Down Expand Up @@ -162,6 +163,20 @@ it("can apply patches", () => {
expect(applyPatches({}, patches)).toEqual({x: 4})
})

it("can apply readonly patches", () => {
const [, patches]: readonly [
{
x: number
},
readonly Patch[],
readonly Patch[]
] = produceWithPatches({x: 3}, d => {
d.x++
})

expect(applyPatches({}, patches)).toEqual({x: 4})
})

describe("curried producer", () => {
it("supports rest parameters", () => {
type State = {readonly a: 1}
Expand Down
16 changes: 6 additions & 10 deletions src/core/immerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,11 @@ export class Immer implements ProducersFns {
this.produceWithPatches(state, (draft: any) => base(draft, ...args))
}

let patches: readonly Patch[], inversePatches: readonly Patch[]
const result = this.produce(
base,
recipe,
(p: readonly Patch[], ip: readonly Patch[]) => {
patches = p
inversePatches = ip
}
)
let patches: Patch[], inversePatches: Patch[]
const result = this.produce(base, recipe, (p: Patch[], ip: Patch[]) => {
patches = p
inversePatches = ip
})
return [result, patches!, inversePatches!]
}

Expand Down Expand Up @@ -171,7 +167,7 @@ export class Immer implements ProducersFns {
this.useStrictShallowCopy_ = value
}

applyPatches<T extends Objectish>(base: T, patches: Patch[]): T {
applyPatches<T extends Objectish>(base: T, patches: readonly Patch[]): T {
// If a patch replaces the entire state, take that replacement as base
// before applying patches
let i: number
Expand Down
7 changes: 2 additions & 5 deletions src/types/types-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ export interface Patch {
value?: any
}

export type PatchListener = (
patches: readonly Patch[],
inversePatches: readonly Patch[]
) => void
export type PatchListener = (patches: Patch[], inversePatches: Patch[]) => void

/** Converts `nothing` into `undefined` */
type FromNothing<T> = T extends typeof NOTHING ? undefined : T
Expand All @@ -84,7 +81,7 @@ export type Produced<Base, Return> = Return extends void
/**
* Utility types
*/
type PatchesTuple<T> = readonly [T, readonly Patch[], readonly Patch[]]
type PatchesTuple<T> = readonly [T, Patch[], Patch[]]

type ValidRecipeReturnType<State> =
| State
Expand Down

0 comments on commit 677becc

Please sign in to comment.