Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make applyPatches to accept readonly Patch[] #1094

Merged
merged 3 commits into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,9 @@ function runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener) {
if (canReferNonEnumerableProperty) s.foo.a++
if (useStrictShallowCopy) expect(isEnumerable(s, "foo")).toBeFalsy()
})
if (canReferNonEnumerableProperty) expect(nextState.foo).toBeTruthy()
if (canReferNonEnumerableProperty) {
expect(nextState.foo).toEqual({a: 2})
}
if (useStrictShallowCopy)
expect(isEnumerable(nextState, "foo")).toBeFalsy()
if (useStrictShallowCopy) expect(nextState.baz).toBeTruthy()
Expand Down
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
2 changes: 1 addition & 1 deletion src/core/immerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,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
2 changes: 1 addition & 1 deletion src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export function enablePatches() {
})
}

function applyPatches_<T>(draft: T, patches: Patch[]): T {
function applyPatches_<T>(draft: T, patches: readonly Patch[]): T {
patches.forEach(patch => {
const {path, op} = patch

Expand Down
2 changes: 1 addition & 1 deletion src/utils/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const plugins: {
patches: Patch[],
inversePatches: Patch[]
): void
applyPatches_<T>(draft: T, patches: Patch[]): T
applyPatches_<T>(draft: T, patches: readonly Patch[]): T
}
MapSet?: {
proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T
Expand Down