mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
IPC error kinds were compared as untyped strings (`(e as { kind?: string }).kind === "notFound"`), so a misspelt kind compiled and silently never matched. A typed `isErrorKind(error, kind)` helper, taking a kind from the generated `IdeError["kind"]`, now handles wrapped and raw IPC errors in document sync, editor session restore, Spring service identity, plugin commands and recent projects.
Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com>
123 lines
4.4 KiB
TypeScript
123 lines
4.4 KiB
TypeScript
/**
|
|
* Typed access to the Rust backend.
|
|
*
|
|
* `bindings.ts` is generated by tauri-specta; never edit it. Use `call` to turn
|
|
* its `{status, data | error}` results into plain promises that reject with
|
|
* `IpcError`, and `onIdeEvent` to subscribe to the backend event bus.
|
|
*/
|
|
import type { UnlistenFn } from "@tauri-apps/api/event";
|
|
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
|
|
import { commands, type IdeError, type IdeEvent, type RequestId } from "@/bindings";
|
|
import { notifyEach } from "./listeners";
|
|
|
|
export { commands };
|
|
export type * from "@/bindings";
|
|
|
|
export class IpcError extends Error {
|
|
readonly kind: IdeError["kind"];
|
|
readonly data: IdeError["data"];
|
|
constructor(error: IdeError) {
|
|
super(error.message);
|
|
this.name = "IpcError";
|
|
this.kind = error.kind;
|
|
this.data = error.data;
|
|
}
|
|
}
|
|
|
|
/** Matches both wrapped IPC errors and raw invoke rejections by kind. */
|
|
export function isErrorKind(e: unknown, kind: IdeError["kind"]): boolean {
|
|
if (typeof e !== "object" || e === null) return false;
|
|
try {
|
|
return "kind" in e && e.kind === kind;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
type Result<T> = { status: "ok"; data: T } | { status: "error"; error: IdeError };
|
|
|
|
/** Unwraps a fallible command result: `await call(commands.docOpen(path))`. */
|
|
export async function call<T>(result: Promise<Result<T>>): Promise<T> {
|
|
const r = await result;
|
|
if (r.status === "error") throw new IpcError(r.error);
|
|
return r.data;
|
|
}
|
|
|
|
export function isCancelled(e: unknown): boolean {
|
|
return e instanceof IpcError && e.kind === "cancelled";
|
|
}
|
|
|
|
/** Backend `unavailable` message when servers run but none has the feature: "jdtls doesn't support inline completion". */
|
|
const LACKS_FEATURE = /^\S.* doesn't support \S.*$/;
|
|
|
|
/**
|
|
* What to tell the user when a language-server feature is `unavailable`: the
|
|
* running server that lacks it, so a working server doesn't read as missing;
|
|
* `fallback` when no server is running.
|
|
*/
|
|
export function unavailableText(e: unknown, fallback = "No language server available"): string {
|
|
return e instanceof IpcError && e.kind === "unavailable" && LACKS_FEATURE.test(e.message) ? e.message : fallback;
|
|
}
|
|
|
|
/** True when `unavailable` means no server is running for the file, rather than a running server lacking the feature. */
|
|
export function isNoServer(e: unknown): boolean {
|
|
return e instanceof IpcError && e.kind === "unavailable" && !LACKS_FEATURE.test(e.message);
|
|
}
|
|
|
|
// Ids are shared by every window of the backend and outlive a reload there (a cancel that
|
|
// arrives before its request began is remembered), so each page starts at a random point.
|
|
let nextId = 1 + Math.floor(Math.random() * 0x7000_0000);
|
|
/** Allocates an id for a cancellable request (see `commands.cancelRequest`). */
|
|
export function nextRequestId(): RequestId {
|
|
return nextId++ as RequestId;
|
|
}
|
|
|
|
export const IDE_EVENT = "ide://event";
|
|
|
|
type EventOf<K extends IdeEvent["type"]> = Extract<IdeEvent, { type: K }>;
|
|
type Handler = (event: IdeEvent) => void;
|
|
|
|
const handlers = new Map<string, Set<Handler>>();
|
|
let unlisten: Promise<UnlistenFn> | null = null;
|
|
|
|
function dispatch(event: IdeEvent) {
|
|
const what = `IDE event ${event.type}`;
|
|
notifyEach(handlers.get(event.type) ?? [], (h) => h(event), what);
|
|
notifyEach(handlers.get("*") ?? [], (h) => h(event), what);
|
|
}
|
|
|
|
function ensureListening() {
|
|
if (unlisten || !isTauri()) return;
|
|
// Window-scoped: a global `listen` also receives events emitted to other windows.
|
|
unlisten = getCurrentWebviewWindow().listen<IdeEvent>(IDE_EVENT, ({ payload }) => dispatch(payload));
|
|
}
|
|
|
|
/** Resolves once backend events reach this window's handlers. */
|
|
export async function ideEventsReady(): Promise<void> {
|
|
ensureListening();
|
|
await unlisten;
|
|
}
|
|
|
|
/** Subscribes to one backend event type (or `"*"`); returns an unsubscribe function. */
|
|
export function onIdeEvent<K extends IdeEvent["type"]>(
|
|
type: K | "*",
|
|
handler: (event: EventOf<K>) => void,
|
|
): () => void {
|
|
ensureListening();
|
|
let set = handlers.get(type);
|
|
if (!set) handlers.set(type, (set = new Set()));
|
|
// A wrapper per subscription, so unsubscribing one of two identical handlers keeps the other.
|
|
const h: Handler = (event) => handler(event as EventOf<K>);
|
|
set.add(h);
|
|
return () => set.delete(h);
|
|
}
|
|
|
|
/** Test helper: dispatch an event as if it came from the backend. */
|
|
export function emitLocalIdeEvent(event: IdeEvent) {
|
|
dispatch(event);
|
|
}
|
|
|
|
export function isTauri(): boolean {
|
|
return typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
|
|
}
|