Bug 1353584 - replace setTimeout with requestIdleCallback in _delayedCapture and return early on about: page in _shouldCapture. r=mconley

MozReview-Commit-ID: DaAcOTFBY6Q
This commit is contained in:
Perry Jiang 2017-07-28 10:46:55 -07:00
parent aabbbd3f55
commit acd548d0b2
2 changed files with 34 additions and 12 deletions

View File

@ -88,7 +88,7 @@ var gBrowserThumbnails = {
this._delayedCapture(aEvent.target.linkedBrowser);
break;
case "TabClose": {
this._clearTimeout(aEvent.target.linkedBrowser);
this._cancelDelayedCapture(aEvent.target.linkedBrowser);
break;
}
}
@ -158,34 +158,56 @@ var gBrowserThumbnails = {
},
_delayedCapture: function Thumbnails_delayedCapture(aBrowser) {
if (this._timeouts.has(aBrowser))
clearTimeout(this._timeouts.get(aBrowser));
else
if (this._timeouts.has(aBrowser)) {
this._cancelDelayedCallbacks(aBrowser);
} else {
aBrowser.addEventListener("scroll", this, true);
}
let timeout = setTimeout(() => {
this._clearTimeout(aBrowser);
let idleCallback = () => {
this._cancelDelayedCapture(aBrowser);
this._capture(aBrowser);
};
// setTimeout to set a guarantee lower bound for the requestIdleCallback
// (and therefore the delayed capture)
let timeoutId = setTimeout(() => {
let idleCallbackId = requestIdleCallback(idleCallback, {
timeout: this._captureDelayMS * 30
});
this._timeouts.set(aBrowser, { isTimeout: false, id: idleCallbackId });
}, this._captureDelayMS);
this._timeouts.set(aBrowser, timeout);
this._timeouts.set(aBrowser, { isTimeout: true, id: timeoutId });
},
_shouldCapture: function Thumbnails_shouldCapture(aBrowser, aCallback) {
// Capture only if it's the currently selected tab.
if (aBrowser != gBrowser.selectedBrowser) {
// Capture only if it's the currently selected tab and not an about: page.
if (aBrowser != gBrowser.selectedBrowser ||
gBrowser.currentURI.schemeIs("about")) {
aCallback(false);
return;
}
PageThumbs.shouldStoreThumbnail(aBrowser, aCallback);
},
_clearTimeout: function Thumbnails_clearTimeout(aBrowser) {
_cancelDelayedCapture: function Thumbnails_cancelDelayedCapture(aBrowser) {
if (this._timeouts.has(aBrowser)) {
aBrowser.removeEventListener("scroll", this);
clearTimeout(this._timeouts.get(aBrowser));
this._cancelDelayedCallbacks(aBrowser);
this._timeouts.delete(aBrowser);
}
},
_cancelDelayedCallbacks: function Thumbnails_cancelDelayedCallbacks(aBrowser) {
let timeoutData = this._timeouts.get(aBrowser);
if (timeoutData.isTimeout) {
clearTimeout(timeoutData.id);
} else {
// idle callback dispatched
window.cancelIdleCallback(timeoutData.id);
}
}
};

View File

@ -221,7 +221,7 @@ function whenFileExists(aURL, aCallback = next) {
callback = () => whenFileExists(aURL, aCallback);
}
executeSoon(callback);
setTimeout(callback, 0);
}
/**