Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: ext/background.js

Issue 29793590: Issue 6490 - Wrap browser.browserAction.set* with Promise (Closed)
Patch Set: Address PS3 comments Created July 10, 2018, 10:53 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | polyfill.js » ('j') | polyfill.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/background.js
===================================================================
--- a/ext/background.js
+++ b/ext/background.js
@@ -296,109 +296,93 @@
let BrowserAction = function(tabId)
{
this._tabId = tabId;
this._changes = null;
};
BrowserAction.prototype = {
_applyChanges()
{
- if ("iconPath" in this._changes)
+ return Promise.all(Object.keys(this._changes).map(change =>
{
// Firefox for Android displays the browser action not as an icon but
- // as a menu item. There is no icon, but such an option may be added in
- // the future.
+ // as a menu item. There is no icon, but such an option may be added
+ // in the future.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1331746
- if ("setIcon" in browser.browserAction)
+ if (change == "iconPath" && "setIcon" in browser.browserAction)
{
let path = {
16: this._changes.iconPath.replace("$size", "16"),
19: this._changes.iconPath.replace("$size", "19"),
20: this._changes.iconPath.replace("$size", "20"),
32: this._changes.iconPath.replace("$size", "32"),
38: this._changes.iconPath.replace("$size", "38"),
40: this._changes.iconPath.replace("$size", "40")
};
try
{
- browser.browserAction.setIcon({tabId: this._tabId, path});
+ return browser.browserAction.setIcon({tabId: this._tabId, path});
}
catch (e)
{
// Edge throws if passed icon sizes different than 19,20,38,40px.
delete path[16];
delete path[32];
- browser.browserAction.setIcon({tabId: this._tabId, path});
+ return browser.browserAction.setIcon({tabId: this._tabId, path});
}
}
- }
- if ("badgeText" in this._changes)
- {
// There is no badge on Firefox for Android; the browser action is
// simply a menu item.
- if ("setBadgeText" in browser.browserAction)
- {
- browser.browserAction.setBadgeText({
+ if (change == "badgeText" && "setBadgeText" in browser.browserAction)
+ return browser.browserAction.setBadgeText({
tabId: this._tabId,
text: this._changes.badgeText
});
- }
- }
- if ("badgeColor" in this._changes)
- {
// There is no badge on Firefox for Android; the browser action is
// simply a menu item.
- if ("setBadgeBackgroundColor" in browser.browserAction)
- {
- browser.browserAction.setBadgeBackgroundColor({
+ if (change == "badgeColor" &&
+ "setBadgeBackgroundColor" in browser.browserAction)
+ return browser.browserAction.setBadgeBackgroundColor({
tabId: this._tabId,
color: this._changes.badgeColor
});
- }
- }
-
- this._changes = null;
- },
- _queueChanges()
- {
- browser.tabs.get(this._tabId, () =>
- {
- // If the tab is prerendered, browser.tabs.get() sets
- // browser.runtime.lastError and we have to delay our changes
- // until the currently visible tab is replaced with the
- // prerendered tab. Otherwise browser.browserAction.set* fails.
- if (browser.runtime.lastError)
- {
- let onReplaced = (addedTabId, removedTabId) =>
- {
- if (addedTabId == this._tabId)
- {
- browser.tabs.onReplaced.removeListener(onReplaced);
- this._applyChanges();
- }
- };
- browser.tabs.onReplaced.addListener(onReplaced);
- }
- else
- {
- this._applyChanges();
- }
- });
+ }));
},
_addChange(name, value)
{
+ let onReplaced = (addedTabId, removedTabId) =>
Jon Sonesen 2018/07/10 23:01:07 Since I had trouble removing the check below this
+ {
+ if (addedTabId == this._tabId)
+ {
+ browser.tabs.onReplaced.removeListener(onReplaced);
+ this._applyChanges().then(() =>
+ {
+ this._changes = null;
+ });
+ }
+ };
if (!this._changes)
- {
this._changes = {};
- this._queueChanges();
- }
this._changes[name] = value;
+ if (!browser.tabs.onReplaced.hasListener(onReplaced))
Jon Sonesen 2018/07/10 23:01:07 Replacing this check with the applyChanges boolean
+ {
+ this._applyChanges().then(() =>
+ {
+ this._changes = null;
+ }).catch(() =>
Jon Sonesen 2018/07/10 23:01:08 I still think this is more readable than the callb
+ {
+ // If the tab is prerendered, browser.browserAction.set* fails
+ // and we have to delay our changes until the currently visible tab
+ // is replaced with the prerendered tab.
+ browser.tabs.onReplaced.addListener(onReplaced);
+ });
+ }
},
setIcon(path)
{
this._addChange("iconPath", path);
},
setBadge(badge)
{
if (!badge)
« no previous file with comments | « no previous file | polyfill.js » ('j') | polyfill.js » ('J')

Powered by Google App Engine
This is Rietveld