Skip to content

Commit bd7b690

Browse files
fix(mobile): a send clear resets the composer's paste marker
After a reset the paste marker could stay ahead of the acknowledged count and make later sends skip the autocorrection flush. Cover the send flush and the undelivered-paste guard in the iOS editor test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6c45966 commit bd7b690

2 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,8 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
681681
}
682682
if isReset {
683683
resetGeneration = document.resetGeneration
684+
// JS drops a paste from before the reset by generation, so the flush must not wait for it.
685+
lastPasteEventCount = 0
684686
endComposition()
685687
}
686688
// iOS destroys this view under display: none and mounts a fresh one while JS keeps its

‎apps/mobile/src/components/ComposerEditor.ios.test.tsx‎

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { RegistryContext } from "@effect/atom-react";
2-
import { act, useLayoutEffect, useRef, type ReactNode } from "react";
2+
import {
3+
act,
4+
createRef,
5+
useImperativeHandle,
6+
useLayoutEffect,
7+
useRef,
8+
type ReactNode,
9+
type Ref,
10+
} from "react";
311
import { createRoot } from "react-dom/client";
412
import { expect, it, vi } from "vite-plus/test";
513

@@ -11,6 +19,7 @@ interface NativeEvent {
1119
}
1220

1321
interface NativeProps {
22+
readonly ref?: Ref<unknown>;
1423
readonly controlledDocumentJson: string;
1524
readonly onComposerChange: (event: { readonly nativeEvent: NativeEvent }) => void;
1625
}
@@ -20,17 +29,27 @@ const native = vi.hoisted(() => ({
2029
text: "",
2130
eventCount: 0,
2231
resetGeneration: 0,
32+
pasteEventCount: 0,
2333
mounts: 0,
2434
props: null as NativeProps | null,
2535
deliver: null as ((event: NativeEvent) => void) | null,
2636
}));
2737

28-
function NativeView(props: NativeProps) {
38+
function NativeView({ ref, ...props }: NativeProps) {
2939
const propsRef = useRef(props);
3040
useLayoutEffect(() => {
31-
Object.assign(native, { text: "", eventCount: 0, resetGeneration: 0 });
41+
Object.assign(native, { text: "", eventCount: 0, resetGeneration: 0, pasteEventCount: 0 });
3242
native.mounts += 1;
3343
}, []);
44+
useImperativeHandle(ref, () => ({
45+
readState: async () => ({
46+
value: native.text,
47+
selection: { start: native.text.length, end: native.text.length },
48+
eventCount: native.eventCount,
49+
resetGeneration: native.resetGeneration,
50+
pasteEventCount: native.pasteEventCount,
51+
}),
52+
}));
3453
useLayoutEffect(() => {
3554
propsRef.current = props;
3655
native.props = props;
@@ -55,6 +74,18 @@ function NativeView(props: NativeProps) {
5574
return null;
5675
}
5776

77+
// A native edit whose event has not reached JS yet.
78+
function pend(value: string): NativeEvent {
79+
native.eventCount += 1;
80+
native.text = value;
81+
return {
82+
value,
83+
selection: { start: value.length, end: value.length },
84+
eventCount: native.eventCount,
85+
resetGeneration: native.resetGeneration,
86+
};
87+
}
88+
5889
function emit(value: string, resetGeneration = native.resetGeneration) {
5990
native.eventCount += 1;
6091
native.text = value;
@@ -116,13 +147,21 @@ import {
116147
useComposerDraft,
117148
} from "../state/use-composer-drafts";
118149
import { ComposerEditor } from "./ComposerEditor";
150+
import type { ComposerEditorHandle } from "../native/T3ComposerEditor.types";
119151

120152
const DRAFT_KEY = "environment:thread";
121153

122-
function ThreadComposer({ draftKey = DRAFT_KEY }: { draftKey?: string }) {
154+
function ThreadComposer({
155+
draftKey = DRAFT_KEY,
156+
editorRef,
157+
}: {
158+
draftKey?: string;
159+
editorRef?: Ref<ComposerEditorHandle>;
160+
}) {
123161
const draft = useComposerDraft(draftKey);
124162
return (
125163
<ComposerEditor
164+
ref={editorRef}
126165
draftKey={draftKey}
127166
value={draft.text}
128167
onChangeText={(text) => setComposerDraftText(draftKey, text)}
@@ -190,3 +229,51 @@ it("keeps a pending event from the previous native editor out of the selected dr
190229
expect(getComposerDraftSnapshot(second).text).toBe("Draft B");
191230
expect(native.text).toBe("Draft B");
192231
});
232+
233+
async function renderFlushableComposer() {
234+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
235+
const editor = createRef<ComposerEditorHandle>();
236+
const root = createRoot(document.createElement("div"));
237+
await act(async () =>
238+
root.render(
239+
<RegistryContext.Provider value={appAtomRegistry}>
240+
<ThreadComposer editorRef={editor} />
241+
</RegistryContext.Provider>,
242+
),
243+
);
244+
return {
245+
editor,
246+
[Symbol.asyncDispose]: async () => act(() => root.unmount()),
247+
};
248+
}
249+
250+
it("sends the autocorrection the Send tap accepted, and its late event cannot refill the draft", async () => {
251+
await using screen = await renderFlushableComposer();
252+
await act(async () => emit("Hello wrold"));
253+
const lateCorrection = pend("Hello world");
254+
255+
await act(async () => screen.editor.current!.flushPendingInput());
256+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Hello world");
257+
258+
await act(async () => {
259+
clearComposerDraftContent(DRAFT_KEY);
260+
native.deliver!(lateCorrection);
261+
});
262+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("");
263+
});
264+
265+
it("does not flush past a paste whose event has not reached JS", async () => {
266+
await using screen = await renderFlushableComposer();
267+
await act(async () => emit("Before"));
268+
const paste = pend("Before pasted");
269+
native.pasteEventCount = paste.eventCount;
270+
const caretMove = pend("Before pasted");
271+
272+
await act(async () => screen.editor.current!.flushPendingInput());
273+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before");
274+
275+
await act(async () => native.deliver!(paste));
276+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before pasted");
277+
await act(async () => native.deliver!(caretMove));
278+
expect(getComposerDraftSnapshot(DRAFT_KEY).text).toBe("Before pasted");
279+
});

0 commit comments

Comments
 (0)