Skip to content

Commit 2ffdd47

Browse files
author
Benjamín Eidelman
committed
Update plugins.md
1 parent f4fe18e commit 2ffdd47

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

docs/plugins.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,77 @@ Here is an example to provide number differences in deltas (when left and right
1919
This, way when diffing 2 numbers instead of obtaining ```[ oldValue, newValue ] ```, the difference between both values will be saved, this could be useful for counters simultaneously incremented in multiple client applications (patches that both increment a value would be combined, instead of failing with a conflict).
2020

2121
``` javascript
22+
23+
/*
24+
Plugin a new diff filter
25+
*/
26+
2227
var diffpatcher = jsondiffpatch.create();
2328
var NUMERIC_DIFFERENCE = -8;
2429

2530
var numericDiffFilter = function(context) {
2631
if (typeof context.left === 'number' && typeof context.right === 'number') {
27-
context.setResult([context.left, context.right - context.left, NUMERIC_DIFFERENCE]).exit();
32+
context.setResult([0, context.right - context.left, NUMERIC_DIFFERENCE]).exit();
2833
}
2934
};
3035
// a filterName is useful if I want to allow other filters to be inserted before/after this one
3136
numericDiffFilter.filterName = 'numeric';
3237

33-
// check the list of filters for the diff pipe
34-
var list = diffpatcher.processor.pipes.diff.list();
35-
assertSame(list, ["collectChildren", "trivial", "dates", "texts", "objects", "arrays"]);
38+
// to decide where to insert your filter you can look at the pipe's filter list
39+
assertSame(diffpatcher.processor.pipes.diff.list(),
40+
["collectChildren", "trivial", "dates", "texts", "objects", "arrays"]);
3641

3742
// insert my new filter, right before trivial one
3843
diffpatcher.processor.pipes.diff.before('trivial', numericDiffFilter);
3944

45+
// for debugging, log each filter
46+
diffpatcher.processor.pipes.diff.debug = true;
47+
4048
// try it
4149
var delta = diffpatcher.diff({ population: 400 }, { population: 403 });
42-
assertSame(delta, [400, 3, -8]);
43-
44-
```
50+
assertSame(delta, [0, 3, NUMERIC_DIFFERENCE]);
4551

46-
Now let's make the corresponding patch filter that will handle the new delta type
52+
/*
53+
Let's make the corresponding patch filter that will handle the new delta type
54+
*/
4755

48-
``` javascript
4956
var numericPatchFilter = function(context) {
5057
if (context.delta && Array.isArray(context.delta) && context.delta[2] === NUMERIC_DIFFERENCE) {
51-
console.log('A number diff!');
5258
context.setResult(context.left + context.delta[1]).exit();
5359
}
5460
};
55-
// a filterName is useful if I want to allow other filters to be inserted before/after this one
5661
numericPatchFilter.filterName = 'numeric';
57-
58-
// check the list of filters for the patch pipe
59-
var list = diffpatcher.processor.pipes.patch.list();
60-
assertSame(list, ["collectChildren", "arraysCollectChildren", "trivial", "texts", "objects", "arrays"]);
61-
62-
// insert my new filter, right before trivial one
6362
diffpatcher.processor.pipes.patch.before('trivial', numericPatchFilter);
6463

6564
// try it
6665
var right = diffpatcher.patch({ population: 400 }, delta);
6766
assertSame(right, { population: 403 });
6867

6968
// patch twice!
70-
var right = diffpatcher.patch(right, delta);
69+
right = diffpatcher.patch(right, delta);
7170
assertSame(right, { population: 406 });
71+
72+
/*
73+
To complete the plugin, let's add the reverse filter, so numeric deltas can be reversed
74+
(this is needed for unpatching too)
75+
*/
76+
77+
var numericReverseFilter = function(context) {
78+
if (context.nested) { return; }
79+
if (context.delta && Array.isArray(context.delta) && context.delta[2] === NUMERIC_DIFFERENCE) {
80+
context.setResult([0, -context.delta[1], NUMERIC_DIFFERENCE]).exit();
81+
}
82+
};
83+
numericReverseFilter.filterName = 'numeric';
84+
diffpatcher.processor.pipes.reverse.after('trivial', numericReverseFilter);
85+
86+
// try it
87+
var reverseDelta = diffpatcher.reverse(delta);
88+
assertSame(reverseDelta, [0, -3, NUMERIC_DIFFERENCE]);
89+
90+
// unpatch twice!
91+
right = diffpatcher.unpatch(right, delta);
92+
right = diffpatcher.unpatch(right, delta);
93+
assertSame(right, { population: 400 });
7294
```
95+

0 commit comments

Comments
 (0)