Skip to content

Commit 6771161

Browse files
committed
Improve code comments of performance improvements.
1 parent e1fbf34 commit 6771161

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

jquery.initialize.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313

1414
"use strict";
1515

16-
// List of mutation types that are observable.
17-
var mtypes = ['childList', 'attributes'];
18-
1916
var combinators = [' ', '>', '+', '~']; // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors#Combinators
20-
var fraternisers = ['+', '~'];
21-
var complexTypes = ['ATTR', 'PSEUDO', 'ID', 'CLASS'];
17+
var fraternisers = ['+', '~']; // These combinators involve siblings.
18+
var complexTypes = ['ATTR', 'PSEUDO', 'ID', 'CLASS']; // These selectors are based upon attributes.
2219

20+
// Understand what kind of selector the initializer is based upon.
2321
function grok(msobserver) {
2422
if (!$.find.tokenize) {
2523
// This is an old version of jQuery, so cannot parse the selector.
@@ -32,22 +30,21 @@
3230
return;
3331
}
3432

33+
// Parse the selector.
3534
msobserver.isCombinatorial = false;
3635
msobserver.isFraternal = false;
3736
msobserver.isComplex = false;
38-
39-
// Search for combinators.
4037
let token = $.find.tokenize(msobserver.selector);
4138
for (let i = 0; i < token.length; i++) {
4239
for (let j = 0; j < token[i].length; j++) {
4340
if (combinators.indexOf(token[i][j].type) != -1)
44-
msobserver.isCombinatorial = true;
41+
msobserver.isCombinatorial = true; // This selector uses combinators.
4542

4643
if (fraternisers.indexOf(token[i][j].type) != -1)
47-
msobserver.isFraternal = true;
44+
msobserver.isFraternal = true; // This selector uses sibling combinators.
4845

4946
if (complexTypes.indexOf(token[i][j].type) != -1)
50-
msobserver.isComplex = true;
47+
msobserver.isComplex = true; // This selector is based on attributes.
5148
}
5249
}
5350
}
@@ -92,9 +89,6 @@
9289
// For each mutation.
9390
for (var m = 0; m < mutations.length; m++) {
9491

95-
// Do we observe this mutation type?
96-
if (mtypes.indexOf(mutations[m].type) == -1) continue;
97-
9892
// If this is an attributes mutation, then the target is the node upon which the mutation occurred.
9993
if (mutations[m].type == 'attributes') {
10094
// Check if the mutated node matchs.
@@ -108,7 +102,10 @@
108102
// If the selector is combinatorial, query descendants of the mutated node for matches.
109103
else if (msobserver.isCombinatorial)
110104
mutations[m].target.querySelectorAll(msobserver.selector).forEach(push);
111-
} else if (mutations[m].type == 'childList') {
105+
}
106+
107+
// If this is an childList mutation, then inspect added nodes.
108+
if (mutations[m].type == 'childList') {
112109

113110
// Search added nodes for matching selectors.
114111
for (var n = 0; n < mutations[m].addedNodes.length; n++) {
@@ -129,6 +126,7 @@
129126
}
130127
}
131128

129+
// For each match, call the callback using jQuery.each() to initialize the element (once only.)
132130
matches.forEach(function(match) {
133131
$(match).each(msobserver.callback);
134132
});
@@ -149,9 +147,10 @@
149147
msobservers.initialize(selector, callback, $.extend({}, $.initialize.defaults, options));
150148
};
151149

150+
// Options
152151
$.initialize.defaults = {
153-
target: document.documentElement, // Defaults observe the entire document.
154-
observer: null
152+
target: document.documentElement, // Defaults to observe the entire document.
153+
observer: null // MutationObserverInit: Defaults to internal configuration if not provided.
155154
}
156155

157156
})(jQuery);

0 commit comments

Comments
 (0)