Fix native composer lag with revision-gated updates #3574
Mergedjuliusmarmingemerged 6 commits into
main fromrefs/pull/3574/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- @@ -350,5 +357,6 @@350357 }351358 }352359353- func setValue(_ value: String) {354- self.value = value360+ func setControlledDocumentJson(_ documentJson: String) {361+ guard let document = decode(ComposerControlledDocumentPayload.self, from: documentJson),362+ document.mostRecentEventCount >= nativeEventCount else {
- apps/mobile/src/native/T3ComposerEditor.ios.tsx line 267Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -169,5 +192,14 @@169192 spellCheck={props.spellCheck ?? true}170193 style={style as StyleProp<ViewStyle>}171194 onComposerChange={(event) => {195+ const acknowledgedEventCount = acceptNativeEvent(event.nativeEvent.eventCount);196+ if (acknowledgedEventCount === false) return;172197 onChangeText(event.nativeEvent.value);173198 onSelectionChange?.(event.nativeEvent.selection);199+ setMostRecentEventCount(acknowledgedEventCount);200+ }}201+ onComposerSelectionChange={(event) => {202+ const acknowledgedEventCount = acceptNativeEvent(event.nativeEvent.eventCount);203+ if (acknowledgedEventCount === false) return;204+ onSelectionChange?.(event.nativeEvent.selection);205+ setMostRecentEventCount(acknowledgedEventCount);apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift line 378Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -371,8 +384,3 @@371384 }372385 }373386374- func setSelectionJson(_ selectionJson: String) {375- requestedSelection = decode(ComposerSelectionPayload.self, from: selectionJson)376- applyRequestedSelection()377- }378-apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift line 366Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -350,5 +357,8 @@350357 }351358 }352359353- func setValue(_ value: String) {354- self.value = value360+ func setControlledDocumentJson(_ documentJson: String) {361+ guard let document = decode(ComposerControlledDocumentPayload.self, from: documentJson),362+ document.mostRecentEventCount >= nativeEventCount else {363+ return364+ }apps/mobile/src/native/T3ComposerEditor.ios.tsx line 140OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -126,3 +130,11 @@126130 })),127131 );128132 }, [props.value, skillLabels]);133+ const controlledDocumentJson = useMemo(134+ () =>135+ JSON.stringify({136+ value: props.value,137+ selection: selection ?? null,138+ mostRecentEventCount,139+ }),140+ [mostRecentEventCount, props.value, selection],
- apps/mobile/src/native/composerEditorRevision.ts line 22Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,16 @@1+export interface ComposerNativeEventSnapshot {2+ readonly eventCount: number;3+ readonly value: string;4+}5+6+export function acknowledgeComposerNativeEvent(7+ mostRecentEventCount: number,8+ incomingEventCount: number,9+): number | null {10+ if (!Number.isSafeInteger(incomingEventCount) || incomingEventCount < mostRecentEventCount) {11+ return null;12+ }13+ return incomingEventCount;14+}15+16+export function resolveComposerControlledEventCount(
- apps/mobile/src/native/T3ComposerEditor.ios.tsx line 166OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -127,2 +146,21 @@127146 );128147 }, [props.value, skillLabels]);148+ const controlledEventCount = resolveComposerControlledEventCount(149+ props.value,150+ Math.max(mostRecentEventCount, mostRecentEventCountRef.current),151+ nativeEventSnapshotsRef.current,152+ );153+ const controlledDocumentJson = JSON.stringify({154+ value: props.value,155+ selection: selection ?? null,156+ tokensJson,157+ mostRecentEventCount: controlledEventCount,158+ });159+ useEffect(() => {160+ nativeEventSnapshotsRef.current = [161+ { eventCount: controlledEventCount, value: props.value },162+ ...nativeEventSnapshotsRef.current.filter(163+ (snapshot) => snapshot.eventCount > controlledEventCount,164+ ),165+ ];166+ }, [controlledEventCount, props.value]);apps/mobile/src/native/composerEditorRevision.ts line 39Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +5,21 @@5+6+export function acknowledgeComposerNativeEvent(7+ mostRecentEventCount: number,8+ incomingEventCount: number,9+): number | null {10+ if (!Number.isSafeInteger(incomingEventCount) || incomingEventCount < mostRecentEventCount) {11+ return null;12+ }13+ return incomingEventCount;14+}15+16+export function resolveComposerControlledEventCount(17+ value: string,18+ mostRecentEventCount: number,19+ snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,20+): number {21+ for (let index = snapshots.length - 1; index >= 0; index -= 1) {22+ const snapshot = snapshots[index];23+ if (snapshot?.value === value) {24+ return snapshot.eventCount;25+ }
- @@ -350,15 +359,11 @@350359 }351360 }352361353- func setValue(_ value: String) {354- self.value = value355- applyControlledDocument(force: tokensNeedRebuild)356- if tokensMatchCurrentValue() {357- tokensNeedRebuild = false362+ func setControlledDocumentJson(_ documentJson: String) {363+ guard let document = decode(ComposerControlledDocumentPayload.self, from: documentJson),364+ document.mostRecentEventCount >= nativeEventCount else {365+ return358366 }359- }360-361- func setTokensJson(_ tokensJson: String) {362- guard self.tokensJson != tokensJson else {367+ if document.isNativeValue && textView.serializedText() != document.value {363368 return364369 }apps/mobile/src/native/composerEditorRevision.ts lines 57 to 57OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +37,21 @@37+ ) {38+ return snapshot.eventCount;39+ }40+ }41+42+ // A value emitted by native paired with a different selection is an43+ // intermediate React render. Keep it behind the native revision so it44+ // cannot move the caret while newer keystrokes are being processed.45+ if (newestValueEventCount !== null && mostRecentEventCount > 0) {46+ return Math.min(newestValueEventCount, mostRecentEventCount - 1);47+ }48+49+ return mostRecentEventCount;50+}51+52+export function resolveComposerControlledSelection(53+ value: string,54+ selection: ComposerEditorSelection | null,55+ snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,56+): ComposerEditorSelection | null {57+ if (selection === null || snapshots.some((snapshot) => snapshot.value === value)) {
- apps/mobile/src/native/composerEditorRevision.ts line 78Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +48,21 @@48+49+ return mostRecentEventCount;50+}51+52+export function resolveComposerControlledSelection(53+ value: string,54+ selection: ComposerEditorSelection | null,55+ snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,56+): ComposerEditorSelection | null {57+ if (selection === null || snapshots.some((snapshot) => snapshot.value === value)) {58+ return null;59+ }60+ return selection;61+}62+63+export function isComposerNativeValue(64+ value: string,65+ snapshots: ReadonlyArray<ComposerNativeEventSnapshot>,66+): boolean {67+ return snapshots.some((snapshot) => snapshot.value === value);68+}apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift line 491OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -484,3 +488,4 @@484488 private func applyControlledDocument(force: Bool = false) {485489 let currentSource = textView.serializedText()486490 guard force || currentSource != value || !documentMatchesExpectedTokens() else {491+ requestedSelection = nil
- apps/mobile/src/native/T3ComposerEditor.ios.tsx lines 178 to 180Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -128,0 +161,21 @@161+ includesNativeEvent &&162+ isComposerNativeEcho(163+ props.value,164+ selection ?? null,165+ controlledEventCount,166+ nativeEventSnapshotsRef.current,167+ );168+ const controlledDocumentJson = JSON.stringify({169+ value: props.value,170+ selection: isNativeEcho ? null : (selection ?? null),171+ tokensJson,172+ mostRecentEventCount: controlledEventCount,173+ isNativeEcho,174+ });175+ useEffect(() => {176+ previousRenderedEventSequenceRef.current = nativeEventSequence;177+ const snapshots = nativeEventSnapshotsRef.current;178+ if (snapshots.length > 50) {179+ nativeEventSnapshotsRef.current = snapshots.slice(-50);180+ }181+ }, [nativeEventSequence]);
- apps/mobile/src/native/T3ComposerEditor.ios.tsx line 160Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -126,3 +148,12 @@126148 })),127149 );128150 }, [props.value, skillLabels]);151+ const includesNativeEvent = nativeEventSequence !== previousRenderedEventSequenceRef.current;152+ const controlledEventCount = includesNativeEvent153+ ? resolveComposerControlledEventCount(154+ props.value,155+ selection ?? null,156+ mostRecentEventCount,157+ nativeEventSnapshotsRef.current,158+ )159+ : mostRecentEventCount;
- apps/mobile/src/native/T3ComposerEditor.ios.tsx lines 198 to 203Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -128,0 +173,21 @@173+ isNativeEcho,174+ });175+ useEffect(() => {176+ previousRenderedEventSequenceRef.current = nativeEventSequence;177+ }, [nativeEventSequence]);178+ const acceptNativeEvent = useCallback(179+ (eventCount: number, value: string, nextSelection: ComposerEditorSelection) => {180+ const acknowledgedEventCount = acknowledgeComposerNativeEvent(181+ mostRecentEventCountRef.current,182+ eventCount,183+ );184+ if (acknowledgedEventCount === null) {185+ return false;186+ }187+ mostRecentEventCountRef.current = acknowledgedEventCount;188+ nativeEventSnapshotsRef.current.push({189+ eventCount: acknowledgedEventCount,190+ value,191+ selection: nextSelection,192+ });193+ return acknowledgedEventCount;
- apps/mobile/src/native/T3ComposerEditor.ios.tsx line 177Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -172,6 +242,20 @@242+ );243+ if (acknowledgedEventCount === false) return;172244 onChangeText(event.nativeEvent.value);173245 onSelectionChange?.(event.nativeEvent.selection);246+ setMostRecentEventCount(acknowledgedEventCount);247+ setNativeEventSequence((sequence) => sequence + 1);248+ }}249+ onComposerSelectionChange={(event) => {250+ const acknowledgedEventCount = acceptNativeEvent(251+ event.nativeEvent.eventCount,252+ event.nativeEvent.value,253+ event.nativeEvent.selection,254+ );255+ if (acknowledgedEventCount === false) return;256+ onSelectionChange?.(event.nativeEvent.selection);257+ setMostRecentEventCount(acknowledgedEventCount);258+ setNativeEventSequence((sequence) => sequence + 1);174259 }}175- onComposerSelectionChange={(event) => onSelectionChange?.(event.nativeEvent.selection)}176260 onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}177261 onComposerFocus={onFocus}
juliusmarminge merged this6245c54
juliusmarminge closed this
Sign in to join this conversation.



Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID:
70fe5f70-319f-40b5-9485-afe786909b93You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.