generated from ali/HarmonyTemplate
- session/TerminalSession: kit-free connect orchestrator (CONNECTING -> HOSTKEY_WAIT -> AUTHENTICATING/AUTH_REQUIRED -> READY/CLOSED/ERROR), fresh SshSession per attempt, inline password retry with optional remember+persist, friendly error mapping - session/SessionManager: process-wide session registry with listener notify and active-session tracking - ui/SessionLaunch: SessionDeps assembly (repos + TOFU dialog + crypto envelopes), host-key AlertDialog (save/once/reject), toast/clipboard, quick-connect and host-profile spec builders - pages/Index: bottom Tabs shell (connections/terminal/keys/settings) with AppStorage-driven cross-page tab switching - ui/ConnectionsPage: quick-connect bar, search, LazyForEach host list, long-press context menu (edit/copy/forget-password/delete), FAB + bindContentCover editor, delete/forget confirm dialogs - ui/HostEditSheet: full-screen host editor (label/host/port/user, password/key auth chips, key Select, 8-color tag picker) - ui/TerminalPage: session tab strip + per-state overlays (progress, inline password prompt, error/closed with reconnect) - ui/KeysPage/SettingsPage: stage placeholders (diag entry kept) - ssh/SshSession: optional onClosed callback for transport loss - data/KnownHostRepository: fix missing KnownHostStore import (T8 post-build regression)
114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
/*
|
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
|
*
|
|
* T9: process-wide registry of live TerminalSessions. Pages subscribe via
|
|
* subscribe() and re-read all()/getActive() on every notification.
|
|
*/
|
|
|
|
import { TerminalSession } from './TerminalSession';
|
|
import type { ConnectSpec, SessionDeps } from './TerminalSession';
|
|
|
|
export class SessionManager {
|
|
private static instance: SessionManager | null = null;
|
|
|
|
static getInstance(): SessionManager {
|
|
if (SessionManager.instance === null) {
|
|
SessionManager.instance = new SessionManager();
|
|
}
|
|
return SessionManager.instance;
|
|
}
|
|
|
|
private readonly sessions: Map<number, TerminalSession> = new Map();
|
|
private readonly listeners: Array<() => void> = [];
|
|
private nextId: number = 1;
|
|
private activeId: number | null = null;
|
|
|
|
/**
|
|
* Create a session, make it active and start dialing. The deps'
|
|
* onStateChange is replaced with a manager notify so every state move
|
|
* refreshes all subscribed pages.
|
|
*/
|
|
launch(spec: ConnectSpec, deps: SessionDeps): TerminalSession {
|
|
const manager: SessionManager = SessionManager.getInstance();
|
|
const wrapped: SessionDeps = {
|
|
knownHostStore: deps.knownHostStore,
|
|
decideHostKey: deps.decideHostKey,
|
|
loadPrivateKey: deps.loadPrivateKey,
|
|
decryptPassword: deps.decryptPassword,
|
|
encryptPassword: deps.encryptPassword,
|
|
persistPassword: deps.persistPassword,
|
|
markConnected: deps.markConnected,
|
|
onStateChange: (session: TerminalSession): void => {
|
|
manager.notify();
|
|
}
|
|
};
|
|
const session: TerminalSession = new TerminalSession(this.nextId, spec, wrapped);
|
|
this.nextId += 1;
|
|
this.sessions.set(session.getId(), session);
|
|
this.activeId = session.getId();
|
|
this.notify();
|
|
session.start();
|
|
return session;
|
|
}
|
|
|
|
get(id: number): TerminalSession | null {
|
|
const found: TerminalSession | undefined = this.sessions.get(id);
|
|
return found === undefined ? null : found;
|
|
}
|
|
|
|
all(): TerminalSession[] {
|
|
return Array.from(this.sessions.values());
|
|
}
|
|
|
|
getActiveId(): number | null {
|
|
return this.activeId;
|
|
}
|
|
|
|
getActive(): TerminalSession | null {
|
|
if (this.activeId === null) {
|
|
return null;
|
|
}
|
|
return this.get(this.activeId);
|
|
}
|
|
|
|
setActive(id: number): void {
|
|
if (this.sessions.has(id) && this.activeId !== id) {
|
|
this.activeId = id;
|
|
this.notify();
|
|
}
|
|
}
|
|
|
|
/** Close and forget a session; active falls back to the newest survivor. */
|
|
remove(id: number): void {
|
|
const session: TerminalSession | null = this.get(id);
|
|
if (session === null) {
|
|
return;
|
|
}
|
|
session.close();
|
|
this.sessions.delete(id);
|
|
if (this.activeId === id) {
|
|
const rest: TerminalSession[] = this.all();
|
|
this.activeId = rest.length > 0 ? rest[rest.length - 1].getId() : null;
|
|
}
|
|
this.notify();
|
|
}
|
|
|
|
subscribe(listener: () => void): () => void {
|
|
this.listeners.push(listener);
|
|
return (): void => {
|
|
const idx: number = this.listeners.indexOf(listener);
|
|
if (idx >= 0) {
|
|
this.listeners.splice(idx, 1);
|
|
}
|
|
};
|
|
}
|
|
|
|
notify(): void {
|
|
const snapshot: Array<() => void> = this.listeners.slice();
|
|
for (let i: number = 0; i < snapshot.length; i++) {
|
|
snapshot[i]();
|
|
}
|
|
}
|
|
}
|