Skip to content

Commit

Permalink
add observeNodes tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Sep 1, 2015
1 parent c09296e commit bd90b57
Show file tree
Hide file tree
Showing 4 changed files with 512 additions and 43 deletions.
23 changes: 10 additions & 13 deletions src/lib/dom-api-mutation-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@
return h;
},

_ensureSetup: function(options) {
this._describeChanges = options && options.changes;
},
_ensureSetup: function(options) {},

notify: function() {
if (this._hasListeners()) {
Expand All @@ -41,7 +39,7 @@
},

_notify: function() {
var info = this._describeChanges ? this._calcChanges() : {};
var info = this._suspendChangeInfo ? {} : this._calcChanges();
if (info) {
info.target = this.node;
this._callListeners(info);
Expand Down Expand Up @@ -79,19 +77,18 @@

if (Settings.useShadow) {

var ensureSetup = DomApi.MutationContent.prototype._ensureSetup;

Polymer.Base.extend(DomApi.MutationContent.prototype, {

_ensureSetup: function(options) {
ensureSetup.call(this, options);
this._trackAttributes = this._trackAttributes ||
options && options.attributes;
var root = this.domApi.getOwnerRoot();
var host = root && root.host;
if (host) {
this._observer = Polymer.dom(host).observeNodes(
this.notify.bind(this), {attributes: this._trackAttributes});
options.attributes;
if (!this._observer) {
var root = this.domApi.getOwnerRoot();
var host = root && root.host;
if (host) {
this._observer = Polymer.dom(host).observeNodes(
this.notify.bind(this), {attributes: this._trackAttributes});
}
}
},

Expand Down
64 changes: 34 additions & 30 deletions src/lib/dom-api-mutation.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
DomApi.Mutation.prototype = {

addListener: function(callback, options) {
options = options || {};
this._ensureSetup(options);
return this._listeners.push({fn: callback, options: options});
var listener = {fn: callback, options: options};
this._notifyInitial(listener);
return this._listeners.push(listener);
},

removeListener: function(handle) {
Expand All @@ -39,7 +42,10 @@
},

_ensureSetup: function(options) {
this._observeContentElements(this.domApi.childNodes);
if (!this._isSetup) {
this._isSetup = true;
this._observeContentElements(this.domApi.childNodes);
}
},

_ensureCleanup: function() {}, // abstract
Expand All @@ -62,8 +68,8 @@
}
},

addAllNodes: function() {
if (this._hasListeners()) {
addAllNodes: function(force) {
if (this._hasListeners() || force) {
var c$ = this.domApi.childNodes;
if (c$.length) {
for (var i=0, c; (i < c$.length) && (c=c$[i]); i++) {
Expand Down Expand Up @@ -93,6 +99,15 @@
this._removedNodes = [];
},

_notifyInitial: function(listener) {
var info = {
target: this.node,
addedNodes: this.domApi.childNodes,
removedNodes: []
};
this._callListener(listener, info);
},

_updateContentElements: function(info) {
this._observeContentElements(info.addedNodes);
this._unobserveContentElements(info.removedNodes);
Expand All @@ -111,9 +126,7 @@
},

_observeContent: function(content) {
return Polymer.dom(content).observeNodes(this._notify.bind(this), {
changes: true
});
return Polymer.dom(content).observeNodes(this._notify.bind(this));
},

_unobserveContentElements: function(elements) {
Expand All @@ -136,42 +149,34 @@
var o$ = this._listeners;
for (var i=0, o; (i < o$.length) && (o=o$[i]); i++) {
if (!filter || filter(o.options)) {
o.fn.call(this.node, info);
this._callListener(o, info);
}
}
},

_callListener: function(listener, info) {
return listener.fn.call(this.node, info);
}

};

if (Settings.useShadow) {

var ensureSetup = DomApi.Mutation.prototype._ensureSetup;

Polymer.Base.extend(DomApi.Mutation.prototype, {

_ensureSetup: function(options) {
this._trackAttributes = this._trackAttributes ||
options && options.attributes;
options.attributes;
if (!this._observer) {
this._observer =
new MutationObserver(this._notify.bind(this));
// make sure to notify initial state...
this._debouncer = Polymer.Debounce(this._debouncer,
function() {
this._notify([{
target: this.node,
addedNodes: this.domApi.childNodes.slice()
}]);
}
);
this._debouncer.context = this;
Polymer.dom.addDebouncer(this._debouncer);
this._observer = new MutationObserver(this._notify.bind(this));
this._preflush = this._flush.bind(this);

}
if (!this._hasListeners()) {
Polymer.dom.addPreflush(this._preflush);
// note: doing this > 1x is a no-op
this._observer.observe(this.node, {childList: true});
Polymer.dom.addPreflush(this._preflush);
// note: doing this > 1x is a no-op
this._observer.observe(this.node, {childList: true});
}
ensureSetup.call(this, options);
},

_ensureCleanup: function() {
Expand Down Expand Up @@ -221,7 +226,6 @@

_observeContent: function(content) {
return Polymer.dom(content).observeNodes(this._notify.bind(this), {
changes: true,
attributes: this._trackAttributes
});
},
Expand All @@ -246,7 +250,7 @@
this._attrObservers.set(element, new MutationObserver(
function(mxns) {
self._callListeners(mxns, function(options) {
return Boolean(options && options.attributes);
return Boolean(options.attributes);
});
}
));
Expand Down
1 change: 1 addition & 0 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'unit/polymer-dom.html',
'unit/polymer-dom-shadow.html',
'unit/polymer-dom-content.html',
'unit/polymer-dom-observeNodes.html',
'unit/bind.html',
'unit/bind.html?dom=shadow',
'unit/notify-path.html',
Expand Down
Loading

0 comments on commit bd90b57

Please sign in to comment.