Process iterator by a common utility

This commit is contained in:
Piro / YUKI Hiroshi 2013-10-30 02:11:31 +09:00
parent c10e126000
commit 037ed95977

View File

@ -171,13 +171,11 @@ var TreeStyleTabBookmarksServiceEditable = {
this.menulist.value = (selected || this.blankItem).getAttribute('value'); this.menulist.value = (selected || this.blankItem).getAttribute('value');
}).bind(this)) }).bind(this))
}, },
_createSiblingsFragment : function TSTBMEditable__createSiblingsFragment(aCurrentItem, aCallback) _doProgressively : function TSTBMEditable__doProgressively(aParams)
{ {
var itemsIterator = this._getSiblingItemsIterator(aCurrentItem); var name = aParams.name;
var items = []; if (this._doProgressivelyTimers[name])
window.clearTimeout(this._doProgressivelyTimers[name]);
if (this._createSiblingsFragmentTimer)
window.clearTimeout(this._createSiblingsFragmentTimer);
var interval = 100; var interval = 100;
var step = 10; var step = 10;
@ -185,24 +183,39 @@ var TreeStyleTabBookmarksServiceEditable = {
try { try {
for (let i = 0; i < step; i++) for (let i = 0; i < step; i++)
{ {
items.push(itemsIterator.next()); aParams.onProgress();
} }
this._createSiblingsFragmentTimer = window.setTimeout(progressiveIteration, interval); this._doProgressivelyTimers[name] = window.setTimeout(progressiveIteration, interval);
} }
catch(e if e instanceof StopIteration) { catch(e if e instanceof StopIteration) {
aParams.onComplete();
}
catch(e) {
Components.utils.reportError(e);
}
finally {
this._doProgressivelyTimers[name] = null;
}
}).bind(this);
this._doProgressivelyTimers[name] = window.setTimeout(progressiveIteration, interval);
},
_doProgressivelyTimers : {},
_createSiblingsFragment : function TSTBMEditable__createSiblingsFragment(aCurrentItem, aCallback)
{
var itemsIterator = this._getSiblingItemsIterator(aCurrentItem);
var items = [];
this._doProgressively({
name : '_createSiblingsFragment',
onProgress : function() {
items.push(itemsIterator.next());
},
onComplete : (function() {
this._createSiblingsFragmentInternal(aCurrentItem, items, function(aSiblingsFragment) { this._createSiblingsFragmentInternal(aCurrentItem, items, function(aSiblingsFragment) {
aCallback(aSiblingsFragment); aCallback(aSiblingsFragment);
}); });
} }).bind(this)
catch(e) { });
}
finally {
this._createSiblingsFragmentTimer = null;
}
}).bind(this);
this._createSiblingsFragmentTimer = window.setTimeout(progressiveIteration, interval);
}, },
_createSiblingsFragmentTimer : null,
_createSiblingsFragmentInternal : function TSTBMEditable_createSiblingsFragmentInternal(aCurrentItem, aItems, aCallback) _createSiblingsFragmentInternal : function TSTBMEditable_createSiblingsFragmentInternal(aCurrentItem, aItems, aCallback)
{ {
var treeStructure = this.getTreeStructureFromItems(aItems); var treeStructure = this.getTreeStructureFromItems(aItems);
@ -217,25 +230,17 @@ var TreeStyleTabBookmarksServiceEditable = {
var fragment = document.createDocumentFragment(); var fragment = document.createDocumentFragment();
if (this._createSiblingsFragmentInternalTimer) var itemsIterator = Iterator(aItems);
window.clearTimeout(this._createSiblingsFragmentInternalTimer); this._doProgressively({
name : '_createSiblingsFragment',
var interval = 100; onProgress : (function() {
var step = 10; let [index, id] = itemsIterator.next();
var current = 0;
var progressiveIteration = (function() {
let id;
for (let i = 0; i < step; i++)
{
id = aItems[current++];
if (!id)
break;
let label = PlacesUtils.bookmarks.getItemTitle(id); let label = PlacesUtils.bookmarks.getItemTitle(id);
let menuitem = document.createElement('menuitem'); let menuitem = document.createElement('menuitem');
menuitem.setAttribute('value', id); menuitem.setAttribute('value', id);
let parent = i; let parent = index;
let nest = 0; let nest = 0;
let disabled = false; let disabled = false;
while ((parent = treeStructure[parent]) != -1) while ((parent = treeStructure[parent]) != -1)
@ -257,17 +262,12 @@ var TreeStyleTabBookmarksServiceEditable = {
menuitem.setAttribute('label', label); menuitem.setAttribute('label', label);
fragment.appendChild(menuitem); fragment.appendChild(menuitem);
}).bind(this),
onComplete : function() {
aCallback(fragment);
} }
if (id) { });
this._createSiblingsFragmentInternalTimer = window.setTimeout(progressiveIteration, interval);
return;
}
this._createSiblingsFragmentInternalTimer = null;
aCallback(fragment);
}).bind(this);
this._createSiblingsFragmentInternalTimer = window.setTimeout(progressiveIteration, interval);
}, },
_createSiblingsFragmentInternalTimer : null,
_getItemsInFolderIterator : function TSTBMEditable_getItemsInFolderIterator(aId) _getItemsInFolderIterator : function TSTBMEditable_getItemsInFolderIterator(aId)
{ {
var count = 0; var count = 0;
@ -300,24 +300,15 @@ var TreeStyleTabBookmarksServiceEditable = {
var itemsIterator = this._getSiblingItemsIterator(aId); var itemsIterator = this._getSiblingItemsIterator(aId);
var items = []; var items = [];
this._doProgressively({
var interval = 100; name : '_createSiblingsFragment',
var step = 10; onProgress : function() {
var progressiveIteration = (function() { items.push(itemsIterator.next());
try { },
for (let i = 0; i < step; i++) onComplete : (function() {
{
items.push(itemsIterator.next());
}
window.setTimeout(progressiveIteration, interval);
}
catch(e if e instanceof StopIteration) {
this._saveParentForInternal(aId, newParentId, items); this._saveParentForInternal(aId, newParentId, items);
} }).bind(this)
catch(e) { });
}
}).bind(this);
window.setTimeout(progressiveIteration, interval);
}, },
_saveParentForInternal : function TSTBMEditable_saveParentForInternal(aId, aNewParentId, aItems) _saveParentForInternal : function TSTBMEditable_saveParentForInternal(aId, aNewParentId, aItems)
{ {