From a27bbb131976a0ba0fed3f994cd4941324edd76b Mon Sep 17 00:00:00 2001 From: Tobias Laundal Date: Thu, 16 Nov 2017 22:12:07 +0100 Subject: [PATCH] Refactor out common code between backgroundLogic.unhideAllTabs and showOnly Refactors code to get all containers in window into the function getContainers. Reduces code duplication between unhideAllTabs and showOnly. --- webextension/js/background/backgroundLogic.js | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/webextension/js/background/backgroundLogic.js b/webextension/js/background/backgroundLogic.js index da59c33..c16dfc1 100644 --- a/webextension/js/background/backgroundLogic.js +++ b/webextension/js/background/backgroundLogic.js @@ -14,6 +14,25 @@ const backgroundLogic = { return extensionInfo; }, + async getContainers(windowId) { + const [identities, state] = await Promise.all([ + browser.contextualIdentities.query({}), + backgroundLogic.queryIdentitiesState(windowId) + ]); + + return identities + .filter(identity => { + return identity.cookieStoreId in state; + }) + .map(identity => { + const stateObject = state[identity.cookieStoreId]; + identity.hasOpenTabs = stateObject.hasOpenTabs; + identity.hasHiddenTabs = stateObject.hasHiddenTabs; + + return identity; + }); + }, + getUserContextIdFromCookieStoreId(cookieStoreId) { if (!cookieStoreId) { return false; @@ -300,21 +319,15 @@ const backgroundLogic = { }, async unhideAllTabs(windowId) { - const [identities, state] = await Promise.all([ - browser.contextualIdentities.query({}), - backgroundLogic.queryIdentitiesState(windowId) - ]); - const promises = []; - identities.filter(identity => { - const stateObject = state[identity.cookieStoreId]; - return stateObject && stateObject.hasHiddenTabs; - }).map(identity => identity.cookieStoreId) - .forEach(cookieStoreId => { - // We need to call unhideContainer in messageHandler to prevent it from - // unhiding multiple times - promises.push(messageHandler.unhideContainer(cookieStoreId)); - }); + (await backgroundLogic.getContainers(windowId)) + .filter(identity => identity.hasHiddenTabs) + .map(identity => identity.cookieStoreId) + .forEach(cookieStoreId => { + // We need to call unhideContainer in messageHandler to prevent it from + // unhiding multiple times + promises.push(messageHandler.unhideContainer(cookieStoreId)); + }); return Promise.all(promises); }, @@ -323,19 +336,10 @@ const backgroundLogic = { return Promise.reject("showOnly needs both a windowId and a cookieStoreId"); } - const [identities, state] = await Promise.all([ - browser.contextualIdentities.query({}), - backgroundLogic.queryIdentitiesState(options.windowId) - ]); - const promises = []; - identities - .filter(identity => { - const stateObject = state[identity.cookieStoreId]; - const filt = identity.cookieStoreId !== options.cookieStoreId && - stateObject && stateObject.hasOpenTabs; - return filt; - }).map(identity => identity.cookieStoreId) + (await backgroundLogic.getContainers(options.windowId)) + .filter(identity => identity.cookieStoreId !== options.cookieStoreId && identity.hasOpenTabs) + .map(identity => identity.cookieStoreId) .forEach(cookieStoreId => { promises.push(backgroundLogic.hideTabs({cookieStoreId, windowId: options.windowId})); });