Skip to content

Instantly share code, notes, and snippets.

@bryanhelmig
Forked from dedels/immer-patches.js
Created October 23, 2020 20:52
Show Gist options
  • Save bryanhelmig/ed948bb9858a75520a104bd80905d0d6 to your computer and use it in GitHub Desktop.
Save bryanhelmig/ed948bb9858a75520a104bd80905d0d6 to your computer and use it in GitHub Desktop.
var immer = require("immer");
var produce = immer.produce;
var patches = [];
var state0 = { a: 1 };
var state1 = produce(
state0,
function (draft) {
draft.b = 9;
},
function (p) {
patches.push(...p);
}
);
var state2 = produce(
state1,
function (draft) {
draft.a = 3;
},
function (p) {
patches.push(...p);
}
);
var state3 = produce(
state2,
function (draft) {
draft.b = 99;
},
function (p) {
patches.push(...p);
}
);
var state4 = produce(
state3,
function (draft) {
draft.a = 5;
},
function (p) {
patches.push(...p);
}
);
console.log(patches);
//Array (4 items)
//0: Object {op: "add", path: , value: 9}
//1: Object {op: "replace", path: , value: 3}
//2: Object {op: "replace", path: , value: 99}
//3: Object {op: "replace", path: , value: 5}
var applyPatches = immer.applyPatches;
produce(
state0,
function (draft) {
applyPatches(draft, patches);
},
function (p) {
patches = p;
}
);
console.log(patches);
//Array (2 items)
//0: Object {op: "add", path: , value: 99}
//1: Object {op: "replace", path: , value: 5}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment