Skip to content

Commit 2e2e93c

Browse files
fix: do not restore terminals if axs is dead (Acode-Foundation#1664)
* fix: do not restore terminals if axs is dead * format
1 parent d17459d commit 2e2e93c

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/components/terminal/terminalManager.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ class TerminalManager {
1919
this.terminalCounter = 0;
2020
}
2121

22-
getPersistedSessions() {
22+
async getPersistedSessions() {
2323
try {
2424
const stored = helpers.parseJSON(
2525
localStorage.getItem(TERMINAL_SESSION_STORAGE_KEY),
2626
);
2727
if (!Array.isArray(stored)) return [];
28+
if (!(await Terminal.isAxsRunning())) {
29+
return [];
30+
}
2831
return stored
2932
.map((entry) => {
3033
if (!entry) return null;
@@ -58,11 +61,11 @@ class TerminalManager {
5861
}
5962
}
6063

61-
persistTerminalSession(pid, name) {
64+
async persistTerminalSession(pid, name) {
6265
if (!pid) return;
6366

6467
const pidStr = String(pid);
65-
const sessions = this.getPersistedSessions();
68+
const sessions = await this.getPersistedSessions();
6669
const existingIndex = sessions.findIndex(
6770
(session) => session.pid === pidStr,
6871
);
@@ -83,11 +86,11 @@ class TerminalManager {
8386
this.savePersistedSessions(sessions);
8487
}
8588

86-
removePersistedSession(pid) {
89+
async removePersistedSession(pid) {
8790
if (!pid) return;
8891

8992
const pidStr = String(pid);
90-
const sessions = this.getPersistedSessions();
93+
const sessions = await this.getPersistedSessions();
9194
const nextSessions = sessions.filter((session) => session.pid !== pidStr);
9295

9396
if (nextSessions.length !== sessions.length) {
@@ -96,7 +99,7 @@ class TerminalManager {
9699
}
97100

98101
async restorePersistedSessions() {
99-
const sessions = this.getPersistedSessions();
102+
const sessions = await this.getPersistedSessions();
100103
if (!sessions.length) return;
101104

102105
const manager = window.editorManager;
@@ -185,7 +188,7 @@ class TerminalManager {
185188
});
186189

187190
// Wait for tab creation and setup
188-
const terminalInstance = await new Promise((resolve, reject) => {
191+
return await new Promise((resolve, reject) => {
189192
setTimeout(async () => {
190193
try {
191194
// Mount terminal component
@@ -222,7 +225,10 @@ class TerminalManager {
222225
this.terminals.set(uniqueId, instance);
223226

224227
if (terminalComponent.serverMode && terminalComponent.pid) {
225-
this.persistTerminalSession(terminalComponent.pid, terminalName);
228+
await this.persistTerminalSession(
229+
terminalComponent.pid,
230+
terminalName,
231+
);
226232
}
227233
resolve(instance);
228234
} catch (error) {
@@ -231,8 +237,6 @@ class TerminalManager {
231237
}
232238
}, 100);
233239
});
234-
235-
return terminalInstance;
236240
} catch (error) {
237241
console.error("Failed to create terminal:", error);
238242
throw error;
@@ -336,7 +340,7 @@ class TerminalManager {
336340
});
337341

338342
// Wait for tab creation and setup
339-
const terminalInstance = await new Promise((resolve, reject) => {
343+
return await new Promise((resolve, reject) => {
340344
setTimeout(async () => {
341345
try {
342346
// Mount terminal component
@@ -376,8 +380,6 @@ class TerminalManager {
376380
}
377381
}, 100);
378382
});
379-
380-
return terminalInstance;
381383
}
382384

383385
/**
@@ -386,7 +388,7 @@ class TerminalManager {
386388
* @param {TerminalComponent} terminalComponent - Terminal component
387389
* @param {string} terminalId - Terminal ID
388390
*/
389-
setupTerminalHandlers(terminalFile, terminalComponent, terminalId) {
391+
async setupTerminalHandlers(terminalFile, terminalComponent, terminalId) {
390392
// Handle tab focus/blur
391393
terminalFile.onfocus = () => {
392394
// Guarded fit on focus: only fit if cols/rows would change, then focus
@@ -504,14 +506,17 @@ class TerminalManager {
504506
this.closeTerminal(terminalId);
505507
};
506508

507-
terminalComponent.onTitleChange = (title) => {
509+
terminalComponent.onTitleChange = async (title) => {
508510
if (title) {
509511
// Format terminal title as "Terminal ! - title"
510512
const formattedTitle = `Terminal ${this.terminalCounter} - ${title}`;
511513
terminalFile.filename = formattedTitle;
512514

513515
if (terminalComponent.serverMode && terminalComponent.pid) {
514-
this.persistTerminalSession(terminalComponent.pid, formattedTitle);
516+
await this.persistTerminalSession(
517+
terminalComponent.pid,
518+
formattedTitle,
519+
);
515520
}
516521

517522
// Refresh the header subtitle if this terminal is active

0 commit comments

Comments
 (0)