From fa9b09b6d0afc8c7bded6b44c643ee131f6e5ba7 Mon Sep 17 00:00:00 2001 From: Pete Miller Date: Wed, 16 Aug 2017 01:34:01 -0700 Subject: [PATCH] Stop losing pinned tabs in a window when adding or removing another pinned tab Fix #3760, fix #9635, fix #10037, fix #10510, and possibly addresses #10117 The previous code resulted in tabs being marked as no longer required to be pinned, and closed prematurely. In many situations, when pinning a tab, all previously pinned tabs would be closed. Sometimes this appeared like a 'crash' when the window closed because all the tabs had been (undesirably) closed. --- app/browser/windows.js | 65 ++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 37 deletions(-) diff --git a/app/browser/windows.js b/app/browser/windows.js index 99284906926..d1b74cab41f 100644 --- a/app/browser/windows.js +++ b/app/browser/windows.js @@ -15,7 +15,6 @@ const {getPinnedTabsByWindowId} = require('../common/state/tabState') const messages = require('../../js/constants/messages') const settings = require('../../js/constants/settings') const windowState = require('../common/state/windowState') -const Immutable = require('immutable') const pinnedSitesState = require('../common/state/pinnedSitesState') const pinnedSitesUtil = require('../common/lib/pinnedSitesUtil') @@ -62,51 +61,44 @@ const updateWindow = (windowId) => { } } +const siteMatchesTab = (site, tab) => { + const matchesLocation = getLocationIfPDF(tab.get('url')) === site.get('location') + const matchesPartition = (tab.get('partitionNumber') || 0) === (site.get('partitionNumber') || 0) + return matchesLocation && matchesPartition +} + const updatePinnedTabs = (win) => { if (win.webContents.browserWindowOptions.disposition === 'new-popup') { return } - const appStore = require('../../js/stores/appStore') const state = appStore.getState() const windowId = win.id const pinnedSites = pinnedSitesState.getSites(state).map(site => pinnedSitesUtil.getPinnedSiteProps(site)) const pinnedTabs = getPinnedTabsByWindowId(state, windowId) - - pinnedSites.filter((site) => - pinnedTabs.find((tab) => - getLocationIfPDF(tab.get('url')) === site.get('location') && - (tab.get('partitionNumber') || 0) === (site.get('partitionNumber') || 0))).forEach((site) => { - win.__alreadyPinnedSites = win.__alreadyPinnedSites.add(site) + // sites are instructions of what should be pinned + // tabs are sites our window already has pinned + const tabsWithSites = new Set() + for (const site of pinnedSites.values()) { + const existingPinnedTab = pinnedTabs.find(tab => siteMatchesTab(site, tab)) + if (existingPinnedTab) { + tabsWithSites.add(existingPinnedTab) + } else { + appActions.createTabRequested({ + url: site.get('location'), + partitionNumber: site.get('partitionNumber'), + pinned: true, + active: false, + windowId }) - - const sitesToAdd = pinnedSites.filter((site) => - !win.__alreadyPinnedSites.find((pinned) => pinned.equals(site))) - - sitesToAdd.forEach((site) => { - win.__alreadyPinnedSites = win.__alreadyPinnedSites.add(site) - appActions.createTabRequested({ - url: site.get('location'), - partitionNumber: site.get('partitionNumber'), - pinned: true, - active: false, - windowId - }) - }) - - const sitesToClose = win.__alreadyPinnedSites.filter((pinned) => - !pinnedSites.find((site) => pinned.equals(site))) - - sitesToClose - .forEach((site) => { - const tab = pinnedTabs.find((tab) => - tab.get('url') === site.get('location') && - (tab.get('partitionNumber') || 0) === (site.get('partitionNumber') || 0)) - if (tab) { - appActions.tabCloseRequested(tab.get('tabId'), true) - } - win.__alreadyPinnedSites = win.__alreadyPinnedSites.remove(site) - }) + } + } + for (const tab of pinnedTabs.values()) { + const shouldBePinned = tabsWithSites.has(tab) + if (!shouldBePinned) { + appActions.tabCloseRequested(tab.get('tabId'), true) + } + } } const api = { @@ -316,7 +308,6 @@ const api = { setImmediate(() => { const win = currentWindows[windowId] if (win && !win.isDestroyed()) { - win.__alreadyPinnedSites = new Immutable.Set() updatePinnedTabs(win) win.__ready = true }