Skip to content

Commit 10718c7

Browse files
authored
Reduce number of calls to host app when computing badge counters (#173)
1 parent 14302e9 commit 10718c7

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

src/background.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ var defaultSettings = {
2828

2929
var authListeners = {};
3030

31+
var badgeCache = {
32+
files: null,
33+
settings: null,
34+
expires: Date.now()
35+
};
36+
3137
// the last text copied to the clipboard is stored here in order to be cleared after 60 seconds
3238
let lastCopiedText = null;
3339

@@ -45,10 +51,8 @@ chrome.tabs.onUpdated.addListener((tabId, info) => {
4551
}
4652
}
4753

48-
// refresh badge counter when url in a tab changes
49-
if (info.url) {
50-
updateMatchingPasswordsCount(tabId);
51-
}
54+
// redraw badge counter
55+
updateMatchingPasswordsCount(tabId);
5256
});
5357

5458
// handle incoming messages
@@ -102,39 +106,53 @@ chrome.runtime.onInstalled.addListener(onExtensionInstalled);
102106
*
103107
* @since 3.0.0
104108
*
105-
* @param int tabId Tab id
109+
* @param int tabId Tab id
110+
* @param bool forceRefresh force invalidate cache
106111
* @return void
107112
*/
108-
async function updateMatchingPasswordsCount(tabId) {
113+
async function updateMatchingPasswordsCount(tabId, forceRefresh = false) {
109114
try {
110-
const settings = await getFullSettings();
111-
var response = await hostAction(settings, "list");
112-
if (response.status != "ok") {
113-
throw new Error(JSON.stringify(response));
115+
if (forceRefresh || Date.now() > badgeCache.expires) {
116+
badgeCache = {
117+
files: null,
118+
settings: null,
119+
expires: Date.now() + 60 * 1000
120+
};
121+
122+
badgeCache.settings = await getFullSettings();
123+
124+
var response = await hostAction(badgeCache.settings, "list");
125+
if (response.status != "ok") {
126+
throw new Error(JSON.stringify(response));
127+
}
128+
129+
badgeCache.files = response.data.files;
130+
}
131+
132+
if (badgeCache.files === null) {
133+
return; // badge cache refresh in progress
114134
}
115135

116-
// Get tab info
117136
try {
118137
const tab = await chrome.tabs.get(tabId);
119-
settings.host = new URL(tab.url).hostname;
138+
badgeCache.settings.host = new URL(tab.url).hostname;
120139
} catch (e) {
121140
throw new Error(`Unable to determine domain of the tab with id ${tabId}`);
122141
}
123142

124-
const files = helpers.ignoreFiles(response.data.files, settings);
125-
const logins = helpers.prepareLogins(files, settings);
143+
// Compule badge counter
144+
const files = helpers.ignoreFiles(badgeCache.files, badgeCache.settings);
145+
const logins = helpers.prepareLogins(files, badgeCache.settings);
126146
const matchedPasswordsCount = logins.reduce(
127147
(acc, login) => acc + (login.recent.count || login.inCurrentDomain ? 1 : 0),
128148
0
129149
);
130150

131-
if (matchedPasswordsCount) {
132-
// Set badge for the current tab
133-
chrome.browserAction.setBadgeText({
134-
text: "" + matchedPasswordsCount,
135-
tabId: tabId
136-
});
137-
}
151+
// Set badge for the current tab
152+
chrome.browserAction.setBadgeText({
153+
text: "" + (matchedPasswordsCount || ""),
154+
tabId: tabId
155+
});
138156
} catch (e) {
139157
console.log(e);
140158
}
@@ -215,7 +233,7 @@ async function saveRecent(settings, login, remove = false) {
215233

216234
// a new entry was added to the popup matching list, need to refresh the count
217235
if (!login.inCurrentDomain && login.recent.count === 1) {
218-
updateMatchingPasswordsCount(settings.tab.id);
236+
updateMatchingPasswordsCount(settings.tab.id, true);
219237
}
220238

221239
// save to usage log

0 commit comments

Comments
 (0)