@@ -17,12 +17,15 @@ class TerminalManager {
1717 this . terminalCounter = 0 ;
1818 }
1919
20- getPersistedSessions ( ) {
20+ async getPersistedSessions ( ) {
2121 try {
2222 const stored = helpers . parseJSON (
2323 localStorage . getItem ( TERMINAL_SESSION_STORAGE_KEY ) ,
2424 ) ;
2525 if ( ! Array . isArray ( stored ) ) return [ ] ;
26+ if ( ! await Terminal . isAxsRunning ( ) ) {
27+ return [ ] ;
28+ }
2629 return stored
2730 . map ( ( entry ) => {
2831 if ( ! entry ) return null ;
@@ -56,11 +59,11 @@ class TerminalManager {
5659 }
5760 }
5861
59- persistTerminalSession ( pid , name ) {
62+ async persistTerminalSession ( pid , name ) {
6063 if ( ! pid ) return ;
6164
6265 const pidStr = String ( pid ) ;
63- const sessions = this . getPersistedSessions ( ) ;
66+ const sessions = await this . getPersistedSessions ( ) ;
6467 const existingIndex = sessions . findIndex (
6568 ( session ) => session . pid === pidStr ,
6669 ) ;
@@ -81,11 +84,11 @@ class TerminalManager {
8184 this . savePersistedSessions ( sessions ) ;
8285 }
8386
84- removePersistedSession ( pid ) {
87+ async removePersistedSession ( pid ) {
8588 if ( ! pid ) return ;
8689
8790 const pidStr = String ( pid ) ;
88- const sessions = this . getPersistedSessions ( ) ;
91+ const sessions = await this . getPersistedSessions ( ) ;
8992 const nextSessions = sessions . filter ( ( session ) => session . pid !== pidStr ) ;
9093
9194 if ( nextSessions . length !== sessions . length ) {
@@ -94,7 +97,7 @@ class TerminalManager {
9497 }
9598
9699 async restorePersistedSessions ( ) {
97- const sessions = this . getPersistedSessions ( ) ;
100+ const sessions = await this . getPersistedSessions ( ) ;
98101 if ( ! sessions . length ) return ;
99102
100103 const manager = window . editorManager ;
@@ -183,54 +186,52 @@ class TerminalManager {
183186 } ) ;
184187
185188 // Wait for tab creation and setup
186- const terminalInstance = await new Promise ( ( resolve , reject ) => {
187- setTimeout ( async ( ) => {
188- try {
189- // Mount terminal component
190- terminalComponent . mount ( terminalContainer ) ;
191-
192- // Connect to session if in server mode
193- if ( terminalComponent . serverMode ) {
194- await terminalComponent . connectToSession ( terminalOptions . pid ) ;
195- } else {
196- // For local mode, just write a welcome message
197- terminalComponent . write (
198- "Local terminal mode - ready for output\r\n" ,
199- ) ;
200- }
201-
202- // Use PID as unique ID if available, otherwise fall back to terminalId
203- const uniqueId = terminalComponent . pid || terminalId ;
204-
205- // Setup event handlers
206- this . setupTerminalHandlers (
207- terminalFile ,
208- terminalComponent ,
209- uniqueId ,
210- ) ;
211-
212- const instance = {
213- id : uniqueId ,
214- name : terminalName ,
215- component : terminalComponent ,
216- file : terminalFile ,
217- container : terminalContainer ,
218- } ;
219-
220- this . terminals . set ( uniqueId , instance ) ;
221-
222- if ( terminalComponent . serverMode && terminalComponent . pid ) {
223- this . persistTerminalSession ( terminalComponent . pid , terminalName ) ;
224- }
225- resolve ( instance ) ;
226- } catch ( error ) {
227- console . error ( "Failed to initialize terminal:" , error ) ;
228- reject ( error ) ;
229- }
230- } , 100 ) ;
231- } ) ;
232-
233- return terminalInstance ;
189+ return await new Promise ( ( resolve , reject ) => {
190+ setTimeout ( async ( ) => {
191+ try {
192+ // Mount terminal component
193+ terminalComponent . mount ( terminalContainer ) ;
194+
195+ // Connect to session if in server mode
196+ if ( terminalComponent . serverMode ) {
197+ await terminalComponent . connectToSession ( terminalOptions . pid ) ;
198+ } else {
199+ // For local mode, just write a welcome message
200+ terminalComponent . write (
201+ "Local terminal mode - ready for output\r\n" ,
202+ ) ;
203+ }
204+
205+ // Use PID as unique ID if available, otherwise fall back to terminalId
206+ const uniqueId = terminalComponent . pid || terminalId ;
207+
208+ // Setup event handlers
209+ this . setupTerminalHandlers (
210+ terminalFile ,
211+ terminalComponent ,
212+ uniqueId ,
213+ ) ;
214+
215+ const instance = {
216+ id : uniqueId ,
217+ name : terminalName ,
218+ component : terminalComponent ,
219+ file : terminalFile ,
220+ container : terminalContainer ,
221+ } ;
222+
223+ this . terminals . set ( uniqueId , instance ) ;
224+
225+ if ( terminalComponent . serverMode && terminalComponent . pid ) {
226+ await this . persistTerminalSession ( terminalComponent . pid , terminalName ) ;
227+ }
228+ resolve ( instance ) ;
229+ } catch ( error ) {
230+ console . error ( "Failed to initialize terminal:" , error ) ;
231+ reject ( error ) ;
232+ }
233+ } , 100 ) ;
234+ } ) ;
234235 } catch ( error ) {
235236 console . error ( "Failed to create terminal:" , error ) ;
236237 throw error ;
@@ -334,48 +335,46 @@ class TerminalManager {
334335 } ) ;
335336
336337 // Wait for tab creation and setup
337- const terminalInstance = await new Promise ( ( resolve , reject ) => {
338- setTimeout ( async ( ) => {
339- try {
340- // Mount terminal component
341- terminalComponent . mount ( terminalContainer ) ;
342-
343- // Write initial message
344- terminalComponent . write ( "🚀 Installing Terminal Environment...\r\n" ) ;
345- terminalComponent . write (
346- "This may take a few minutes depending on your connection.\r\n\r\n" ,
347- ) ;
348-
349- // Setup event handlers
350- this . setupTerminalHandlers (
351- terminalFile ,
352- terminalComponent ,
353- terminalId ,
354- ) ;
355-
356- // Set up custom title for installation terminal
357- terminalFile . setCustomTitle (
358- ( ) => "Installing Terminal Environment..." ,
359- ) ;
360-
361- const instance = {
362- id : terminalId ,
363- name : terminalName ,
364- component : terminalComponent ,
365- file : terminalFile ,
366- container : terminalContainer ,
367- } ;
368-
369- this . terminals . set ( terminalId , instance ) ;
370- resolve ( instance ) ;
371- } catch ( error ) {
372- console . error ( "Failed to create installation terminal:" , error ) ;
373- reject ( error ) ;
374- }
375- } , 100 ) ;
376- } ) ;
377-
378- return terminalInstance ;
338+ return await new Promise ( ( resolve , reject ) => {
339+ setTimeout ( async ( ) => {
340+ try {
341+ // Mount terminal component
342+ terminalComponent . mount ( terminalContainer ) ;
343+
344+ // Write initial message
345+ terminalComponent . write ( "🚀 Installing Terminal Environment...\r\n" ) ;
346+ terminalComponent . write (
347+ "This may take a few minutes depending on your connection.\r\n\r\n" ,
348+ ) ;
349+
350+ // Setup event handlers
351+ this . setupTerminalHandlers (
352+ terminalFile ,
353+ terminalComponent ,
354+ terminalId ,
355+ ) ;
356+
357+ // Set up custom title for installation terminal
358+ terminalFile . setCustomTitle (
359+ ( ) => "Installing Terminal Environment..." ,
360+ ) ;
361+
362+ const instance = {
363+ id : terminalId ,
364+ name : terminalName ,
365+ component : terminalComponent ,
366+ file : terminalFile ,
367+ container : terminalContainer ,
368+ } ;
369+
370+ this . terminals . set ( terminalId , instance ) ;
371+ resolve ( instance ) ;
372+ } catch ( error ) {
373+ console . error ( "Failed to create installation terminal:" , error ) ;
374+ reject ( error ) ;
375+ }
376+ } , 100 ) ;
377+ } ) ;
379378 }
380379
381380 /**
@@ -384,7 +383,7 @@ class TerminalManager {
384383 * @param {TerminalComponent } terminalComponent - Terminal component
385384 * @param {string } terminalId - Terminal ID
386385 */
387- setupTerminalHandlers ( terminalFile , terminalComponent , terminalId ) {
386+ async setupTerminalHandlers ( terminalFile , terminalComponent , terminalId ) {
388387 // Handle tab focus/blur
389388 terminalFile . onfocus = ( ) => {
390389 // Guarded fit on focus: only fit if cols/rows would change, then focus
@@ -486,26 +485,26 @@ class TerminalManager {
486485 this . closeTerminal ( terminalId ) ;
487486 } ;
488487
489- terminalComponent . onTitleChange = ( title ) => {
490- if ( title ) {
491- // Format terminal title as "Terminal ! - title"
492- const formattedTitle = `Terminal ${ this . terminalCounter } - ${ title } ` ;
493- terminalFile . filename = formattedTitle ;
494-
495- if ( terminalComponent . serverMode && terminalComponent . pid ) {
496- this . persistTerminalSession ( terminalComponent . pid , formattedTitle ) ;
497- }
498-
499- // Refresh the header subtitle if this terminal is active
500- if (
501- editorManager . activeFile &&
502- editorManager . activeFile . id === terminalFile . id
503- ) {
504- // Force refresh of the header subtitle
505- terminalFile . setCustomTitle ( getTerminalTitle ) ;
506- }
507- }
508- } ;
488+ terminalComponent . onTitleChange = async ( title ) => {
489+ if ( title ) {
490+ // Format terminal title as "Terminal ! - title"
491+ const formattedTitle = `Terminal ${ this . terminalCounter } - ${ title } ` ;
492+ terminalFile . filename = formattedTitle ;
493+
494+ if ( terminalComponent . serverMode && terminalComponent . pid ) {
495+ await this . persistTerminalSession ( terminalComponent . pid , formattedTitle ) ;
496+ }
497+
498+ // Refresh the header subtitle if this terminal is active
499+ if (
500+ editorManager . activeFile &&
501+ editorManager . activeFile . id === terminalFile . id
502+ ) {
503+ // Force refresh of the header subtitle
504+ terminalFile . setCustomTitle ( getTerminalTitle ) ;
505+ }
506+ }
507+ } ;
509508
510509 terminalComponent . onProcessExit = ( exitData ) => {
511510 // Format exit message based on exit code and signal
0 commit comments