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.
This commit is contained in:
Tobias Laundal 2017-11-16 22:12:07 +01:00
parent 3d7dfba2f9
commit a27bbb1319

View file

@ -14,6 +14,25 @@ const backgroundLogic = {
return extensionInfo; 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) { getUserContextIdFromCookieStoreId(cookieStoreId) {
if (!cookieStoreId) { if (!cookieStoreId) {
return false; return false;
@ -300,21 +319,15 @@ const backgroundLogic = {
}, },
async unhideAllTabs(windowId) { async unhideAllTabs(windowId) {
const [identities, state] = await Promise.all([
browser.contextualIdentities.query({}),
backgroundLogic.queryIdentitiesState(windowId)
]);
const promises = []; const promises = [];
identities.filter(identity => { (await backgroundLogic.getContainers(windowId))
const stateObject = state[identity.cookieStoreId]; .filter(identity => identity.hasHiddenTabs)
return stateObject && stateObject.hasHiddenTabs; .map(identity => identity.cookieStoreId)
}).map(identity => identity.cookieStoreId) .forEach(cookieStoreId => {
.forEach(cookieStoreId => { // We need to call unhideContainer in messageHandler to prevent it from
// We need to call unhideContainer in messageHandler to prevent it from // unhiding multiple times
// unhiding multiple times promises.push(messageHandler.unhideContainer(cookieStoreId));
promises.push(messageHandler.unhideContainer(cookieStoreId)); });
});
return Promise.all(promises); return Promise.all(promises);
}, },
@ -323,19 +336,10 @@ const backgroundLogic = {
return Promise.reject("showOnly needs both a windowId and a cookieStoreId"); 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 = []; const promises = [];
identities (await backgroundLogic.getContainers(options.windowId))
.filter(identity => { .filter(identity => identity.cookieStoreId !== options.cookieStoreId && identity.hasOpenTabs)
const stateObject = state[identity.cookieStoreId]; .map(identity => identity.cookieStoreId)
const filt = identity.cookieStoreId !== options.cookieStoreId &&
stateObject && stateObject.hasOpenTabs;
return filt;
}).map(identity => identity.cookieStoreId)
.forEach(cookieStoreId => { .forEach(cookieStoreId => {
promises.push(backgroundLogic.hideTabs({cookieStoreId, windowId: options.windowId})); promises.push(backgroundLogic.hideTabs({cookieStoreId, windowId: options.windowId}));
}); });