Skip to content

Commit

Permalink
Merge pull request #2 from matthewp/ca
Browse files Browse the repository at this point in the history
Combine the mutation observers into one
  • Loading branch information
matthewp authored Sep 22, 2016
2 parents 60acb2a + 5a47f78 commit d6ccf6f
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions attr.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
(function(){
var forEach = Array.prototype.forEach;

class CustomAttributeRegistry {
Expand Down Expand Up @@ -31,35 +32,31 @@ class CustomAttributeRegistry {
var customAttributes = this;
var document = this.ownerDocument;
var root = document.documentElement;
var downgrade = this._downgrade.bind(this);
var upgrade = this._upgradeElement.bind(this);

this.attrMO = new MutationObserver(function(mutations){
this.observer = new MutationObserver(function(mutations){
forEach.call(mutations, function(m){
var attr = customAttributes._getConstructor(m.attributeName);
if(attr) {
customAttributes._found(m.attributeName, m.target, m.oldValue);
if(m.type === 'attributes') {
var attr = customAttributes._getConstructor(m.attributeName);
if(attr) {
customAttributes._found(m.attributeName, m.target, m.oldValue);
}
}
// chlidList
else {
forEach.call(m.removedNodes, downgrade);
forEach.call(m.addedNodes, upgrade);
}
});
});

this.attrMO.observe(root, {
this.observer.observe(root, {
childList: true,
subtree: true,
attributes: true,
attributeOldValue: true
});

this.childMO = new MutationObserver(function(mutations){
var downgrade = customAttributes._downgrade.bind(customAttributes);
var upgrade = customAttributes._upgradeElement.bind(customAttributes);
forEach.call(mutations, function(m){
forEach.call(m.removedNodes, downgrade);
forEach.call(m.addedNodes, upgrade)
});
});

this.childMO.observe(root, {
childList: true,
subtree: true
});
}

_upgradeAttr(attrName) {
Expand Down Expand Up @@ -134,3 +131,4 @@ class CustomAttributeRegistry {
}

window.customAttributes = new CustomAttributeRegistry(document);
})();

0 comments on commit d6ccf6f

Please sign in to comment.