Immer produce with patches and inverse patches: working versions (tested 2026-10-15)
Update immutable state with Immer produce, capture patches and inversePatches for undo, and applyPatches to replay edits in tests.
passed1 passed · 0 failed2026-10-15 16:01 UTC- Status
- passed
- Last passed
- 2026-10-15 16:01 UTC
- Tests
- 1 / 1
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
immer@11.1.21@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38764699834
The code
Exactly the files that passed. Your agent can pull them with get_recipe("immer-patches-history") (install), or see them on GitHub.
package.json
{
"name": "immer-patches-history",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"immer": "11.1.21"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/store.ts
import { applyPatches, enablePatches, produceWithPatches, type Patch } from "immer";
enablePatches();
export type Todo = { id: number; text: string; done: boolean };
export type State = { todos: Todo[]; nextId: number };
export function createState(): State {
return { todos: [], nextId: 1 };
}
export function addTodo(state: State, text: string) {
return produceWithPatches(state, (draft) => {
draft.todos.push({ id: draft.nextId++, text, done: false });
});
}
export function toggleTodo(state: State, id: number) {
return produceWithPatches(state, (draft) => {
const t = draft.todos.find((x) => x.id === id);
if (t) t.done = !t.done;
});
}
export function undo(state: State, inverse: Patch[]) {
return applyPatches(state, inverse);
}
export function redo(state: State, patches: Patch[]) {
return applyPatches(state, patches);
}
test/store.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { addTodo, createState, redo, toggleTodo, undo } from "../src/store.ts";
test("produceWithPatches supports undo and redo", () => {
let state = createState();
const [s1, patches, inverse] = addTodo(state, "write recipe");
state = s1;
assert.equal(state.todos.length, 1);
assert.equal(state.todos[0].done, false);
const [s2, p2, i2] = toggleTodo(state, 1);
state = s2;
assert.equal(state.todos[0].done, true);
state = undo(state, i2);
assert.equal(state.todos[0].done, false);
state = redo(state, p2);
assert.equal(state.todos[0].done, true);
state = undo(state, inverse);
assert.equal(state.todos.length, 0);
state = redo(state, patches);
assert.equal(state.todos[0].text, "write recipe");
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.