Reduce number of calls to host app when computing badge counters (#173)

This commit is contained in:
Maxim Baz
2019-09-27 12:56:27 +02:00
committed by GitHub
parent 14302e9362
commit 10718c7187

View File

@@ -28,6 +28,12 @@ var defaultSettings = {
var authListeners = {}; var authListeners = {};
var badgeCache = {
files: null,
settings: null,
expires: Date.now()
};
// the last text copied to the clipboard is stored here in order to be cleared after 60 seconds // the last text copied to the clipboard is stored here in order to be cleared after 60 seconds
let lastCopiedText = null; let lastCopiedText = null;
@@ -45,10 +51,8 @@ chrome.tabs.onUpdated.addListener((tabId, info) => {
} }
} }
// refresh badge counter when url in a tab changes // redraw badge counter
if (info.url) {
updateMatchingPasswordsCount(tabId); updateMatchingPasswordsCount(tabId);
}
}); });
// handle incoming messages // handle incoming messages
@@ -103,38 +107,52 @@ chrome.runtime.onInstalled.addListener(onExtensionInstalled);
* @since 3.0.0 * @since 3.0.0
* *
* @param int tabId Tab id * @param int tabId Tab id
* @param bool forceRefresh force invalidate cache
* @return void * @return void
*/ */
async function updateMatchingPasswordsCount(tabId) { async function updateMatchingPasswordsCount(tabId, forceRefresh = false) {
try { try {
const settings = await getFullSettings(); if (forceRefresh || Date.now() > badgeCache.expires) {
var response = await hostAction(settings, "list"); badgeCache = {
files: null,
settings: null,
expires: Date.now() + 60 * 1000
};
badgeCache.settings = await getFullSettings();
var response = await hostAction(badgeCache.settings, "list");
if (response.status != "ok") { if (response.status != "ok") {
throw new Error(JSON.stringify(response)); throw new Error(JSON.stringify(response));
} }
// Get tab info badgeCache.files = response.data.files;
}
if (badgeCache.files === null) {
return; // badge cache refresh in progress
}
try { try {
const tab = await chrome.tabs.get(tabId); const tab = await chrome.tabs.get(tabId);
settings.host = new URL(tab.url).hostname; badgeCache.settings.host = new URL(tab.url).hostname;
} catch (e) { } catch (e) {
throw new Error(`Unable to determine domain of the tab with id ${tabId}`); throw new Error(`Unable to determine domain of the tab with id ${tabId}`);
} }
const files = helpers.ignoreFiles(response.data.files, settings); // Compule badge counter
const logins = helpers.prepareLogins(files, settings); const files = helpers.ignoreFiles(badgeCache.files, badgeCache.settings);
const logins = helpers.prepareLogins(files, badgeCache.settings);
const matchedPasswordsCount = logins.reduce( const matchedPasswordsCount = logins.reduce(
(acc, login) => acc + (login.recent.count || login.inCurrentDomain ? 1 : 0), (acc, login) => acc + (login.recent.count || login.inCurrentDomain ? 1 : 0),
0 0
); );
if (matchedPasswordsCount) {
// Set badge for the current tab // Set badge for the current tab
chrome.browserAction.setBadgeText({ chrome.browserAction.setBadgeText({
text: "" + matchedPasswordsCount, text: "" + (matchedPasswordsCount || ""),
tabId: tabId tabId: tabId
}); });
}
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
@@ -215,7 +233,7 @@ async function saveRecent(settings, login, remove = false) {
// a new entry was added to the popup matching list, need to refresh the count // a new entry was added to the popup matching list, need to refresh the count
if (!login.inCurrentDomain && login.recent.count === 1) { if (!login.inCurrentDomain && login.recent.count === 1) {
updateMatchingPasswordsCount(settings.tab.id); updateMatchingPasswordsCount(settings.tab.id, true);
} }
// save to usage log // save to usage log