Skip to content

Commit 6fdb82c

Browse files
committed
Merge pull request #37 from ansuz/gh-pages
diff hooks
2 parents 268f752 + 8b4f1c9 commit 6fdb82c

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,41 @@ dd = new diffDOM({
9797
});
9898
```
9999

100+
#### Pre and post diff hooks
101+
102+
diffDOM provides extension points before and after virtual and actual diffs, exposing some of the internals of the diff algorithm, and allowing you to make additional decisions based on that information.
103+
104+
```
105+
dd = new diffDOM({
106+
preVirtualDiffApply: function (info) {
107+
console.log(info);
108+
},
109+
postVirtualDiffApply: function (info) {
110+
console.log(info);
111+
},
112+
preDiffApply: function (info) {
113+
console.log(info);
114+
},
115+
postDiffApply: function (info) {
116+
console.log(info);
117+
}
118+
});
119+
```
120+
121+
Additionally, the _pre_ hooks allow you to shortcircuit the standard behaviour of the diff by returning 'true' from this callback. This will cause the diffApply functions to return prematurely, skipping their standard behaviour.
122+
123+
```
124+
dd = new diffDOM({
125+
// prevent removal of attributes
126+
preDiffApply: function (info) {
127+
if (info.diff.action === 'removeAttribute') {
128+
console.log("preventing attribute removal");
129+
return true;
130+
}
131+
}
132+
});
133+
```
134+
100135
#### Debugging
101136

102137
For debugging you might want to set a max number of diff changes between two elements before diffDOM gives up. To allow for a maximum of 500 differences between elements when diffing, initialize diffDOM like this:

diffDOM.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,16 +438,22 @@
438438
textDiff: function() {
439439
arguments[0].data = arguments[3];
440440
return;
441-
}
441+
},
442+
// empty functions were benchmarked as running faster than both
443+
// `f && f()` and `if (f) { f(); }`
444+
preVirtualDiffApply: function () {},
445+
postVirtualDiffApply: function () {},
446+
preDiffApply: function () {},
447+
postDiffApply: function () {}
442448
},
443449
i;
444450

445-
if (typeof options == "undefined") {
451+
if (typeof options === "undefined") {
446452
options = {};
447453
}
448454

449455
for (i in defaults) {
450-
if (typeof options[i] == "undefined") {
456+
if (typeof options[i] === "undefined") {
451457
this[i] = defaults[i];
452458
} else {
453459
this[i] = options[i];
@@ -971,6 +977,14 @@
971977
nodeIndex = routeInfo.nodeIndex,
972978
newNode, route, c;
973979

980+
// pre-diff hook
981+
var info = {
982+
diff: diff,
983+
node: node
984+
};
985+
986+
if (this.preVirtualDiffApply(info)) { return true; }
987+
974988
switch (diff.action) {
975989
case 'addAttribute':
976990
if (!node.attributes) {
@@ -1095,6 +1109,10 @@
10951109
console.log('unknown action');
10961110
}
10971111

1112+
// capture newNode for the callback
1113+
info.newNode = newNode;
1114+
this.postVirtualDiffApply(info);
1115+
10981116
return;
10991117
},
11001118

@@ -1132,6 +1150,14 @@
11321150
var node = this.getFromRoute(tree, diff.route),
11331151
newNode, reference, route, c;
11341152

1153+
// pre-diff hook
1154+
var info = {
1155+
diff: diff,
1156+
node: node
1157+
};
1158+
1159+
if (this.preDiffApply(info)) { return true; }
1160+
11351161
switch (diff.action) {
11361162
case 'addAttribute':
11371163
if (!node || !node.setAttribute) {
@@ -1223,6 +1249,11 @@
12231249
console.log('unknown action');
12241250
}
12251251

1252+
// if a new node was created, we might be interested in it
1253+
// post diff hook
1254+
info.newNode = newNode;
1255+
this.postDiffApply(info);
1256+
12261257
return true;
12271258
},
12281259

0 commit comments

Comments
 (0)