Skip to content

Commit ddb2b6d

Browse files
author
ansuz
committed
add pre and post diff hooks with defaults
1 parent 268f752 commit ddb2b6d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

diffDOM.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,13 @@
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

@@ -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+
this.preVirtualDiffApply(info);
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+
this.preDiffApply(info);
1160+
11351161
switch (diff.action) {
11361162
case 'addAttribute':
11371163
if (!node || !node.setAttribute) {
@@ -1223,6 +1249,10 @@
12231249
console.log('unknown action');
12241250
}
12251251

1252+
// if a new node was created, we might be interested in it
1253+
info.newNode = newNode;
1254+
this.postDiffApply(info);
1255+
12261256
return true;
12271257
},
12281258

0 commit comments

Comments
 (0)