@@ -38,13 +38,17 @@ async function getClientId() {
3838
3939type TabSwitchQueueEntry =
4040 | {
41- createTab : false ;
41+ op : "switch" ;
4242 tabId : string ;
4343 setInBackend : boolean ;
4444 }
4545 | {
46- createTab : true ;
46+ op : "create" ;
4747 pinned : boolean ;
48+ }
49+ | {
50+ op : "close" ;
51+ tabId : string ;
4852 } ;
4953
5054export class WaveBrowserWindow extends BaseWindow {
@@ -252,6 +256,11 @@ export class WaveBrowserWindow extends BaseWindow {
252256 console . log ( "win quitting or updating" , this . waveWindowId ) ;
253257 return ;
254258 }
259+ waveWindowMap . delete ( this . waveWindowId ) ;
260+ if ( focusedWaveWindow == this ) {
261+ focusedWaveWindow = null ;
262+ }
263+ this . removeAllChildViews ( ) ;
255264 if ( getGlobalIsRelaunching ( ) ) {
256265 console . log ( "win relaunching" , this . waveWindowId ) ;
257266 this . destroy ( ) ;
@@ -266,17 +275,19 @@ export class WaveBrowserWindow extends BaseWindow {
266275 console . log ( "win removing window from backend DB" , this . waveWindowId ) ;
267276 fireAndForget ( ( ) => WindowService . CloseWindow ( this . waveWindowId , true ) ) ;
268277 }
269- for ( const tabView of this . allLoadedTabViews . values ( ) ) {
270- tabView ?. destroy ( ) ;
271- }
272- waveWindowMap . delete ( this . waveWindowId ) ;
273- if ( focusedWaveWindow == this ) {
274- focusedWaveWindow = null ;
275- }
276278 } ) ;
277279 waveWindowMap . set ( waveWindow . oid , this ) ;
278280 }
279281
282+ removeAllChildViews ( ) {
283+ for ( const tabView of this . allLoadedTabViews . values ( ) ) {
284+ if ( ! this . isDestroyed ( ) ) {
285+ this . contentView . removeChildView ( tabView ) ;
286+ }
287+ tabView ?. destroy ( ) ;
288+ }
289+ }
290+
280291 async switchWorkspace ( workspaceId : string ) {
281292 console . log ( "switchWorkspace" , workspaceId , this . waveWindowId ) ;
282293 if ( workspaceId == this . workspaceId ) {
@@ -311,12 +322,7 @@ export class WaveBrowserWindow extends BaseWindow {
311322 return ;
312323 }
313324 console . log ( "switchWorkspace newWs" , newWs ) ;
314- if ( this . allLoadedTabViews . size ) {
315- for ( const tab of this . allLoadedTabViews . values ( ) ) {
316- this . contentView . removeChildView ( tab ) ;
317- tab ?. destroy ( ) ;
318- }
319- }
325+ this . removeAllChildViews ( ) ;
320326 console . log ( "destroyed all tabs" , this . waveWindowId ) ;
321327 this . workspaceId = workspaceId ;
322328 this . allLoadedTabViews = new Map ( ) ;
@@ -329,22 +335,7 @@ export class WaveBrowserWindow extends BaseWindow {
329335 }
330336
331337 async closeTab ( tabId : string ) {
332- console . log ( `closeTab tabid=${ tabId } ws=${ this . workspaceId } window=${ this . waveWindowId } ` ) ;
333- const rtn = await WorkspaceService . CloseTab ( this . workspaceId , tabId , true ) ;
334- if ( rtn == null ) {
335- console . log ( "[error] closeTab: no return value" , tabId , this . workspaceId , this . waveWindowId ) ;
336- return ;
337- }
338- if ( rtn . closewindow ) {
339- this . close ( ) ;
340- return ;
341- }
342- if ( ! rtn . newactivetabid ) {
343- console . log ( "[error] closeTab, no new active tab" , tabId , this . workspaceId , this . waveWindowId ) ;
344- return ;
345- }
346- await this . setActiveTab ( rtn . newactivetabid , false ) ;
347- this . allLoadedTabViews . delete ( tabId ) ;
338+ await this . queueCloseTab ( tabId ) ;
348339 }
349340
350341 async initializeTab ( tabView : WaveTabView ) {
@@ -447,11 +438,15 @@ export class WaveBrowserWindow extends BaseWindow {
447438 }
448439
449440 async queueTabSwitch ( tabId : string , setInBackend : boolean ) {
450- await this . _queueTabSwitchInternal ( { createTab : false , tabId, setInBackend } ) ;
441+ await this . _queueTabSwitchInternal ( { op : "switch" , tabId, setInBackend } ) ;
451442 }
452443
453444 async queueCreateTab ( pinned = false ) {
454- await this . _queueTabSwitchInternal ( { createTab : true , pinned } ) ;
445+ await this . _queueTabSwitchInternal ( { op : "create" , pinned } ) ;
446+ }
447+
448+ async queueCloseTab ( tabId : string ) {
449+ await this . _queueTabSwitchInternal ( { op : "close" , tabId } ) ;
455450 }
456451
457452 async _queueTabSwitchInternal ( entry : TabSwitchQueueEntry ) {
@@ -466,6 +461,12 @@ export class WaveBrowserWindow extends BaseWindow {
466461 }
467462 }
468463
464+ removeTabViewLater ( tabId : string , delayMs : number ) {
465+ setTimeout ( ( ) => {
466+ this . removeTabView ( tabId , false ) ;
467+ } , 1000 ) ;
468+ }
469+
469470 // the queue and this function are used to serialize tab switches
470471 // [0] => the tab that is currently being switched to
471472 // [1] => the tab that will be switched to next
@@ -478,10 +479,10 @@ export class WaveBrowserWindow extends BaseWindow {
478479 const entry = this . tabSwitchQueue [ 0 ] ;
479480 let tabId : string = null ;
480481 // have to use "===" here to get the typechecker to work :/
481- if ( entry . createTab === true ) {
482+ if ( entry . op === "create" ) {
482483 const { pinned } = entry ;
483484 tabId = await WorkspaceService . CreateTab ( this . workspaceId , null , true , pinned ) ;
484- } else if ( entry . createTab === false ) {
485+ } else if ( entry . op === "switch" ) {
485486 let setInBackend : boolean = false ;
486487 ( { tabId, setInBackend } = entry ) ;
487488 if ( this . activeTabView ?. waveTabId == tabId ) {
@@ -490,11 +491,28 @@ export class WaveBrowserWindow extends BaseWindow {
490491 if ( setInBackend ) {
491492 await WorkspaceService . SetActiveTab ( this . workspaceId , tabId ) ;
492493 }
494+ } else if ( entry . op === "close" ) {
495+ console . log ( "processTabSwitchQueue closeTab" , entry . tabId ) ;
496+ tabId = entry . tabId ;
497+ const rtn = await WorkspaceService . CloseTab ( this . workspaceId , tabId , true ) ;
498+ if ( rtn == null ) {
499+ console . log ( "[error] closeTab: no return value" , tabId , this . workspaceId , this . waveWindowId ) ;
500+ return ;
501+ }
502+ this . removeTabViewLater ( tabId , 1000 ) ;
503+ if ( rtn . closewindow ) {
504+ this . close ( ) ;
505+ return ;
506+ }
507+ if ( ! rtn . newactivetabid ) {
508+ return ;
509+ }
510+ tabId = rtn . newactivetabid ;
493511 }
494512 if ( tabId == null ) {
495513 return ;
496514 }
497- const [ tabView , tabInitialized ] = await getOrCreateWebViewForTab ( tabId ) ;
515+ const [ tabView , tabInitialized ] = await getOrCreateWebViewForTab ( this . waveWindowId , tabId ) ;
498516 await this . setTabViewIntoWindow ( tabView , tabInitialized ) ;
499517 } catch ( e ) {
500518 console . log ( "error caught in processTabSwitchQueue" , e ) ;
@@ -520,6 +538,22 @@ export class WaveBrowserWindow extends BaseWindow {
520538 }
521539 }
522540
541+ removeTabView ( tabId : string , force : boolean ) {
542+ if ( ! force && this . activeTabView ?. waveTabId == tabId ) {
543+ console . log ( "cannot remove active tab" , tabId , this . waveWindowId ) ;
544+ return ;
545+ }
546+ const tabView = this . allLoadedTabViews . get ( tabId ) ;
547+ if ( tabView == null ) {
548+ console . log ( "removeTabView -- tabView not found" , tabId , this . waveWindowId ) ;
549+ // the tab was never loaded, so just return
550+ return ;
551+ }
552+ this . contentView . removeChildView ( tabView ) ;
553+ this . allLoadedTabViews . delete ( tabId ) ;
554+ tabView . destroy ( ) ;
555+ }
556+
523557 destroy ( ) {
524558 console . log ( "destroy win" , this . waveWindowId ) ;
525559 this . deleteAllowed = true ;
@@ -607,9 +641,7 @@ ipcMain.on("close-tab", async (event, workspaceId, tabId) => {
607641 console . log ( `close-tab: no window found for workspace ws=${ workspaceId } tab=${ tabId } ` ) ;
608642 return ;
609643 }
610- if ( ww != null ) {
611- await ww . closeTab ( tabId ) ;
612- }
644+ await ww . queueCloseTab ( tabId ) ;
613645 event . returnValue = true ;
614646 return null ;
615647} ) ;
@@ -685,9 +717,12 @@ export async function relaunchBrowserWindows() {
685717 console . log ( "relaunchBrowserWindows" ) ;
686718 setGlobalIsRelaunching ( true ) ;
687719 const windows = getAllWaveWindows ( ) ;
688- for ( const window of windows ) {
689- console . log ( "relaunch -- closing window" , window . waveWindowId ) ;
690- window . close ( ) ;
720+ if ( windows . length > 0 ) {
721+ for ( const window of windows ) {
722+ console . log ( "relaunch -- closing window" , window . waveWindowId ) ;
723+ window . close ( ) ;
724+ }
725+ await delay ( 1200 ) ;
691726 }
692727 setGlobalIsRelaunching ( false ) ;
693728
0 commit comments