GeezerVison is showing github.com — refactor(mobile): composer pastes and picks are cancelled by their dr… · pingdotgg/t3code@6ffde5b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/components/ComposerEditor.tsx +19-18Lines changed: 19 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,14 +3,15 @@ import { ComposerContextId } from "@t3tools/contracts"; | 3 | 3 | import { useAtomValue } from "@effect/atom-react"; | 4 | 4 | import { AsyncResult } from "effect/reactivity"; | 5 | 5 | import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; | 6 | | -import { useEffect, useRef, useState } from "react"; | | 6 | +import { useEffect, useState } from "react"; | 7 | 7 | import type { EnvironmentId } from "@t3tools/contracts"; | 8 | 8 | import { encodeComposerContextFragment } from "@t3tools/shared/composerContextClipboard"; | 9 | 9 | import { collectComposerContextReferences } from "@t3tools/shared/composerContextReferences"; | 10 | 10 | import { ComposerEditor as NativeComposerEditor } from "../native/T3ComposerEditor"; | 11 | 11 | import type { ComposerEditorProps as NativeComposerEditorProps } from "../native/T3ComposerEditor"; | 12 | 12 | import { | 13 | 13 | appendComposerDraftAttachments, | | 14 | + composerContextImportsAtom, | 14 | 15 | createComposerDraftContextHistory, | 15 | 16 | getComposerDraftAfterSelection, | 16 | 17 | getComposerDraftSnapshot, | @@ -22,6 +23,8 @@ import { | 22 | 23 | useComposerDraft, | 23 | 24 | } from "../state/use-composer-drafts"; | 24 | 25 | import { importComposerContextClipboard } from "../lib/composerContextClipboard"; | | 26 | +import { startComposerDraftOperation } from "../state/composerDraftOperations"; | | 27 | +import { appAtomRegistry } from "../state/atom-registry"; | 25 | 28 | import { mobilePreferencesAtom } from "../state/preferences"; | 26 | 29 | import { useOpenLinkedThread } from "../features/threads/useOpenLinkedThread"; | 27 | 30 | import { ComposerContextSheet } from "./ComposerContextSheet"; | @@ -73,33 +76,32 @@ export function ComposerEditor({ | 73 | 76 | const [selected, setSelected] = useState<{ source: string; start: number; end: number } | null>( | 74 | 77 | null, | 75 | 78 | ); | 76 | | - const importRef = useRef<AbortController | null>(null); | 77 | | - const [importing, setImporting] = useState(false); | 78 | | - useEffect( | 79 | | - () => () => { | 80 | | - importRef.current?.abort(); | 81 | | - }, | 82 | | - // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Switching drafts aborts an import owned by the previous draft. | 83 | | - [draftKey], | 84 | | - ); | | 79 | + const contextImports = useAtomValue(composerContextImportsAtom); | | 80 | + const importing = draftKey ? contextImports[draftKey] === true : false; | 85 | 81 | const pasteContext = async ( | 86 | 82 | clipboard: Parameters<NonNullable<NativeComposerEditorProps["onPasteContext"]>>[0], | 87 | 83 | ) => { | 88 | | - if (!draftKey || importRef.current || props.readOnly || props.editable === false) return; | | 84 | + if ( | | 85 | + !draftKey || | | 86 | + appAtomRegistry.get(composerContextImportsAtom)[draftKey] || | | 87 | + props.readOnly || | | 88 | + props.editable === false | | 89 | + ) | | 90 | + return; | 89 | 91 | const insertion = { text: clipboard.value, ...clipboard.selection }; | 90 | | - const controller = new AbortController(); | 91 | | - importRef.current = controller; | 92 | | - setImporting(true); | | 92 | + const operation = startComposerDraftOperation(draftKey); | | 93 | + const { signal } = operation; | 93 | 94 | setComposerContextImporting(draftKey, true); | 94 | 95 | const settled = await settlePromise(async () => { | 95 | 96 | const retained = getComposerDraftAfterSelection(draftKey, insertion); | 96 | 97 | const result = await importComposerContextClipboard( | 97 | 98 | clipboard, | 98 | 99 | retained.attachments.length, | 99 | | - controller.signal, | | 100 | + signal, | 100 | 101 | retained.context?.records.length ?? 0, | 101 | 102 | environmentId, | 102 | 103 | ); | | 104 | + if (signal.aborted) return; | 103 | 105 | if (!result) { | 104 | 106 | insertComposerDraftText(draftKey, clipboard.text, insertion); | 105 | 107 | return; | @@ -126,10 +128,9 @@ export function ComposerEditor({ | 126 | 128 | [...warnings, "Those references are marked unavailable."].join("\n\n"), | 127 | 129 | ); | 128 | 130 | }); | | 131 | + operation.finish(); | 129 | 132 | setComposerContextImporting(draftKey, false); | 130 | | - importRef.current = null; | 131 | | - setImporting(false); | 132 | | - if (AsyncResult.isFailure(settled) && !controller.signal.aborted) { | | 133 | + if (AsyncResult.isFailure(settled) && !signal.aborted) { | 133 | 134 | const error = squashAtomCommandFailure(settled); | 134 | 135 | Dialog.alert( | 135 | 136 | "Could not paste context", | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,7 @@ import { | 36 | 36 |
| 37 | 37 | import { ComposerEditor, type ComposerEditorHandle } from "../../components/ComposerEditor"; | 38 | 38 | import { composerContextImportsAtom } from "../../state/use-composer-drafts"; | | 39 | +import { abortComposerDraftOperations } from "../../state/composerDraftOperations"; | 39 | 40 | import { composerContextSendBlockReason } from "../../lib/composerContext"; | 40 | 41 | import { ComposerInlineControl } from "../../components/ComposerToolbar"; | 41 | 42 |
| @@ -934,6 +935,9 @@ export function NewTaskDraftScreen(props: { | 934 | 935 | submittedThreadParams(String(message.environmentId), String(message.threadId)), | 935 | 936 | ), | 936 | 937 | ); | | 938 | + // The composer has moved to the submitted thread; a pick still converting | | 939 | + // would otherwise resurrect this draft. | | 940 | + abortComposerDraftOperations(draftKey); | 937 | 941 | const draftSnapshot = getComposerDraftSnapshot(draftKey); | 938 | 942 | if (editingPendingTask) { | 939 | 943 | flow.finishEditingPendingTask(); | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/composerContextClipboard.test.ts +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -169,6 +169,24 @@ describe("mobile context clipboard imports", () => { | 169 | 169 | expect(mocks.dispose).toHaveBeenCalledOnce(); | 170 | 170 | }); | 171 | 171 |
| | 172 | + it("releases its files if cancelled after the last copy finishes", async () => { | | 173 | + const controller = new AbortController(); | | 174 | + mocks.dispose.mockImplementationOnce(() => controller.abort()); | | 175 | + const imageOnly = { | | 176 | + text: "", | | 177 | + fragment: encodeComposerContextFragment({ | | 178 | + version: 1, | | 179 | + source: { environmentId: EnvironmentId.make("source") }, | | 180 | + records: [image], | | 181 | + })!, | | 182 | + html: "", | | 183 | + }; | | 184 | + await expect(importComposerContextClipboard(imageOnly, 0, controller.signal)).rejects.toThrow( | | 185 | + "cancelled", | | 186 | + ); | | 187 | + expect(mocks.remove).toHaveBeenCalledWith("file:///owned.png"); | | 188 | + }); | | 189 | + | 172 | 190 | it("can copy an image before its source upload completes", async () => { | 173 | 191 | mocks.local.mockReturnValue({ | 174 | 192 | id: "source-file", | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/lib/composerContextClipboard.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -106,6 +106,8 @@ export async function importComposerContextClipboard( | 106 | 106 | failures.push(record.name); | 107 | 107 | } | 108 | 108 | } | | 109 | + // The last copy can be cancelled after its own check; a cancelled import must not return files nobody will release. | | 110 | + checkAborted(signal); | 109 | 111 | return { | 110 | 112 | text: imported.text, | 111 | 113 | context: { version: 1 as const, records }, | |
Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { describe, expect, it } from "vite-plus/test"; | | 2 | + | | 3 | +import { | | 4 | + abortComposerDraftOperations, | | 5 | + startComposerDraftOperation, | | 6 | +} from "./composerDraftOperations"; | | 7 | + | | 8 | +describe("composer draft operations", () => { | | 9 | + it("aborts every running operation for the draft and leaves other drafts alone", () => { | | 10 | + const first = startComposerDraftOperation("thread-a"); | | 11 | + const second = startComposerDraftOperation("thread-a"); | | 12 | + const other = startComposerDraftOperation("thread-b"); | | 13 | + | | 14 | + abortComposerDraftOperations("thread-a"); | | 15 | + | | 16 | + expect(first.signal.aborted).toBe(true); | | 17 | + expect(second.signal.aborted).toBe(true); | | 18 | + expect(other.signal.aborted).toBe(false); | | 19 | + other.finish(); | | 20 | + }); | | 21 | + | | 22 | + it("does not abort an operation that finished before the draft switched", () => { | | 23 | + const finished = startComposerDraftOperation("thread-c"); | | 24 | + finished.finish(); | | 25 | + const running = startComposerDraftOperation("thread-c"); | | 26 | + | | 27 | + abortComposerDraftOperations("thread-c"); | | 28 | + | | 29 | + expect(finished.signal.aborted).toBe(false); | | 30 | + expect(running.signal.aborted).toBe(true); | | 31 | + }); | | 32 | + | | 33 | + it("keeps tracking an operation started after an abort when an older one finishes", () => { | | 34 | + const before = startComposerDraftOperation("thread-d"); | | 35 | + abortComposerDraftOperations("thread-d"); | | 36 | + const after = startComposerDraftOperation("thread-d"); | | 37 | + before.finish(); | | 38 | + abortComposerDraftOperations("thread-d"); | | 39 | + | | 40 | + expect(after.signal.aborted).toBe(true); | | 41 | + }); | | 42 | +}); |
Collapse file Copy file name to clipboard +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +const operationsByDraftKey = new Map<string, Set<AbortController>>(); | | 2 | + | | 3 | +/** | | 4 | + * Async work that writes into one draft (a context import, a picker) starts | | 5 | + * here. Whoever discards that draft or moves the composer off it aborts the | | 6 | + * work, so a late result never lands in a draft the user has left. | | 7 | + */ | | 8 | +export function startComposerDraftOperation(draftKey: string): { | | 9 | + readonly signal: AbortSignal; | | 10 | + readonly finish: () => void; | | 11 | +} { | | 12 | + const controller = new AbortController(); | | 13 | + const operations = operationsByDraftKey.get(draftKey) ?? new Set(); | | 14 | + operations.add(controller); | | 15 | + operationsByDraftKey.set(draftKey, operations); | | 16 | + return { | | 17 | + signal: controller.signal, | | 18 | + finish: () => { | | 19 | + operations.delete(controller); | | 20 | + if (operations.size === 0 && operationsByDraftKey.get(draftKey) === operations) { | | 21 | + operationsByDraftKey.delete(draftKey); | | 22 | + } | | 23 | + }, | | 24 | + }; | | 25 | +} | | 26 | + | | 27 | +export function abortComposerDraftOperations(draftKey: string): void { | | 28 | + const operations = operationsByDraftKey.get(draftKey); | | 29 | + if (!operations) return; | | 30 | + operationsByDraftKey.delete(draftKey); | | 31 | + for (const controller of operations) controller.abort(); | | 32 | +} | | 33 | + | | 34 | +export function abortComposerDraftOperationsWhere(matches: (draftKey: string) => boolean): void { | | 35 | + // Deleting the current entry while iterating a Map is safe. | | 36 | + for (const draftKey of operationsByDraftKey.keys()) { | | 37 | + if (matches(draftKey)) abortComposerDraftOperations(draftKey); | | 38 | + } | | 39 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/state/queued-run-edit.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ import { useAtomValue } from "@effect/atom-react"; | 10 | 10 |
| 11 | 11 | import { uploadedComposerContext } from "../lib/composerContext"; | 12 | 12 | import { appAtomRegistry } from "./atom-registry"; | | 13 | +import { abortComposerDraftOperations } from "./composerDraftOperations"; | 13 | 14 | import { queuedEditDraftKey } from "./queued-edit-draft-key"; | 14 | 15 | import { | 15 | 16 | clearComposerDraft, | @@ -59,6 +60,8 @@ export function beginQueuedRunEdit(threadKey: string, edit: QueuedRunEdit): void | 59 | 60 | if (previous !== null && previous.runId !== edit.runId) { | 60 | 61 | clearComposerDraft(queuedEditDraftKey(threadKey, previous.runId)); | 61 | 62 | } | | 63 | + // The composer moves off the thread draft, so a paste or pick still running for it is dropped. | | 64 | + abortComposerDraftOperations(threadKey); | 62 | 65 | const draftKey = queuedEditDraftKey(threadKey, edit.runId); | 63 | 66 | clearComposerDraft(draftKey); | 64 | 67 | setComposerDraftText(draftKey, edit.originalText); | |
|