Skip to content

Instantly share code, notes, and snippets.

@dedels
Created October 10, 2018 13:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dedels/7aba57738f28722b86a104024c70a3ac to your computer and use it in GitHub Desktop.
Save dedels/7aba57738f28722b86a104024c70a3ac 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