@@ -65,7 +65,7 @@ function logUpdated(...args) {
65
65
internalLogger ( 'common/tabs-update' , ...args ) ;
66
66
}
67
67
68
- export function startListen ( ) {
68
+ export function init ( ) {
69
69
browser . tabs . onActivated . addListener ( onActivated ) ;
70
70
browser . tabs . onUpdated . addListener ( onUpdated ) ;
71
71
browser . tabs . onHighlighted . addListener ( onHighlighted ) ;
@@ -77,7 +77,9 @@ export function startListen() {
77
77
browser . windows . onRemoved . addListener ( onWindowRemoved ) ;
78
78
}
79
79
80
- export function endListen ( ) {
80
+ export function destroy ( ) {
81
+ mPromisedStartedResolver = undefined ;
82
+ mPromisedStarted = undefined ;
81
83
browser . tabs . onActivated . removeListener ( onActivated ) ;
82
84
browser . tabs . onUpdated . removeListener ( onUpdated ) ;
83
85
browser . tabs . onHighlighted . removeListener ( onHighlighted ) ;
@@ -89,6 +91,18 @@ export function endListen() {
89
91
browser . windows . onRemoved . removeListener ( onWindowRemoved ) ;
90
92
}
91
93
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
+ }
92
106
93
107
94
108
const mTabOperationQueue = [ ] ;
@@ -113,6 +127,9 @@ function warnTabDestroyedWhileWaiting(tabId, tab) {
113
127
114
128
115
129
async function onActivated ( activeInfo ) {
130
+ if ( mPromisedStarted )
131
+ await mPromisedStarted ;
132
+
116
133
TabsStore . activeTabInWindow . set ( activeInfo . windowId , Tab . get ( activeInfo . tabId ) ) ;
117
134
118
135
const [ onCompleted , previous ] = addTabOperationQueue ( ) ;
@@ -197,6 +214,9 @@ async function onActivated(activeInfo) {
197
214
}
198
215
199
216
async function onUpdated ( tabId , changeInfo , tab ) {
217
+ if ( mPromisedStarted )
218
+ await mPromisedStarted ;
219
+
200
220
TabIdFixer . fixTab ( tab ) ;
201
221
tabId = tab . id ;
202
222
@@ -268,7 +288,10 @@ async function onUpdated(tabId, changeInfo, tab) {
268
288
269
289
const mTabsHighlightedTimers = new Map ( ) ;
270
290
const mLastHighlightedCount = new Map ( ) ;
271
- function onHighlighted ( highlightInfo ) {
291
+ async function onHighlighted ( highlightInfo ) {
292
+ if ( mPromisedStarted )
293
+ await mPromisedStarted ;
294
+
272
295
let timer = mTabsHighlightedTimers . get ( highlightInfo . windowId ) ;
273
296
if ( timer )
274
297
clearTimeout ( timer ) ;
@@ -296,7 +319,10 @@ function onHighlighted(highlightInfo) {
296
319
mTabsHighlightedTimers . set ( highlightInfo . windowId , timer ) ;
297
320
}
298
321
299
- function onCreated ( tab ) {
322
+ async function onCreated ( tab ) {
323
+ if ( mPromisedStarted )
324
+ await mPromisedStarted ;
325
+
300
326
log ( 'tabs.onCreated: ' , dumpTab ( tab ) ) ;
301
327
return onNewTabTracked ( tab ) ;
302
328
}
@@ -541,6 +567,9 @@ function checkRecycledTab(windowId) {
541
567
}
542
568
543
569
async function onRemoved ( tabId , removeInfo ) {
570
+ if ( mPromisedStarted )
571
+ await mPromisedStarted ;
572
+
544
573
log ( 'tabs.onRemoved: ' , tabId , removeInfo ) ;
545
574
const window = Window . init ( removeInfo . windowId ) ;
546
575
const byInternalOperation = window . internalClosingTabs . has ( tabId ) ;
@@ -629,6 +658,9 @@ async function onRemoved(tabId, removeInfo) {
629
658
}
630
659
631
660
async function onMoved ( tabId , moveInfo ) {
661
+ if ( mPromisedStarted )
662
+ await mPromisedStarted ;
663
+
632
664
const window = Window . init ( moveInfo . windowId ) ;
633
665
634
666
// Firefox may move the tab between TabsMove.moveTabsInternallyBefore/After()
@@ -744,6 +776,9 @@ async function onMoved(tabId, moveInfo) {
744
776
const mTreeInfoForTabsMovingAcrossWindows = new Map ( ) ;
745
777
746
778
async function onAttached ( tabId , attachInfo ) {
779
+ if ( mPromisedStarted )
780
+ await mPromisedStarted ;
781
+
747
782
const [ onCompleted , previous ] = addTabOperationQueue ( ) ;
748
783
if ( ! configs . acceleratedTabOperations && previous )
749
784
await previous ;
@@ -793,6 +828,9 @@ async function onAttached(tabId, attachInfo) {
793
828
}
794
829
795
830
async function onDetached ( tabId , detachInfo ) {
831
+ if ( mPromisedStarted )
832
+ await mPromisedStarted ;
833
+
796
834
const [ onCompleted , previous ] = addTabOperationQueue ( ) ;
797
835
if ( ! configs . acceleratedTabOperations && previous )
798
836
await previous ;
@@ -852,6 +890,9 @@ async function onDetached(tabId, detachInfo) {
852
890
}
853
891
854
892
async function onWindowRemoved ( windowId ) {
893
+ if ( mPromisedStarted )
894
+ await mPromisedStarted ;
895
+
855
896
mTabsHighlightedTimers . delete ( windowId ) ;
856
897
mLastHighlightedCount . delete ( windowId ) ;
857
898
0 commit comments