@@ -122,11 +122,7 @@ function checkIfSessionIsRestoring(newTab: Tab) {
122122 maybeRestoredTabs &&
123123 maybeRestoredTabs . length >= SESSION_RESTORE_MIN_TABS_COUNT
124124 ) {
125- // TODO: well, at this moment sidebery doesn't know yet if
126- // this is a session restore or just multiple new tabs (low probability though)
127- // so maybe I should show popup with the title like "Processing new tabs..."
128- // instead?
129- Popups . openSessionRestorePopup ( )
125+ Popups . openProcessingTabsPopup ( )
130126
131127 cancelDeferredMovingOfNewTabs ( )
132128
@@ -146,6 +142,13 @@ function checkIfSessionIsRestoring(newTab: Tab) {
146142 maybeRestoredTabsDataQuerying . push ( dataQuerying . catch ( ( ) => undefined ) )
147143 }
148144
145+ // Set the promise for awaiting a session restore detection
146+ if ( maybeRestoredTabsDataQuerying ) {
147+ newTab . checkingSessionRestore = new Promise ( ok => {
148+ newTab . resolveSessionRestoreDetection = ok
149+ } )
150+ }
151+
149152 clearTimeout ( checkingIfSessionRestoringTimeout )
150153 checkingIfSessionRestoringTimeout = setTimeout ( async ( ) => {
151154 if (
@@ -163,13 +166,8 @@ function checkIfSessionIsRestoring(newTab: Tab) {
163166
164167 tryToRestoreTabsStateFromSessionData ( maybeRestoredTabs , tabsSessionData )
165168 } else {
166- cancelDeferredMovingOfNewTabs ( )
167-
168- // Reinit tabs
169- Tabs . unload ( )
170- Tabs . load ( LoadSrc . SessionOnly )
171-
172- Popups . closeSessionRestorePopup ( )
169+ // It's not a session restore, resolve the promises
170+ maybeRestoredTabs ?. forEach ( t => t . resolveSessionRestoreDetection ?.( false ) )
173171 }
174172
175173 maybeRestoredTabs = null
@@ -189,18 +187,23 @@ async function tryToRestoreTabsStateFromSessionData(
189187 if ( tabsWithSessionDataLen < SESSION_RESTORE_MIN_TABS_COUNT ) {
190188 Logs . warn ( 'Tabs.tryToRestoreTabsStateFromSessionData: Not enough session data' )
191189
192- cancelDeferredMovingOfNewTabs ( )
190+ // Restart deferred tabs moving to sort all new tabs, not just
191+ // the last one in case of two opened tabs b/c the first deferred
192+ // moving was canceled in checkIfSessionIsRestoring fn.
193+ handleNewTabMove ( )
193194
194- // Reinit tabs
195- Tabs . unload ( )
196- Tabs . load ( LoadSrc . SessionOnly )
195+ // It's not a session restore, resolve the promises
196+ tabs ?. forEach ( t => t . resolveSessionRestoreDetection ?.( false ) )
197197
198- Popups . closeSessionRestorePopup ( )
198+ Popups . closeProcessingTabsPopup ( )
199199 return
200200 }
201201
202+ // It's a session restore, resolve the session restore detection promises
203+ tabs . forEach ( t => t . resolveSessionRestoreDetection ?.( true ) )
204+
202205 // Show popup about restoring the tabs
203- Popups . openSessionRestorePopup ( )
206+ Popups . openProcessingTabsPopup ( )
204207
205208 // Unload tabs
206209 Tabs . unload ( )
@@ -226,10 +229,10 @@ async function tryToRestoreTabsStateFromSessionData(
226229 await Utils . sleep ( 100 )
227230
228231 // Close popup
229- Popups . closeSessionRestorePopup ( )
232+ Popups . closeProcessingTabsPopup ( )
230233}
231234
232- function onTabCreated ( nativeTab : NativeTab , attached ?: boolean ) : void {
235+ async function onTabCreated ( nativeTab : NativeTab , attached ?: boolean ) {
233236 if ( nativeTab . windowId !== Windows . id ) return
234237 if ( ! Tabs . ready || Tabs . sorting ) {
235238 Tabs . deferredEventHandling . push ( ( ) => onTabCreated ( nativeTab ) )
@@ -248,9 +251,16 @@ function onTabCreated(nativeTab: NativeTab, attached?: boolean): void {
248251 const tab = Tabs . mutateNativeTabToSideberyTab ( nativeTab )
249252
250253 // Stop if multiple tabs were open and data querying is started
254+ let notSessionRestore
251255 if ( maybeRestoredTabsDataQuerying ) {
252256 checkIfSessionIsRestoring ( tab )
253- return
257+ if ( tab . checkingSessionRestore ) {
258+ const sessionRestoreIsDetected = await tab . checkingSessionRestore
259+ delete tab . checkingSessionRestore
260+ delete tab . resolveSessionRestoreDetection
261+ if ( sessionRestoreIsDetected ) return
262+ notSessionRestore = true
263+ }
254264 }
255265
256266 // Check if opener tab is pinned
@@ -381,11 +391,9 @@ function onTabCreated(nativeTab: NativeTab, attached?: boolean): void {
381391
382392 // Find appropriate position using the current settings
383393 else {
384- panel = Tabs . getPanelForNewTab ( tab )
385- if ( ! panel ) return Logs . err ( 'Cannot handle new tab: Cannot find target panel' )
386-
387394 // Check if this is event is part of session restore
388395 if (
396+ ! notSessionRestore &&
389397 ! attached &&
390398 // Tab is unloaded and has set url
391399 ( ( tab . discarded && tab . url !== 'about:blank' ) ||
@@ -395,17 +403,28 @@ function onTabCreated(nativeTab: NativeTab, attached?: boolean): void {
395403 tab . pinned )
396404 ) {
397405 checkIfSessionIsRestoring ( tab )
398- if ( maybeRestoredTabsDataQuerying ) return
406+ if ( maybeRestoredTabsDataQuerying && tab . checkingSessionRestore ) {
407+ const sessionRestoreIsDetected = await tab . checkingSessionRestore
408+ delete tab . checkingSessionRestore
409+ delete tab . resolveSessionRestoreDetection
410+ if ( sessionRestoreIsDetected ) return
411+ }
399412 }
400413
401- // Get parent tab
414+ // Get panel
415+ panel = Tabs . getPanelForNewTab ( tab )
416+ if ( ! panel ) return Logs . err ( 'Cannot handle new tab: Cannot find target panel' )
417+
418+ // Outdent if opener tab is folded (if configured)
402419 const parent = Tabs . byId [ tab . openerTabId ?? NOID ]
403420 if ( ! attached && parent ?. folded && Settings . state . ignoreFoldedParent ) {
404421 tab . openerTabId = parent . parentId
405422 }
406423
407- // Get index
424+ // Get target index
408425 index = Tabs . getIndexForNewTab ( panel , tab )
426+
427+ // Update opener
409428 if ( ! autoGroupTab ) {
410429 tab . openerTabId = Tabs . getParentForNewTab ( panel , tab )
411430 }
0 commit comments