Ignore superseded image preview requests
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
948bf61626
commit
a6f1072834
4 files changed
+98
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
|
||||
import type { PreviewSessionSnapshot, ScopedThreadRef } from "@t3tools/contracts";
|
||||
import { AsyncResult } from "effect/unstable/reactivity";
|
||||
import { beforeEach, expect, it } from "vite-plus/test";
|
||||
|
||||
import { readThreadPreviewState, resetPreviewStateForTests } from "~/previewStateStore";
|
||||
import { selectThreadRightPanelState, useRightPanelStore } from "~/rightPanelStore";
|
||||
|
||||
import { type OpenPreviewMutation, openUrlInPreview } from "./openFileInPreview";
|
||||
|
||||
const threadRef = {
|
||||
environmentId: "local" as ScopedThreadRef["environmentId"],
|
||||
threadId: "thread-1" as ScopedThreadRef["threadId"],
|
||||
};
|
||||
|
||||
const snapshot = (tabId: string, url: string): PreviewSessionSnapshot => ({
|
||||
threadId: threadRef.threadId,
|
||||
tabId,
|
||||
navStatus: { _tag: "Success", url, title: "" },
|
||||
canGoBack: false,
|
||||
canGoForward: false,
|
||||
updatedAt: "2026-06-21T00:00:00.000Z",
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
resetPreviewStateForTests();
|
||||
useRightPanelStore.setState({ byThreadKey: {} });
|
||||
});
|
||||
|
||||
it("does not apply an older preview response after a newer request", async () => {
|
||||
const firstController = new AbortController();
|
||||
const firstSnapshot = snapshot("tab-1", "https://assets.test/first.png");
|
||||
const secondSnapshot = snapshot("tab-2", "https://assets.test/second.png");
|
||||
let resolveFirst!: (result: AtomCommandResult<PreviewSessionSnapshot, never>) => void;
|
||||
const openPreview: OpenPreviewMutation<never> = ({ input }) =>
|
||||
input.url === "https://assets.test/first.png"
|
||||
? new Promise<AtomCommandResult<PreviewSessionSnapshot, never>>((resolve) => {
|
||||
resolveFirst = resolve;
|
||||
})
|
||||
: Promise.resolve(AsyncResult.success(secondSnapshot));
|
||||
|
||||
const firstRequest = openUrlInPreview({
|
||||
threadRef,
|
||||
url: "https://assets.test/first.png",
|
||||
openPreview,
|
||||
signal: firstController.signal,
|
||||
});
|
||||
firstController.abort();
|
||||
|
||||
await openUrlInPreview({
|
||||
threadRef,
|
||||
url: "https://assets.test/second.png",
|
||||
openPreview,
|
||||
});
|
||||
resolveFirst(AsyncResult.success(firstSnapshot));
|
||||
await firstRequest;
|
||||
|
||||
expect(readThreadPreviewState(threadRef).snapshot).toEqual(secondSnapshot);
|
||||
expect(
|
||||
selectThreadRightPanelState(useRightPanelStore.getState().byThreadKey, threadRef).surfaces,
|
||||
).toEqual([{ id: "browser:tab-2", kind: "preview", resourceId: "tab-2" }]);
|
||||
});
|
||||
@@ -46,12 +46,14 @@ export async function openUrlInPreview<E>(input: {
|
||||
readonly threadRef: ScopedThreadRef;
|
||||
readonly url: string;
|
||||
readonly openPreview: OpenPreviewMutation<E>;
|
||||
readonly signal?: AbortSignal;
|
||||
}): Promise<AtomCommandResult<void, E>> {
|
||||
const result = await input.openPreview({
|
||||
environmentId: input.threadRef.environmentId,
|
||||
input: { threadId: input.threadRef.threadId, url: input.url },
|
||||
});
|
||||
return mapAtomCommandResult(result, (snapshot) => {
|
||||
if (input.signal?.aborted) return;
|
||||
applyPreviewServerSnapshot(input.threadRef, snapshot);
|
||||
rememberPreviewUrl(input.threadRef, input.url);
|
||||
useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId);
|
||||
@@ -67,6 +69,7 @@ export async function openFileInPreview<AssetError, PreviewError>(input: {
|
||||
readonly input: { readonly resource: AssetResource };
|
||||
}) => Promise<AtomCommandResult<AssetCreateUrlResult, AssetError>>;
|
||||
readonly openPreview: OpenPreviewMutation<PreviewError>;
|
||||
readonly signal?: AbortSignal;
|
||||
}): Promise<AtomCommandResult<void, AssetError | PreviewError | BrowserPreviewUnavailableError>> {
|
||||
if (!isPreviewSupportedInRuntime()) {
|
||||
return AsyncResult.failure(
|
||||
@@ -87,6 +90,9 @@ export async function openFileInPreview<AssetError, PreviewError>(input: {
|
||||
},
|
||||
},
|
||||
});
|
||||
if (input.signal?.aborted) {
|
||||
return AsyncResult.success(undefined);
|
||||
}
|
||||
if (assetResult._tag === "Failure") {
|
||||
return AsyncResult.failure(assetResult.cause);
|
||||
}
|
||||
@@ -100,5 +106,6 @@ export async function openFileInPreview<AssetError, PreviewError>(input: {
|
||||
threadRef: input.threadRef,
|
||||
url: assetUrl,
|
||||
openPreview: input.openPreview,
|
||||
...(input.signal ? { signal: input.signal } : {}),
|
||||
});
|
||||
}
|
||||
@@ -1023,6 +1023,7 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
const createAssetUrl = useAtomQueryRunner(assetEnvironment.createUrl, {
|
||||
reportFailure: false,
|
||||
});
|
||||
const filePreviewAbortControllerRef = useRef<AbortController | null>(null);
|
||||
const closePreview = useAtomCommand(previewEnvironment.close, "preview close");
|
||||
const { environments } = useEnvironments();
|
||||
const primaryEnvironment = usePrimaryEnvironment();
|
||||
@@ -2751,9 +2752,16 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
if (!activeThreadRef || !activeProject) return;
|
||||
useRightPanelStore.getState().open(activeThreadRef, "files");
|
||||
}, [activeProject, activeThreadRef]);
|
||||
useEffect(
|
||||
() => () => {
|
||||
filePreviewAbortControllerRef.current?.abort();
|
||||
},
|
||||
[activeThreadKey],
|
||||
);
|
||||
const openFileSurface = useCallback(
|
||||
(relativePath: string) => {
|
||||
if (!activeThreadRef || !activeProject) return;
|
||||
filePreviewAbortControllerRef.current?.abort();
|
||||
const openFallback = () => {
|
||||
useRightPanelStore.getState().openFile(activeThreadRef, relativePath);
|
||||
};
|
||||
@@ -2767,6 +2775,8 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
return;
|
||||
}
|
||||
|
||||
const abortController = new AbortController();
|
||||
filePreviewAbortControllerRef.current = abortController;
|
||||
const absolutePath = resolvePathLinkTarget(relativePath, activeWorkspaceRoot);
|
||||
void (async () => {
|
||||
const result = await openFileInPreview({
|
||||
@@ -2775,7 +2785,11 @@ function ChatViewContent(props: ChatViewProps) {
|
||||
httpBaseUrl: activeEnvironmentHttpBaseUrl,
|
||||
createAssetUrl,
|
||||
openPreview,
|
||||
signal: abortController.signal,
|
||||
});
|
||||
if (abortController.signal.aborted) {
|
||||
return;
|
||||
}
|
||||
if (result._tag === "Success") {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -227,6 +227,7 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
reportFailure: false,
|
||||
});
|
||||
const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: false });
|
||||
const filePreviewAbortControllerRef = useRef<AbortController | null>(null);
|
||||
const serverConfig = useAtomValue(
|
||||
serverEnvironment.configValueAtom(activeThread?.environmentId ?? null),
|
||||
);
|
||||
@@ -457,8 +458,16 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
codeViewRef.current?.scrollTo({ type: "item", id: file.fileKey, align: "start" });
|
||||
}, [codeViewFiles, selectedFilePath, selectedFileRevealRequestId]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
filePreviewAbortControllerRef.current?.abort();
|
||||
},
|
||||
[routeThreadRef?.environmentId, routeThreadRef?.threadId],
|
||||
);
|
||||
|
||||
const openDiffFile = useCallback(
|
||||
(filePath: string) => {
|
||||
filePreviewAbortControllerRef.current?.abort();
|
||||
const openFallback = () => {
|
||||
openDiffFilePrimaryAction({
|
||||
threadRef: routeThreadRef,
|
||||
@@ -495,6 +504,8 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
return;
|
||||
}
|
||||
|
||||
const abortController = new AbortController();
|
||||
filePreviewAbortControllerRef.current = abortController;
|
||||
void (async () => {
|
||||
const result = await openFileInPreview({
|
||||
threadRef: routeThreadRef,
|
||||
@@ -502,7 +513,11 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
|
||||
httpBaseUrl: environmentHttpBaseUrl,
|
||||
createAssetUrl,
|
||||
openPreview,
|
||||
signal: abortController.signal,
|
||||
});
|
||||
if (abortController.signal.aborted) {
|
||||
return;
|
||||
}
|
||||
if (result._tag === "Success") {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user