Skip to content

Commit c9c636e

Browse files
authored
Fix slowness on startup caused by having to reload discarded tabs for refreshing tab markers (#302)
1 parent aba0201 commit c9c636e

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/background/misc/tabMarkers.ts

+33-10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ export async function getTabMarker(tabId: number) {
3232
});
3333
}
3434

35+
async function assignTabMarker(tabId: number, marker: string) {
36+
return withTabMarkers(({ free, tabIdsToMarkers, markersToTabIds }) => {
37+
if (!free.includes(marker)) {
38+
throw new Error(
39+
`Unable to assign marker ${marker} as it's already in use`
40+
);
41+
}
42+
43+
const markerIndex = free.indexOf(marker);
44+
free.splice(markerIndex, 1);
45+
46+
tabIdsToMarkers.set(tabId, marker);
47+
markersToTabIds.set(marker, tabId);
48+
});
49+
}
50+
3551
export async function getTabIdForMarker(marker: string) {
3652
return withTabMarkers(({ markersToTabIds }) => {
3753
const tabId = markersToTabIds.get(marker);
@@ -84,25 +100,32 @@ async function resetTabMarkers() {
84100
export async function initTabMarkers() {
85101
await resetTabMarkers();
86102

87-
// We need to reload all tabs where the content script is not running in case
103+
// We need to assign the tab markers to their corresponding tab id in case
88104
// the user has the setting "Continue where you left off" enabled. If we don't
89-
// those tabs will have an invalid tab marker. We also can't reassign the tab
90-
// markers to those tabs since we can't get their title, so we don't know
91-
// which tab marker each one has.
105+
// those tabs will have an invalid tab marker.
92106

93107
if (!(await retrieve("includeTabMarkers"))) return;
94108

95109
const tabs = await browser.tabs.query({});
96110

111+
const getMarkerFromTitle = (title: string) => {
112+
return /^([a-z]{1,2}) \| /i.exec(title)?.[1]?.toLowerCase();
113+
};
114+
97115
await Promise.allSettled(
98-
tabs.map(async (tab) => {
116+
tabs.map(async ({ title, id }) => {
117+
if (!title || !id) return;
118+
119+
const marker = getMarkerFromTitle(title);
120+
if (!marker) return;
121+
99122
try {
100-
await sendRequestToContent(
101-
{ type: "checkContentScriptRunning" },
102-
tab.id
103-
);
123+
await assignTabMarker(id, marker);
104124
} catch {
105-
return browser.tabs.reload(tab.id);
125+
// If the tab marker is already in use we reload the tab so it gets a
126+
// new one. I'm not entirely sure if this is necessary but I leave it
127+
// here just to be safe.
128+
return browser.tabs.reload(id);
106129
}
107130
})
108131
);

0 commit comments

Comments
 (0)