diff --git a/src/features/services/connectionState.test.ts b/src/features/services/connectionState.test.ts new file mode 100644 index 000000000..2812a475d --- /dev/null +++ b/src/features/services/connectionState.test.ts @@ -0,0 +1,51 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +const backend = vi.hoisted(() => ({ snapshot: vi.fn(), events: vi.fn(), cancel: vi.fn(), settings: vi.fn() })); +vi.mock("@tauri-apps/api/core", () => ({ Channel: class {} })); +vi.mock("@/platform/ipc", async (original) => ({ + ...(await original()), + isTauri: () => true, + commands: { + dockerSettings: backend.settings, + dockerSnapshot: backend.snapshot, + dockerEvents: backend.events, + cancelRequest: backend.cancel, + }, +})); +import { connectConnection, connectionState, disconnectConnection, refresh, useServices, watch } from "./store"; + +const ready = { availability: { kind: "ready", context: null }, errors: [] }; +const ok = (data: T) => ({ status: "ok" as const, data }); + +beforeEach(() => { + for (const f of Object.values(backend)) f.mockReset(); + backend.snapshot.mockResolvedValue(ok(ready)); + backend.events.mockReturnValue(new Promise(() => {})); + backend.settings.mockResolvedValue( + ok({ connections: [{ id: "box", name: "Box", endpoint: { kind: "default" } }], registries: [] }), + ); + useServices.setState({ + settings: { connections: [{ id: "box", name: "Box", endpoint: { kind: "default" } }], registries: [] } as never, + conns: {}, + }); +}); + +describe("Docker connections", () => { + it("stop talking to the daemon while disconnected, and connect again on Connect", async () => { + const stop = watch(); + await vi.waitFor(() => expect(connectionState("box").snapshot).not.toBeNull()); + expect(backend.events).toHaveBeenCalledOnce(); + + disconnectConnection("box"); + expect(backend.cancel).toHaveBeenCalledOnce(); + expect(connectionState("box")).toMatchObject({ snapshot: null, disconnected: true }); + backend.snapshot.mockClear(); + await refresh("box"); + expect(backend.snapshot).not.toHaveBeenCalled(); + + await connectConnection("box"); + expect(backend.snapshot).toHaveBeenCalledOnce(); + expect(connectionState("box")).toMatchObject({ disconnected: false, snapshot: ready }); + stop(); + }); +}); diff --git a/src/features/services/docker.tsx b/src/features/services/docker.tsx index de95bcc26..c2050ce5d 100644 --- a/src/features/services/docker.tsx +++ b/src/features/services/docker.tsx @@ -74,7 +74,9 @@ import { } from "./ops"; import { cancelTask, + connectConnection, connectionState, + disconnectConnection, endpointOf, errorMessage, refresh, @@ -110,6 +112,7 @@ function endpointText(ep: DockerEndpoint): string { } function connectionDetail(state: ConnectionState): string | undefined { + if (state.disconnected) return "disconnected"; const a = state.snapshot?.availability; if (!a) return state.error ?? (state.loading ? "connecting…" : undefined); if (a.kind === "notInstalled") return "docker not installed"; @@ -250,6 +253,9 @@ function menu(item: ServiceTreeItem): MenuItem[] { if (c) items.push( { kind: "separator" }, + connectionState(c.id).disconnected + ? { label: "Connect", run: () => void connectConnection(c.id) } + : { label: "Disconnect", run: () => disconnectConnection(c.id) }, { label: "Edit Connection…", icon: "edit", run: () => openConnectionDialog(c) }, { label: "Remove Connection", icon: "trash", danger: true, run: () => void removeConnectionPrompt(c) }, ); @@ -439,6 +445,13 @@ function DockerDetail({ item }: { item: ServiceTreeItem }) { if (!target || !parsed || !state || !node) return ; const conn = connectionOf(parsed.conn); const snapshot = state.snapshot; + if (state.disconnected) + return ( + void connectConnection(parsed.conn)}>Connect} + /> + ); if (!snapshot) return (
diff --git a/src/features/services/index.ts b/src/features/services/index.ts index 6bcb2acdd..4fbb1612a 100644 --- a/src/features/services/index.ts +++ b/src/features/services/index.ts @@ -52,7 +52,15 @@ import { } from "./ops"; import { SERVICES_VIEW_FOCUS, ServicesTreeView, showAddServiceMenu } from "./ServicesTree"; import { ServicesHeaderActions, ServicesHost, ServicesTabs } from "./ServicesHost"; -import { connections, refresh, setDockerFilters, useServices } from "./store"; +import { + connectConnection, + connectionState, + connections, + disconnectConnection, + refresh, + setDockerFilters, + useServices, +} from "./store"; import { SERVICES_TREE_VIEW } from "./tree"; import { isRunning } from "./model"; import type { PruneTarget } from "@/platform/ipc"; @@ -143,6 +151,26 @@ export function activate(): () => void { when: PROJECT, run: () => refresh(), }, + { + id: "docker.connect", + title: "Connect to Docker", + category: "Docker", + when: PROJECT, + run: () => { + const id = currentConnection(); + if (id && connectionState(id).disconnected) void connectConnection(id); + }, + }, + { + id: "docker.disconnect", + title: "Disconnect from Docker", + category: "Docker", + when: PROJECT, + run: () => { + const id = currentConnection(); + if (id && !connectionState(id).disconnected) disconnectConnection(id); + }, + }, { id: "docker.start", title: "Start Container", diff --git a/src/features/services/store.ts b/src/features/services/store.ts index f88250453..1ffacbeee 100644 --- a/src/features/services/store.ts +++ b/src/features/services/store.ts @@ -58,6 +58,8 @@ export interface ConnectionState { snapshot: DockerSnapshot | null; loading: boolean; error: string | null; + /** Disconnected for this session: no snapshots, events or polling until Connect. */ + disconnected?: boolean; } interface DockerState { @@ -231,6 +233,7 @@ const again = new Set(); export function refresh(id?: string): Promise { if (!isTauri()) return Promise.resolve(); if (id === undefined) return Promise.all(connections().map((c) => refresh(c.id))).then(() => undefined); + if (connectionState(id).disconnected) return Promise.resolve(); const running = inflight.get(id); if (running) { again.add(id); @@ -239,12 +242,14 @@ export function refresh(id?: string): Promise { patchConn(id, { loading: true }); const p = call(commands.dockerSnapshot(endpointOf(id))) .then((snapshot) => { - if (!connections().some((c) => c.id === id)) return; + if (!connections().some((c) => c.id === id) || connectionState(id).disconnected) return; const wasReady = connectionState(id).snapshot?.availability.kind === "ready"; patchConn(id, { snapshot, error: null }); if (snapshot.availability.kind === "ready" && !wasReady) startEvents(id); }) - .catch((e) => patchConn(id, { error: errorMessage(e) })) + .catch((e) => { + if (!connectionState(id).disconnected) patchConn(id, { error: errorMessage(e) }); + }) .finally(() => { inflight.delete(id); if (connections().some((c) => c.id === id)) patchConn(id, { loading: false }); @@ -268,7 +273,7 @@ let pollTimer: ReturnType | undefined; let watchers = 0; function startEvents(id: string) { - if (events.has(id) || watchers === 0) return; + if (events.has(id) || watchers === 0 || connectionState(id).disconnected) return; const request = nextRequestId(); events.set(id, request); const channel = new Channel(); @@ -315,6 +320,22 @@ export function watch(): () => void { }; } +/** + * Disconnect: stops talking to the connection's daemon (its event stream and polling; for an + * SSH connection, the SSH sessions they open) until Connect. Not saved: a new session connects. + */ +export function disconnectConnection(id: string): void { + stopEvents(id); + clearTimeout(debounces.get(id)); + again.delete(id); + patchConn(id, { snapshot: null, error: null, loading: false, disconnected: true }); +} + +export function connectConnection(id: string): Promise { + patchConn(id, { disconnected: false }); + return refresh(id); +} + // --------------------------------------------------------------------------- // Operations diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index b9cb7db85..5aff98dc1 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -1089,6 +1089,11 @@ "Conflicts": "冲突", "Conflicts:": "冲突:", "Connect": "连接", + "Connect to Docker": "连接到 Docker", + "Disconnect": "断开连接", + "Disconnect from Docker": "断开与 Docker 的连接", + "Disconnected": "已断开连接", + "disconnected": "已断开连接", "Connected": "已连接", "Conda Environment": "Conda 环境", "Connecting to Docker…": "正在连接 Docker…",