@@ -51,30 +51,57 @@ class TerminalManager {
5151 }
5252
5353 normalizePersistedSessions ( stored ) {
54- if ( ! Array . isArray ( stored ) ) return [ ] ;
54+ if ( ! Array . isArray ( stored ) ) {
55+ return {
56+ sessions : [ ] ,
57+ changed : stored != null ,
58+ } ;
59+ }
5560
56- const sessions = stored
57- . map ( ( entry ) => {
58- if ( ! entry ) return null ;
59- if ( typeof entry === "string" ) {
60- return { pid : entry , name : `Terminal ${ entry } ` } ;
61- }
62- if ( typeof entry === "object" && entry . pid ) {
63- const pid = String ( entry . pid ) ;
64- return {
65- pid,
66- name : entry . name || `Terminal ${ pid } ` ,
67- } ;
68- }
69- return null ;
70- } )
71- . filter ( Boolean ) ;
61+ const sessions = [ ] ;
7262 const uniqueSessions = [ ] ;
7363 const seenPids = new Set ( ) ;
64+ let changed = false ;
65+
66+ for ( const entry of stored ) {
67+ if ( ! entry ) {
68+ changed = true ;
69+ continue ;
70+ }
71+
72+ if ( typeof entry === "string" ) {
73+ sessions . push ( {
74+ pid : entry ,
75+ name : `Terminal ${ entry } ` ,
76+ } ) ;
77+ changed = true ;
78+ continue ;
79+ }
80+
81+ if ( typeof entry !== "object" || ! entry . pid ) {
82+ changed = true ;
83+ continue ;
84+ }
85+
86+ const pid = String ( entry . pid ) ;
87+ const name =
88+ typeof entry . name === "string" && entry . name . trim ( )
89+ ? entry . name . trim ( )
90+ : `Terminal ${ pid } ` ;
91+
92+ if ( entry . pid !== pid || entry . name !== name ) {
93+ changed = true ;
94+ }
95+
96+ sessions . push ( { pid, name } ) ;
97+ }
7498
7599 for ( const session of sessions ) {
76100 const pid = String ( session . pid ) ;
77- if ( seenPids . has ( pid ) ) continue ;
101+ if ( seenPids . has ( pid ) ) {
102+ changed = true ;
103+ continue ;
104+ }
78105 seenPids . add ( pid ) ;
79106 uniqueSessions . push ( {
80107 pid,
@@ -85,7 +112,14 @@ class TerminalManager {
85112 } ) ;
86113 }
87114
88- return uniqueSessions ;
115+ if ( uniqueSessions . length !== stored . length ) {
116+ changed = true ;
117+ }
118+
119+ return {
120+ sessions : uniqueSessions ,
121+ changed,
122+ } ;
89123 }
90124
91125 readPersistedSessions ( ) {
@@ -95,25 +129,30 @@ class TerminalManager {
95129 ) ;
96130 } catch ( error ) {
97131 console . error ( "Failed to read persisted terminal sessions:" , error ) ;
98- return [ ] ;
132+ return {
133+ sessions : [ ] ,
134+ changed : false ,
135+ } ;
99136 }
100137 }
101138
102139 async getPersistedSessions ( ) {
103140 try {
104- const sessions = this . readPersistedSessions ( ) ;
105- if ( ! sessions . length ) return [ ] ;
141+ const { sessions, changed } = this . readPersistedSessions ( ) ;
142+ if ( ! sessions . length ) {
143+ if ( changed ) {
144+ this . savePersistedSessions ( [ ] ) ;
145+ }
146+ return [ ] ;
147+ }
106148
107149 if ( ! ( await Terminal . isAxsRunning ( ) ) ) {
108150 // Once the backend is gone, previously persisted PIDs are invalid.
109151 this . savePersistedSessions ( [ ] ) ;
110152 return [ ] ;
111153 }
112154
113- const stored = helpers . parseJSON (
114- localStorage . getItem ( TERMINAL_SESSION_STORAGE_KEY ) ,
115- ) ;
116- if ( Array . isArray ( stored ) && sessions . length !== stored . length ) {
155+ if ( changed ) {
117156 this . savePersistedSessions ( sessions ) ;
118157 }
119158
@@ -139,7 +178,7 @@ class TerminalManager {
139178 if ( ! pid ) return ;
140179
141180 const pidStr = String ( pid ) ;
142- const sessions = this . readPersistedSessions ( ) ;
181+ const { sessions } = this . readPersistedSessions ( ) ;
143182 const existingIndex = sessions . findIndex (
144183 ( session ) => session . pid === pidStr ,
145184 ) ;
@@ -164,7 +203,7 @@ class TerminalManager {
164203 if ( ! pid ) return ;
165204
166205 const pidStr = String ( pid ) ;
167- const sessions = this . readPersistedSessions ( ) ;
206+ const { sessions } = this . readPersistedSessions ( ) ;
168207 const nextSessions = sessions . filter ( ( session ) => session . pid !== pidStr ) ;
169208
170209 if ( nextSessions . length !== sessions . length ) {
0 commit comments