Skip to content

Commit

Permalink
More robust check before eliminating observers
Browse files Browse the repository at this point in the history
Check to ensure all rows have really been removed before eliminating
observers. Also provide better tolerance of missing rows.
  • Loading branch information
kriszyp committed Jan 10, 2014
1 parent 0d3278d commit 4b8ff49
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 9 additions & 3 deletions List.js
Expand Up @@ -462,7 +462,7 @@ function(kernel, declare, listen, has, miscUtil, TouchScroll, hasClass, put){
if(results.observe){ if(results.observe){
// observe the results for changes // observe the results for changes
self._numObservers++; self._numObservers++;
observerIndex = observers.push(results.observe(function(object, from, to){ var observer = results.observe(function(object, from, to){
var row, firstRow, nextNode, parentNode; var row, firstRow, nextNode, parentNode;


function advanceNext() { function advanceNext() {
Expand Down Expand Up @@ -504,6 +504,7 @@ function(kernel, declare, listen, has, miscUtil, TouchScroll, hasClass, put){
// result from our own array, so go from the previous row and advance one // result from our own array, so go from the previous row and advance one
nextNode = rows[to - 1]; nextNode = rows[to - 1];
if(nextNode){ if(nextNode){
nextNode = correctElement(nextNode);
// Make sure to skip connected nodes, so we don't accidentally // Make sure to skip connected nodes, so we don't accidentally
// insert a row in between a parent and its children. // insert a row in between a parent and its children.
advanceNext(); advanceNext();
Expand Down Expand Up @@ -551,7 +552,8 @@ function(kernel, declare, listen, has, miscUtil, TouchScroll, hasClass, put){


from != to && firstRow && self.adjustRowIndices(firstRow); from != to && firstRow && self.adjustRowIndices(firstRow);
self._onNotification(rows, object, from, to); self._onNotification(rows, object, from, to);
}, true)) - 1; }, true);
observerIndex = observers.push(observer) - 1;
} }
var rowsFragment = document.createDocumentFragment(), var rowsFragment = document.createDocumentFragment(),
lastRow; lastRow;
Expand All @@ -578,7 +580,8 @@ function(kernel, declare, listen, has, miscUtil, TouchScroll, hasClass, put){
// Make sure we have the correct row element // Make sure we have the correct row element
// (not one that was previously removed) // (not one that was previously removed)
lastRow = correctElement(lastRow); lastRow = correctElement(lastRow);
var row = self[top ? "up" : "down"](self.row(lastRow)); var row = self.row(lastRow);
row = row && self[top ? "up" : "down"](row);
if(row && row.element != lastRow){ if(row && row.element != lastRow){
var method = top ? "unshift" : "push"; var method = top ? "unshift" : "push";
// Take the row and data from the adjacent page and unshift to the // Take the row and data from the adjacent page and unshift to the
Expand Down Expand Up @@ -632,6 +635,9 @@ function(kernel, declare, listen, has, miscUtil, TouchScroll, hasClass, put){
whenError(); whenError();
} }
rows = resolvedRows; rows = resolvedRows;
if(observer){
observer.rows = rows;
}
} }


// Now render the results // Now render the results
Expand Down
9 changes: 9 additions & 0 deletions OnDemandList.js
Expand Up @@ -619,6 +619,15 @@ return declare([List, _StoreMixin], {
var observers = this.observers; var observers = this.observers;
var observer = observers[thisIndex]; var observer = observers[thisIndex];
if(observer){ if(observer){
// first we need to verify that all the rows really have been removed. If there
// are overlapping rows, it is possible another element exists
var rows = observer.rows;
for(var i = 0; i < rows.length; i++){
if(rows[i] != rowElement && rows[i].offsetParent){
// still rows in this list, abandon
return this.inherited(arguments);
}
}
observer.cancel(); observer.cancel();
this._numObservers--; this._numObservers--;
observers[thisIndex] = 0; // remove it so we don't call cancel twice observers[thisIndex] = 0; // remove it so we don't call cancel twice
Expand Down

0 comments on commit 4b8ff49

Please sign in to comment.