Open History to see the last commit
1export interface ComposerNativeEventSnapshot {
2 readonly eventCount: number;
3 readonly value: string;
4 readonly selection: ComposerEditorSelection | null;
5}
6
7interface ComposerEditorSelection {
8 readonly start: number;
9 readonly end: number;
10}
11
12export function acknowledgeComposerNativeEvent(
13 mostRecentEventCount: number,
14 incomingEventCount: number,
15): number | null {
16 if (!Number.isSafeInteger(incomingEventCount) || incomingEventCount < mostRecentEventCount) {
17 return null;
18 }
19 return incomingEventCount;
20}
21
22export function resolveComposerControlledEventCount(
23 value: string,
24 selection: ComposerEditorSelection | null,
25 mostRecentEventCount: number,
26 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
27): number {
28 let newestValueEventCount: number | null = null;
29 for (let index = snapshots.length - 1; index >= 0; index -= 1) {
30 const snapshot = snapshots[index];
31 if (snapshot?.value !== value) continue;
32
33 newestValueEventCount ??= snapshot.eventCount;
34 if (selection === null || snapshotSelectionMatches(snapshot, selection)) {
35 return snapshot.eventCount;
36 }
37 }
38
39 // A value emitted by native paired with a different selection is an
40 // intermediate React render. Keep it behind the native revision so it
41 // cannot move the caret while newer keystrokes are being processed.
42 if (newestValueEventCount !== null && mostRecentEventCount > 0) {
43 return Math.min(newestValueEventCount, mostRecentEventCount - 1);
44 }
45
46 return mostRecentEventCount;
47}
48
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). Echo detection must not reuse this wildcard: an echo payload
52// serializes `selection: null`, which would drop that caret move instead of
53// applying it.
54function snapshotSelectionMatches(
55 snapshot: ComposerNativeEventSnapshot,
56 selection: ComposerEditorSelection,
57): boolean {
58 if (snapshot.selection === null) return true;
59 return snapshot.selection.start === selection.start && snapshot.selection.end === selection.end;
60}
61
62export function isComposerNativeEcho(
63 value: string,
64 selection: ComposerEditorSelection | null,
65 eventCount: number,
66 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
67): boolean {
68 for (let index = snapshots.length - 1; index >= 0; index -= 1) {
69 const snapshot = snapshots[index];
70 if (
71 snapshot !== undefined &&
72 snapshot.eventCount === eventCount &&
73 snapshot.value === value &&
74 (selection === null ||
75 (snapshot.selection !== null &&
76 snapshot.selection.start === selection.start &&
77 snapshot.selection.end === selection.end))
78 ) {
79 return true;
80 }
81 }
82 return false;
83}
84
85/**
86 * Records that a parent-driven controlled document was handed to the native
87 * editor. From that point the acknowledged snapshot history describes a
88 * superseded native state, so it is replaced with the assumed applied state; a
89 * later parent update back to a previously acknowledged value must classify as
90 * a fresh edit, not as a native echo the editor would drop.
91 */
92export function assumeComposerControlledState(
93 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
94 eventCount: number,
95 value: string,
96): ComposerNativeEventSnapshot[] {
97 return [
98 { eventCount, value, selection: null },
99 ...snapshots.filter((snapshot) => snapshot.eventCount > eventCount),
100 ];
101}
102
103export function pruneAcknowledgedComposerNativeEvents(
104 snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,
105 acknowledgedEventCount: number,
106): ComposerNativeEventSnapshot[] {
107 // The newest acknowledged snapshot must survive pruning: it is what lets a
108 // later, unrelated re-render classify the settled composer state as a native
109 // echo instead of a parent-driven edit that would re-control the caret (and
110 // reset the keyboard's autocorrect context on iOS).
111 let latestAcknowledgedIndex = -1;
112 for (let index = snapshots.length - 1; index >= 0; index -= 1) {
113 const snapshot = snapshots[index];
114 if (snapshot !== undefined && snapshot.eventCount <= acknowledgedEventCount) {
115 latestAcknowledgedIndex = index;
116 break;
117 }
118 }
119 return snapshots.filter(
120 (snapshot, index) =>
121 index === latestAcknowledgedIndex || snapshot.eventCount > acknowledgedEventCount,
122 );
123}