Skip to content

Commit aaf9e11

Browse files
committed
Don't drop tab related events notified while initializing
1 parent 7f0350e commit aaf9e11

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

webextensions/background/api-tabs-listener.js

+45-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function logUpdated(...args) {
6565
internalLogger('common/tabs-update', ...args);
6666
}
6767

68-
export function startListen() {
68+
export function init() {
6969
browser.tabs.onActivated.addListener(onActivated);
7070
browser.tabs.onUpdated.addListener(onUpdated);
7171
browser.tabs.onHighlighted.addListener(onHighlighted);
@@ -77,7 +77,9 @@ export function startListen() {
7777
browser.windows.onRemoved.addListener(onWindowRemoved);
7878
}
7979

80-
export function endListen() {
80+
export function destroy() {
81+
mPromisedStartedResolver = undefined;
82+
mPromisedStarted = undefined;
8183
browser.tabs.onActivated.removeListener(onActivated);
8284
browser.tabs.onUpdated.removeListener(onUpdated);
8385
browser.tabs.onHighlighted.removeListener(onHighlighted);
@@ -89,6 +91,18 @@ export function endListen() {
8991
browser.windows.onRemoved.removeListener(onWindowRemoved);
9092
}
9193

94+
let mPromisedStartedResolver;
95+
let mPromisedStarted = new Promise((resolve, _reject) => {
96+
mPromisedStartedResolver = resolve;
97+
});
98+
99+
export function start() {
100+
if (!mPromisedStartedResolver)
101+
return;
102+
mPromisedStartedResolver();
103+
mPromisedStartedResolver = undefined;
104+
mPromisedStarted = undefined;
105+
}
92106

93107

94108
const mTabOperationQueue = [];
@@ -113,6 +127,9 @@ function warnTabDestroyedWhileWaiting(tabId, tab) {
113127

114128

115129
async function onActivated(activeInfo) {
130+
if (mPromisedStarted)
131+
await mPromisedStarted;
132+
116133
TabsStore.activeTabInWindow.set(activeInfo.windowId, Tab.get(activeInfo.tabId));
117134

118135
const [onCompleted, previous] = addTabOperationQueue();
@@ -197,6 +214,9 @@ async function onActivated(activeInfo) {
197214
}
198215

199216
async function onUpdated(tabId, changeInfo, tab) {
217+
if (mPromisedStarted)
218+
await mPromisedStarted;
219+
200220
TabIdFixer.fixTab(tab);
201221
tabId = tab.id;
202222

@@ -268,7 +288,10 @@ async function onUpdated(tabId, changeInfo, tab) {
268288

269289
const mTabsHighlightedTimers = new Map();
270290
const mLastHighlightedCount = new Map();
271-
function onHighlighted(highlightInfo) {
291+
async function onHighlighted(highlightInfo) {
292+
if (mPromisedStarted)
293+
await mPromisedStarted;
294+
272295
let timer = mTabsHighlightedTimers.get(highlightInfo.windowId);
273296
if (timer)
274297
clearTimeout(timer);
@@ -296,7 +319,10 @@ function onHighlighted(highlightInfo) {
296319
mTabsHighlightedTimers.set(highlightInfo.windowId, timer);
297320
}
298321

299-
function onCreated(tab) {
322+
async function onCreated(tab) {
323+
if (mPromisedStarted)
324+
await mPromisedStarted;
325+
300326
log('tabs.onCreated: ', dumpTab(tab));
301327
return onNewTabTracked(tab);
302328
}
@@ -541,6 +567,9 @@ function checkRecycledTab(windowId) {
541567
}
542568

543569
async function onRemoved(tabId, removeInfo) {
570+
if (mPromisedStarted)
571+
await mPromisedStarted;
572+
544573
log('tabs.onRemoved: ', tabId, removeInfo);
545574
const window = Window.init(removeInfo.windowId);
546575
const byInternalOperation = window.internalClosingTabs.has(tabId);
@@ -629,6 +658,9 @@ async function onRemoved(tabId, removeInfo) {
629658
}
630659

631660
async function onMoved(tabId, moveInfo) {
661+
if (mPromisedStarted)
662+
await mPromisedStarted;
663+
632664
const window = Window.init(moveInfo.windowId);
633665

634666
// Firefox may move the tab between TabsMove.moveTabsInternallyBefore/After()
@@ -744,6 +776,9 @@ async function onMoved(tabId, moveInfo) {
744776
const mTreeInfoForTabsMovingAcrossWindows = new Map();
745777

746778
async function onAttached(tabId, attachInfo) {
779+
if (mPromisedStarted)
780+
await mPromisedStarted;
781+
747782
const [onCompleted, previous] = addTabOperationQueue();
748783
if (!configs.acceleratedTabOperations && previous)
749784
await previous;
@@ -793,6 +828,9 @@ async function onAttached(tabId, attachInfo) {
793828
}
794829

795830
async function onDetached(tabId, detachInfo) {
831+
if (mPromisedStarted)
832+
await mPromisedStarted;
833+
796834
const [onCompleted, previous] = addTabOperationQueue();
797835
if (!configs.acceleratedTabOperations && previous)
798836
await previous;
@@ -852,6 +890,9 @@ async function onDetached(tabId, detachInfo) {
852890
}
853891

854892
async function onWindowRemoved(windowId) {
893+
if (mPromisedStarted)
894+
await mPromisedStarted;
895+
855896
mTabsHighlightedTimers.delete(windowId);
856897
mLastHighlightedCount.delete(windowId);
857898

webextensions/background/background.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export async function init() {
8282
populate: true,
8383
windowTypes: ['normal']
8484
}).catch(ApiTabs.createErrorHandler());
85+
ApiTabsListener.init();
8586
}),
8687
ContextualIdentities.init(),
8788
configs.$loaded
@@ -100,7 +101,7 @@ export async function init() {
100101
mPreloadedCaches.clear();
101102
await MetricsData.addAsync('init: TreeStructure.loadTreeStructure', TreeStructure.loadTreeStructure(windows, restoredFromCache));
102103

103-
ApiTabsListener.startListen();
104+
ApiTabsListener.start();
104105

105106
// Open new tab now (after listening is started, before the end of initialization),
106107
// because the sidebar may fail to track tabs.onCreated for the tab while its
@@ -211,7 +212,7 @@ function destroy() {
211212
}).catch(ApiTabs.createErrorSuppressor());
212213

213214
onDestroy.dispatch();
214-
ApiTabsListener.endListen();
215+
ApiTabsListener.destroy();
215216
ContextualIdentities.endObserve();
216217
}
217218

0 commit comments

Comments
 (0)