Skip to content

Commit 2e0b505

Browse files
fix(mobile): apply parent caret moves and mid-mutation text on assumed composer states
An assumed controlled snapshot (selection: null) matched any controlled selection in isComposerNativeEcho, so a parent-only caret move on the same value was classified as an echo, serialized with selection: null, and dropped. Echo detection now requires an exact selection match; the wildcard stays in resolveComposerControlledEventCount so the caret move is still stamped at the assumed revision and accepted by the editor. Selection events can also race a text mutation on Android (the caret moves before afterTextChanged), so their payload can carry post-edit text at a newly acknowledged revision. Forward that value through onChangeText so the parent draft cannot be stamped stale at the acknowledged revision and re-applied over newer native text. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 8b7bbd1 commit 2e0b505

4 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎apps/mobile/src/native/T3ComposerEditor.ios.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ export function ComposerEditor({
275275
event.nativeEvent.selection,
276276
);
277277
if (acknowledgedEventCount === false) return;
278+
// A selection change that raced a text mutation can carry post-edit
279+
// text. It must reach the parent alongside the acknowledged revision,
280+
// or the next render stamps the stale draft at that revision and can
281+
// re-apply it over the newer native text.
282+
if (event.nativeEvent.value !== props.value) {
283+
onChangeText(event.nativeEvent.value);
284+
}
278285
onSelectionChange?.(event.nativeEvent.selection);
279286
setMostRecentEventCount(acknowledgedEventCount);
280287
forceNativeEventRender((sequence) => sequence + 1);

‎apps/mobile/src/native/T3ComposerEditor.native.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ export function ComposerEditor({
281281
event.nativeEvent.selection,
282282
);
283283
if (acknowledgedEventCount === false) return;
284+
// Android emits the selection change mid-mutation, before the change
285+
// event, so the payload can carry post-edit text. It must reach the
286+
// parent alongside the acknowledged revision, or the next render
287+
// stamps the stale draft at that revision and can re-apply it over
288+
// the newer native text.
289+
if (event.nativeEvent.value !== props.value) {
290+
onChangeText(event.nativeEvent.value);
291+
}
284292
onSelectionChange?.(event.nativeEvent.selection);
285293
setMostRecentEventCount(acknowledgedEventCount);
286294
forceNativeEventRender((sequence) => sequence + 1);

‎apps/mobile/src/native/composerEditorRevision.test.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ describe("isComposerNativeEcho", () => {
4444
expect(isComposerNativeEcho("native", null, 3, snapshots)).toBe(true);
4545
});
4646

47-
it("matches any controlled selection against an assumed state without one", () => {
47+
it("does not claim a controlled selection against an assumed state without one", () => {
48+
// An echo payload serializes `selection: null`; classifying a controlled
49+
// selection as an echo of an assumed state would drop a parent caret move.
4850
const assumed = [{ eventCount: 3, value: "native", selection: null }];
49-
expect(isComposerNativeEcho("native", { start: 0, end: 0 }, 3, assumed)).toBe(true);
51+
expect(isComposerNativeEcho("native", { start: 0, end: 0 }, 3, assumed)).toBe(false);
5052
expect(isComposerNativeEcho("other", { start: 0, end: 0 }, 3, assumed)).toBe(false);
5153
});
54+
55+
it("matches an assumed state when selection is uncontrolled", () => {
56+
const assumed = [{ eventCount: 3, value: "native", selection: null }];
57+
expect(isComposerNativeEcho("native", null, 3, assumed)).toBe(true);
58+
});
5259
});
5360

5461
describe("resolveComposerControlledEventCount", () => {
@@ -157,6 +164,17 @@ describe("assumeComposerControlledState", () => {
157164
]);
158165
});
159166

167+
it("applies a parent caret move on the assumed value at the assumed revision", () => {
168+
// Same value, new caret: not an echo (so the selection is serialized) but
169+
// still stamped at the assumed revision so the editor accepts it.
170+
const snapshots = assumeComposerControlledState([], 3, "typed");
171+
172+
expect(isComposerNativeEcho("typed", { start: 2, end: 2 }, 3, snapshots)).toBe(false);
173+
expect(resolveComposerControlledEventCount("typed", { start: 2, end: 2 }, 3, snapshots)).toBe(
174+
3,
175+
);
176+
});
177+
160178
it("re-applies a parent value that round-trips back to an acknowledged state", () => {
161179
// Native acknowledged "typed", the parent then controlled the editor to ""
162180
// (a send clearing the draft) and back to "typed" (the send failed and the

‎apps/mobile/src/native/composerEditorRevision.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ export function resolveComposerControlledEventCount(
4848

4949
// A snapshot without a selection describes a state the editor applied itself
5050
// (an assumed controlled document, where the native side may have bounded the
51-
// caret). It matches any controlled selection: such matches only ever produce
52-
// echoes, and echo payloads never control the caret.
51+
// caret). Revision stamping treats it as matching any controlled selection so
52+
// a parent caret move on the assumed value stays at the assumed revision and
53+
// passes the editor's staleness guard. Echo detection must not reuse this
54+
// wildcard: an echo payload serializes `selection: null`, which would drop
55+
// that caret move instead of applying it.
5356
function snapshotSelectionMatches(
5457
snapshot: ComposerNativeEventSnapshot,
5558
selection: ComposerEditorSelection,
@@ -70,7 +73,10 @@ export function isComposerNativeEcho(
7073
snapshot !== undefined &&
7174
snapshot.eventCount === eventCount &&
7275
snapshot.value === value &&
73-
(selection === null || snapshotSelectionMatches(snapshot, selection))
76+
(selection === null ||
77+
(snapshot.selection !== null &&
78+
snapshot.selection.start === selection.start &&
79+
snapshot.selection.end === selection.end))
7480
) {
7581
return true;
7682
}

0 commit comments

Comments
 (0)