-
-
Save bryanhelmig/ed948bb9858a75520a104bd80905d0d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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