
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
dendriform-immer-patch-optimiser
Advanced tools
Dendriform uses Immer. Immer is wonderful, especially its ability to produce patches for implementing concurrent editing and undo & redo functionality.
But Dendriform needs to track how array elements are moved around. Luckily the RFC6902 JSON patch standard that Immer's patches are similar to describes "move" operations. Immer does not produce "move" patches, so this plugin post-processes Immer patches to infer array element movement and produce "move" patches.
When using Immer alone, certain types of changes can lead to a large number of patches because Immer's design optimises for good performance at generation time. A nice side effect of this library is that describing movements naturally reduces the number of patches.
Please note that while this library does some immer patch optimisations that could be used in any project, the main goal of this library is to solve problems for Dendriform. As such, it's API may be a little volatile, and it won't necessarily aim to please the use cases of the general public. You are welcome to use the library as is, copy the source code for your own uses, or even republish the code as a new library if you'd like to take on the responsibility of maintaining a more public-serving variant of it.
import {produceWithPatches} from 'immer';
import {optimise} from 'dendriform-immer-patch-optimiser';
// enablePatches() is already called by dendriform-immer-patch-optimiser
const base = {foo: ['a','b','c']};
const [result, recordedPatches] = produceWithPatches(base, draft => {
draft.foo.unshift('d');
});
// result:
// ['d','a','b','c']
// recordedPatches:
// {op: 'replace', path: ['foo', 0], value: 'd'},
// {op: 'replace', path: ['foo', 1], value: 'a'},
// {op: 'replace', path: ['foo', 2], value: 'b'},
// {op: 'add', path: ['foo', 3], value: 'c'}
const optimisedPatches = optimise(base, recordedPatches);
// optimisedPatches:
// {op: 'add', path: ['foo', 0], value: 'd'}
import {produceWithPatches} from 'immer';
import {optimise, applyPatches} from 'dendriform-immer-patch-optimiser';
// enablePatches() is already called by dendriform-immer-patch-optimiser
const base = {foo: ['a','b','c']};
const [result, recordedPatches] = produceWithPatches(base, draft => {
draft.foo.shift();
});
// result:
// ['b','c']
// recordedPatches:
// {op: 'replace', path: ['foo', 0], value: 'b'},
// {op: 'replace', path: ['foo', 1], value: 'c'},
// {op: 'replace', path: ['foo', 'length'], value: 2}
const optimisedPatches = optimise(base, recordedPatches);
// optimisedPatches:
// {op: 'move', from: ['foo', 1], path: ['foo', 0]},
// {op: 'move', from: ['foo', 2], path: ['foo', 1]},
// {op: 'replace', path: ['foo', 'length'], value: 2}
// Immer doesn't understand "move" patches, so use applyPatches() exported from dendriform-immer-patch-optimiser
const result2 = applyPatches(base, optimisedPatches);
// result2 is the same as result
import {produceWithPatches} from 'immer';
import {optimise, applyPatches} from 'dendriform-immer-patch-optimiser';
// enablePatches() is already called by dendriform-immer-patch-optimiser
const base = ['a','b','X','Y','c','d','e','Z'];
const [result, recordedPatches] = produceWithPatches(base, draft => {
draft.splice(2,2);
draft.pop();
draft.reverse();
draft.push('C');
draft.splice(3, 0, 'B');
draft.unshift('A');
});
// result:
// ['A','e','d','c','B','b','a','C']
// recordedPatches:
// {op: 'replace', path: [0], value: 'A'},
// {op: 'replace', path: [1], value: 'e'},
// {op: 'replace', path: [2], value: 'd'},
// {op: 'replace', path: [3], value: 'c'},
// {op: 'replace', path: [4], value: 'B'},
// {op: 'replace', path: [5], value: 'b'},
// {op: 'replace', path: [6], value: 'a'},
// {op: 'replace', path: [7], value: 'C'}
const optimisedPatches = optimise(base, recordedPatches);
// optimisedPatches:
// {op: 'move', from: [6], path: [0]},
// {op: 'move', from: [6], path: [1]},
// {op: 'move', from: [6], path: [2]},
// {op: 'move', from: [4], path: [3]},
// {op: 'replace', path: ['length'], value: 5},
// {op: 'add', path: [0], value: 'A'},
// {op: 'add', path: [4], value: 'B'},
// {op: 'add', path: [7], value: 'C'}
// Immer doesn't understand "move" patches, so use applyPatches() exported from dendriform-immer-patch-optimiser
const result2 = applyPatches(base, optimisedPatches);
// result2 is the same as result
FAQs
Immer patch optimisation to add "move" operations.
We found that dendriform-immer-patch-optimiser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.