fix: reattach the terminal after an app restart
The terminal session id lived only in memory, so the first time the panel opened after a launch it always created another PTY. The old shell stayed on the server (unattached shells live for half an hour) but the client could no longer find it. The id is now remembered per connection and directory. On first open the panel lists /api/terminal/sessions?cwd=<dir> and attaches to the remembered session while the server still has it, showing its replayed history. Without that id it adopts the newest running session of the directory, and only when the directory has nothing does it create one. An explicit "close terminal" forgets the id and forces a fresh shell, and a fatal SESSION_NOT_FOUND clears it and recovers, so a server restart cannot leave a dead panel. The panel is still one terminal per project, not a tab strip. 0.4.2 (402).
This commit is contained in:
1 parent
d25167de58
commit
675d19d563
9 files changed
+287
-11
No files matched your search
@@ -67,6 +67,7 @@ class Connection {
|
||||
class Storage {
|
||||
static const _connsKey = 'openchamber.connections';
|
||||
static const _installKey = 'openchamber.installId';
|
||||
static const _terminalPrefix = 'openchamber.terminalSessions.';
|
||||
// v11 encrypts with AES-GCM and an Android Keystore-wrapped key by default.
|
||||
final _secure = const FlutterSecureStorage();
|
||||
|
||||
@@ -92,6 +93,59 @@ class Storage {
|
||||
await prefs.setString(_connsKey, jsonEncode(sorted.map((e) => e.toJson()).toList()));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------- terminals
|
||||
|
||||
// The last terminal session per directory, so reopening the terminal after
|
||||
// an app restart can reattach to the shell the server still holds instead of
|
||||
// starting another one. Ids are meaningless across servers, hence one map
|
||||
// per connection.
|
||||
|
||||
static String _terminalKey(String connectionId) =>
|
||||
'$_terminalPrefix$connectionId';
|
||||
|
||||
Future<Map<String, String>> _terminalSessions(String connectionId) async {
|
||||
if (connectionId.isEmpty) return {};
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final raw = prefs.getString(_terminalKey(connectionId));
|
||||
if (raw == null || raw.isEmpty) return {};
|
||||
try {
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is! Map) return {};
|
||||
return {
|
||||
for (final entry in decoded.entries)
|
||||
if (entry.key is String && entry.value is String)
|
||||
entry.key as String: entry.value as String,
|
||||
};
|
||||
} catch (_) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> terminalSession(String connectionId, String directory) async =>
|
||||
(await _terminalSessions(connectionId))[directory];
|
||||
|
||||
Future<void> saveTerminalSession(
|
||||
String connectionId, String directory, String sessionId) async {
|
||||
if (connectionId.isEmpty || directory.isEmpty || sessionId.isEmpty) return;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final map = await _terminalSessions(connectionId);
|
||||
map[directory] = sessionId;
|
||||
await prefs.setString(_terminalKey(connectionId), jsonEncode(map));
|
||||
}
|
||||
|
||||
Future<void> clearTerminalSession(
|
||||
String connectionId, String directory) async {
|
||||
if (connectionId.isEmpty) return;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final map = await _terminalSessions(connectionId);
|
||||
if (map.remove(directory) == null) return;
|
||||
if (map.isEmpty) {
|
||||
await prefs.remove(_terminalKey(connectionId));
|
||||
} else {
|
||||
await prefs.setString(_terminalKey(connectionId), jsonEncode(map));
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> installId() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
var id = prefs.getString(_installKey);
|
||||
|
||||
Reference in new issue
Block a user