/** * Tauri adapter for {@link SystemGateway}. This is the *only* place (together * with the sibling adapters) that touches `@tauri-apps/api`. Components reach it * exclusively through the port — never `invoke()` directly. */ import { invoke } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { open } from "@tauri-apps/plugin-dialog"; import type { DomainEvent, HealthReport, Unsubscribe } from "@/domain"; import type { SystemGateway } from "@/ports"; /** Tauri event name carrying relayed domain events (mirror of `DOMAIN_EVENT`). */ const DOMAIN_EVENT = "domain://event"; export class TauriSystemGateway implements SystemGateway { async health(note?: string): Promise { // The backend command takes an optional `request: { note }` (camelCase). return invoke("health", { request: note === undefined ? null : { note }, }); } async onDomainEvent( handler: (event: DomainEvent) => void, ): Promise { const unlisten = await listen(DOMAIN_EVENT, (e) => { handler(e.payload); }); return unlisten; } async pickFolder(): Promise { const result = await open({ directory: true, multiple: false }); if (result === null || result === undefined) return null; // `open` with `multiple: false` returns a string when a path is chosen. return typeof result === "string" ? result : null; } }