Repository navigation
fix(android): Events batching & reordering overhaul - #869
Open
szydlovsky wants to merge 4 commits into
Open
szydlovsky wants to merge 4 commits into
szydlovsky wants to merge 4 commits into
Conversation
szydlovsky
changed the base branch from
main
to
@kacperzolkiewski/fix-onlink-detected
November 19, 2026 17:00
szydlovsky
force-pushed
the
@szydlovsky/batch-reorder-android-events
branch
from
November 19, 2026 17:02
111c7d6 to
606c177
Compare
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Android batching can emit out-of-order state events and incorrect paste updates, while iOS marked-text handling can leave toolbar and detected-link state stale.
2 open issues
What changed in this PR
This PR batches Android editor events around completed edits to reduce duplicate notifications and stale link detection, bringing event delivery closer to iOS behavior.
Changes:
- Adds nested event batching across text edits, style validation, and programmatic operations.
- Updates link detection, manual-link newline handling, and iOS active-link reporting.
- Initializes the example editor’s selection to an empty range.
| File | Description |
|---|---|
ios/EnrichedTextInputView.mm |
Refreshes detected-link text and adjusts active-style update timing. |
apps/example/src/hooks/useEditorState.ts |
Initializes selection at position zero. |
android/src/main/java/com/swmansion/enriched/textinput/watchers/EnrichedTextWatcher.kt |
Defers text notifications until styling completes. |
android/src/main/java/com/swmansion/enriched/textinput/watchers/EnrichedSpanWatcher.kt |
Defers HTML generation during batches. |
android/src/main/java/com/swmansion/enriched/textinput/utils/EnrichedSpanState.kt |
Suppresses state notifications during batching. |
android/src/main/java/com/swmansion/enriched/textinput/utils/EnrichedSelection.kt |
Batches validation and includes link ranges in deduplication. |
android/src/main/java/com/swmansion/enriched/textinput/utils/EnrichedEditableFactory.kt |
Wraps editable replacements in event batches. |
android/src/main/java/com/swmansion/enriched/textinput/styles/ParametrizedStyles.kt |
Splits manual links around inserted newlines. |
android/src/main/java/com/swmansion/enriched/textinput/EnrichedTextInputView.kt |
Coordinates nested batches and event delivery order. |
Review effort: Balanced
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Motivation
Android sent JS events at the moment each change happened, often while an edit was still in progress. One keystroke could produce several
onChangeHtml,onChangeStateandonLinkDetectedevents with intermediate or stale values. The visible symptom was reported in #624: after pressing Enter inside a link, a staleonLinkDetectedfires first and the correct one follows a few ms later.The root cause is the order of Android's callbacks.
onSelectionChangedruns only after everyTextWatcheris done, so the styles were validated, and the events sent, against the caret from before the edit.iOS doesn't have this problem: it applies all changes first and emits events once at the end. This PR makes Android work the same way and removes the
isSelectionOutdated()workaround added in #624.How it works
EnrichedTextInputView.batchEvents { }runs its block immediately and holds back these events while it runs:onChangeTextonChangeHtmlonChangeStateonLinkDetectedonMentionDetectedOnce the block finishes, each event that is due is sent once, in this order: text → link/mention → state → html. Links and mentions are detected again at that point, so they use the final caret and spans.
It doesn't queue or delay anything, and it doesn't collect unrelated events. It only merges nested calls, where one callback runs another inside it. A depth counter makes sure only the outermost scope sends events. Two separate edits still emit separately.
onChangeSelectionand the mention-editing events (onStartMention,onChangeMention,onEndMention) aren't batched, same as on iOS.Where batching was added
EnrichedEditable.replace(new, inEnrichedEditableFactory.kt): every text edit goes through it. It coversafterTextChangedand theonSelectionChangedthat Android calls after it, so events use the final caret. This is the fix for fix(iOS, Android): split manual links when newline inserted in link #624's issue.EnrichedTextWatcher.afterTextChanged: applying styles adds and removes many spans.onChangeTextandonChangeHtmlare now sent once, after styling, instead of before it plus once per span change.EnrichedSelection.validateStyles: it calls about 20 state setters, which now produce oneonChangeStateinstead of several intermediate ones.runAsATransaction: multi-step programmatic edits (setValue, style toggles, mention insertion) emit once, when they're done.Event order is closer to iOS
onChangeSelectionnow arrives beforeonChangeTextandonChangeStatefor an edit, as on iOS.onChangeTextandonChangeHtmlnow describe the text with styles already applied.Benefits
EnrichedParser.toHtml()runs once per edit instead of once per span change. That's a noticeable saving on long documents and fast typing. The state payload is also built and compared once.isSelectionOutdated()workaround from fix(iOS, Android): split manual links when newline inserted in link #624 is no longer needed and has been removed.Follow-up fixes: actions that are made of several steps
Some actions run several separate steps, each with its own transaction, so each step sent its own events. Two cases were found:
Toggling a code block over a link (found in manual testing):
onLinkDetectedonly arrived after moving the caret. This was already happening on main.(0,0).Both come from
verifyStyle. It removes each conflicting style in its own transaction, and links are restored after the last validation.Pasting (found in review):
handleTextPastefirst callssetValue, which moves the caret to the end of the document. Only after that does it restore the caret and detect links in the pasted text.onChangeHtmlboth before and after its link was added.The fix wraps each of these entry points in a single
batchEvents, so all their steps emit once, with final values:verifyAndToggleStyletoggleCheckboxListItemaddLinkaddImagestartMentionaddMentionsetStylehandleTextPasteTest Plan
Tested manually on Android, with an event log open:
onLinkDetectedsetLink, editing and removing a link: one event each(0,0)@and query typing: mention events stay liveaddLinkwith a selection and with a collapsed caretaddImagein plain text, in a list and at the start of a linestartMention/addMentionfrom JSsetStylewith and without a selection-,1.)onChangeTextand at most oneonChangeHtmlper keystrokesetValue, clearing, and setting html from JSonChangeHtmlpassedonChangeHtmlwith the link includedScreenshots / Videos
--
Compatibility
Checklist