Stop configuration snapshot request loop
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Code2, FileCog, Save, X } from "lucide-react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
|
||||
import { platformApiClient } from "../api/client";
|
||||
import type { DeclaredFileReadSnapshotResponse, PluginConfigFieldResponse, PluginLogicalFileResponse, ServerFileWorkspaceResponse, ServerInstanceResponse } from "../api/types";
|
||||
@@ -27,21 +27,27 @@ export function ServerConfigEditor({ instance, operations, requester, onClose }:
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [message, setMessage] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const readInFlightRef = useRef("");
|
||||
const operationsRef = useRef(operations);
|
||||
operationsRef.current = operations;
|
||||
|
||||
const configFiles = useMemo(() => (workspace?.files ?? []).filter((file) => file.kind === "config" && file.editable !== false), [workspace]);
|
||||
const selectedFile = configFiles.find((file) => file.key === selectedKey) ?? configFiles[0];
|
||||
const fields = useMemo(() => (workspace?.configFields ?? []).filter((field) => field.fileKey === selectedFile?.key), [selectedFile?.key, workspace?.configFields]);
|
||||
|
||||
const readSelectedFile = useCallback(async (file: PluginLogicalFileResponse) => {
|
||||
const readKey = `${instance.id}:${file.key}`;
|
||||
if (readInFlightRef.current === readKey) return;
|
||||
readInFlightRef.current = readKey;
|
||||
setLoading(true);
|
||||
setError("");
|
||||
setMessage("正在通过 Run 读取配置文件…");
|
||||
try {
|
||||
let next = await platformApiClient.getServerFileReadSnapshot(instance.id, file.key);
|
||||
if (next.state !== "ready") {
|
||||
const operationId = operations.begin({ intent: "读取服务器配置", targetKind: "config", targetId: instance.id, requester });
|
||||
const operationId = operationsRef.current.begin({ intent: "读取服务器配置", targetKind: "config", targetId: instance.id, requester });
|
||||
const dispatch = await platformApiClient.readServerFile(instance.id, { key: file.key, idempotencyKey: configOperationKey("read", instance.id, file.key) });
|
||||
operations.succeed(operationId, `配置读取任务 ${dispatch.job.id} 已派发`, dispatch.job);
|
||||
operationsRef.current.succeed(operationId, `配置读取任务 ${dispatch.job.id} 已派发`, dispatch.job);
|
||||
for (let attempt = 0; attempt < 60 && next.state !== "ready"; attempt += 1) {
|
||||
await new Promise((resolve) => window.setTimeout(resolve, 500));
|
||||
next = await platformApiClient.getServerFileReadSnapshot(instance.id, file.key);
|
||||
@@ -59,8 +65,9 @@ export function ServerConfigEditor({ instance, operations, requester, onClose }:
|
||||
setMessage("");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
if (readInFlightRef.current === readKey) readInFlightRef.current = "";
|
||||
}
|
||||
}, [fields, instance.id, operations, requester]);
|
||||
}, [fields, instance.id, requester]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!workspace) {
|
||||
|
||||
Reference in New Issue
Block a user