Skip to content

Commit 8b7bbd1

Browse files
fix(mobile): address composer review findings
Track assumed native state when a parent-driven controlled document is sent, so a later parent update back to a previously acknowledged value (e.g. a draft restored after a failed send) applies as a fresh edit instead of being dropped as a native echo. Remove the selection-handler value delivery that could publish the empty editor's text over a restored draft; stale round-trips are already rejected by revision stamping. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 8446574 commit 8b7bbd1

4 files changed

Lines changed: 105 additions & 26 deletions

File tree

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useFontFamily } from "../lib/useFontFamily";
1919
import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
2020
import {
2121
acknowledgeComposerNativeEvent,
22+
assumeComposerControlledState,
2223
isComposerNativeEcho,
2324
pruneAcknowledgedComposerNativeEvents,
2425
resolveComposerControlledEventCount,
@@ -107,7 +108,6 @@ export function ComposerEditor({
107108
// first controlled payload must be a non-echo so a restored draft (or a
108109
// recycled native view) is applied rather than skipped.
109110
const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([]);
110-
const lastDeliveredValueRef = useRef(props.value);
111111
const confirmedTokensRef = useRef(collectComposerInlineTokens(props.value));
112112
const bodyText = useScaledTextRole("body");
113113
const textColor = useThemeColor("--color-foreground");
@@ -187,6 +187,18 @@ export function ComposerEditor({
187187
mostRecentEventCount,
188188
);
189189
}, [acknowledgesLatestNativeEvent, mostRecentEventCount]);
190+
const assumedValue = props.value;
191+
useEffect(() => {
192+
// A native event that arrived after this render was committed moves the
193+
// acknowledged revision forward; the editor rejects this payload, so the
194+
// snapshot history must not assume it applied.
195+
if (isNativeEcho || controlledEventCount !== mostRecentEventCountRef.current) return;
196+
nativeEventSnapshotsRef.current = assumeComposerControlledState(
197+
nativeEventSnapshotsRef.current,
198+
controlledEventCount,
199+
assumedValue,
200+
);
201+
}, [assumedValue, controlledEventCount, isNativeEcho, controlledDocumentJson]);
190202
const acceptNativeEvent = useCallback(
191203
(eventCount: number, value: string, nextSelection: ComposerEditorSelection) => {
192204
const acknowledgedEventCount = acknowledgeComposerNativeEvent(
@@ -251,7 +263,6 @@ export function ComposerEditor({
251263
event.nativeEvent.selection,
252264
);
253265
if (acknowledgedEventCount === false) return;
254-
lastDeliveredValueRef.current = event.nativeEvent.value;
255266
onChangeText(event.nativeEvent.value);
256267
onSelectionChange?.(event.nativeEvent.selection);
257268
setMostRecentEventCount(acknowledgedEventCount);
@@ -264,14 +275,6 @@ export function ComposerEditor({
264275
event.nativeEvent.selection,
265276
);
266277
if (acknowledgedEventCount === false) return;
267-
// A selection event that carries text the change handler has not
268-
// delivered yet (the platform emitted it mid-mutation) must also
269-
// deliver the value, or the parent's next render would round-trip
270-
// stale text stamped with this acknowledged revision.
271-
if (event.nativeEvent.value !== lastDeliveredValueRef.current) {
272-
lastDeliveredValueRef.current = event.nativeEvent.value;
273-
onChangeText(event.nativeEvent.value);
274-
}
275278
onSelectionChange?.(event.nativeEvent.selection);
276279
setMostRecentEventCount(acknowledgedEventCount);
277280
forceNativeEventRender((sequence) => sequence + 1);

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { useFontFamily } from "../lib/useFontFamily";
2121
import { useThemeColor } from "../lib/useThemeColor";
2222
import {
2323
acknowledgeComposerNativeEvent,
24+
assumeComposerControlledState,
2425
isComposerNativeEcho,
2526
pruneAcknowledgedComposerNativeEvents,
2627
resolveComposerControlledEventCount,
@@ -108,7 +109,6 @@ export function ComposerEditor({
108109
// first controlled payload must be a non-echo so a restored draft (or a
109110
// recycled native view) is applied rather than skipped.
110111
const nativeEventSnapshotsRef = useRef<ComposerNativeEventSnapshot[]>([]);
111-
const lastDeliveredValueRef = useRef(props.value);
112112
const [initialConfirmedTokens] = useState(() => collectComposerInlineTokens(props.value));
113113
const confirmedTokensRef = useRef(initialConfirmedTokens);
114114
const textColor = useThemeColor("--color-foreground");
@@ -188,6 +188,18 @@ export function ComposerEditor({
188188
mostRecentEventCount,
189189
);
190190
}, [acknowledgesLatestNativeEvent, mostRecentEventCount]);
191+
const assumedValue = props.value;
192+
useEffect(() => {
193+
// A native event that arrived after this render was committed moves the
194+
// acknowledged revision forward; the editor rejects this payload, so the
195+
// snapshot history must not assume it applied.
196+
if (isNativeEcho || controlledEventCount !== mostRecentEventCountRef.current) return;
197+
nativeEventSnapshotsRef.current = assumeComposerControlledState(
198+
nativeEventSnapshotsRef.current,
199+
controlledEventCount,
200+
assumedValue,
201+
);
202+
}, [assumedValue, controlledEventCount, isNativeEcho, controlledDocumentJson]);
191203
const acceptNativeEvent = useCallback(
192204
(eventCount: number, value: string, nextSelection: ComposerEditorSelection) => {
193205
const acknowledgedEventCount = acknowledgeComposerNativeEvent(
@@ -257,7 +269,6 @@ export function ComposerEditor({
257269
event.nativeEvent.selection,
258270
);
259271
if (acknowledgedEventCount === false) return;
260-
lastDeliveredValueRef.current = event.nativeEvent.value;
261272
onChangeText(event.nativeEvent.value);
262273
onSelectionChange?.(event.nativeEvent.selection);
263274
setMostRecentEventCount(acknowledgedEventCount);
@@ -270,14 +281,6 @@ export function ComposerEditor({
270281
event.nativeEvent.selection,
271282
);
272283
if (acknowledgedEventCount === false) return;
273-
// A selection event that carries text the change handler has not
274-
// delivered yet (the platform emitted it mid-mutation) must also
275-
// deliver the value, or the parent's next render would round-trip
276-
// stale text stamped with this acknowledged revision.
277-
if (event.nativeEvent.value !== lastDeliveredValueRef.current) {
278-
lastDeliveredValueRef.current = event.nativeEvent.value;
279-
onChangeText(event.nativeEvent.value);
280-
}
281284
onSelectionChange?.(event.nativeEvent.selection);
282285
setMostRecentEventCount(acknowledgedEventCount);
283286
forceNativeEventRender((sequence) => sequence + 1);

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "@effect/vitest";
22

33
import {
44
acknowledgeComposerNativeEvent,
5+
assumeComposerControlledState,
56
isComposerNativeEcho,
67
pruneAcknowledgedComposerNativeEvents,
78
resolveComposerControlledEventCount,
@@ -42,6 +43,12 @@ describe("isComposerNativeEcho", () => {
4243
it("matches value and revision when selection is uncontrolled", () => {
4344
expect(isComposerNativeEcho("native", null, 3, snapshots)).toBe(true);
4445
});
46+
47+
it("matches any controlled selection against an assumed state without one", () => {
48+
const assumed = [{ eventCount: 3, value: "native", selection: null }];
49+
expect(isComposerNativeEcho("native", { start: 0, end: 0 }, 3, assumed)).toBe(true);
50+
expect(isComposerNativeEcho("other", { start: 0, end: 0 }, 3, assumed)).toBe(false);
51+
});
4552
});
4653

4754
describe("resolveComposerControlledEventCount", () => {
@@ -128,3 +135,42 @@ describe("pruneAcknowledgedComposerNativeEvents", () => {
128135
expect(pruneAcknowledgedComposerNativeEvents(snapshots, 41)).toEqual([snapshots[1]]);
129136
});
130137
});
138+
139+
describe("assumeComposerControlledState", () => {
140+
it("replaces the acknowledged history with the applied controlled state", () => {
141+
const snapshots = [{ eventCount: 3, value: "typed", selection: { start: 5, end: 5 } }];
142+
143+
expect(assumeComposerControlledState(snapshots, 3, "")).toEqual([
144+
{ eventCount: 3, value: "", selection: null },
145+
]);
146+
});
147+
148+
it("keeps native events that raced past the controlled revision", () => {
149+
const snapshots = [
150+
{ eventCount: 3, value: "typed", selection: { start: 5, end: 5 } },
151+
{ eventCount: 4, value: "typed!", selection: { start: 6, end: 6 } },
152+
];
153+
154+
expect(assumeComposerControlledState(snapshots, 3, "")).toEqual([
155+
{ eventCount: 3, value: "", selection: null },
156+
snapshots[1],
157+
]);
158+
});
159+
160+
it("re-applies a parent value that round-trips back to an acknowledged state", () => {
161+
// Native acknowledged "typed", the parent then controlled the editor to ""
162+
// (a send clearing the draft) and back to "typed" (the send failed and the
163+
// draft was restored). The restore must be a fresh non-echo edit stamped at
164+
// the current revision, not an echo the editor would drop.
165+
const snapshots = assumeComposerControlledState(
166+
[{ eventCount: 3, value: "typed", selection: { start: 5, end: 5 } }],
167+
3,
168+
"",
169+
);
170+
171+
expect(isComposerNativeEcho("typed", { start: 5, end: 5 }, 3, snapshots)).toBe(false);
172+
expect(resolveComposerControlledEventCount("typed", { start: 5, end: 5 }, 3, snapshots)).toBe(
173+
3,
174+
);
175+
});
176+
});

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ export function resolveComposerControlledEventCount(
3131
if (snapshot?.value !== value) continue;
3232

3333
newestValueEventCount ??= snapshot.eventCount;
34-
if (
35-
selection === null ||
36-
(snapshot.selection?.start === selection.start && snapshot.selection.end === selection.end)
37-
) {
34+
if (selection === null || snapshotSelectionMatches(snapshot, selection)) {
3835
return snapshot.eventCount;
3936
}
4037
}
@@ -49,6 +46,18 @@ export function resolveComposerControlledEventCount(
4946
return mostRecentEventCount;
5047
}
5148

49+
// A snapshot without a selection describes a state the editor applied itself
50+
// (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.
53+
function snapshotSelectionMatches(
54+
snapshot: ComposerNativeEventSnapshot,
55+
selection: ComposerEditorSelection,
56+
): boolean {
57+
if (snapshot.selection === null) return true;
58+
return snapshot.selection.start === selection.start && snapshot.selection.end === selection.end;
59+
}
60+
5261
export function isComposerNativeEcho(
5362
value: string,
5463
selection: ComposerEditorSelection | null,
@@ -61,15 +70,33 @@ export function isComposerNativeEcho(
6170
snapshot !== undefined &&
6271
snapshot.eventCount === eventCount &&
6372
snapshot.value === value &&
64-
(selection === null ||
65-
(snapshot.selection?.start === selection.start && snapshot.selection.end === selection.end))
73+
(selection === null || snapshotSelectionMatches(snapshot, selection))
6674
) {
6775
return true;
6876
}
6977
}
7078
return false;
7179
}
7280

81+
/**
82+
* Records that a parent-driven controlled document was handed to the native
83+
* editor. From that point the acknowledged snapshot history describes a
84+
* superseded native state, so it is replaced with the assumed applied state;
85+
* a later parent update back to a previously acknowledged value must classify
86+
* as a fresh edit, not as a native echo the editor would drop. Native events
87+
* that raced past the controlled revision stay authoritative and are kept.
88+
*/
89+
export function assumeComposerControlledState(
90+
snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
91+
eventCount: number,
92+
value: string,
93+
): ComposerNativeEventSnapshot[] {
94+
return [
95+
{ eventCount, value, selection: null },
96+
...snapshots.filter((snapshot) => snapshot.eventCount > eventCount),
97+
];
98+
}
99+
73100
export function pruneAcknowledgedComposerNativeEvents(
74101
snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
75102
acknowledgedEventCount: number,

0 commit comments

Comments
 (0)