Skip to content

Commit

Permalink
Make Patch[] arrays readonly on the outer bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Feb 27, 2024
1 parent f6736a4 commit a125a1e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/core/immerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,15 @@ export class Immer implements ProducersFns {
this.produceWithPatches(state, (draft: any) => base(draft, ...args))
}

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

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
7 changes: 5 additions & 2 deletions src/types/types-external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export interface Patch {
value?: any
}

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

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

type ValidRecipeReturnType<State> =
| State
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

0 comments on commit a125a1e

Please sign in to comment.