Repository navigation
perf(Android, Tabs): apply tab configuration changes at the end of the React transaction #4976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kkafar
merged 3 commits into
main
from
@kkafar/rfc-1823-android-tabs-batch-item-updates
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
.../src/main/java/com/swmansion/rnscreens/core/containers/common/ContainerUpdateScheduler.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.swmansion.rnscreens.core.containers.common | ||
|
|
||
| import android.os.Handler | ||
| import android.os.Looper | ||
|
|
||
| /** | ||
| * Decides when a container runs its update, so that any number of update requests results in | ||
| * a single run of [updateCallback]. | ||
| * | ||
| * - [requestUpdate] posts one run to the main thread's message queue. Further requests do nothing | ||
| * until that run happens. | ||
| * - Between [beginBatch] and [endBatch] (e.g. a React transaction) requests are only recorded, | ||
| * and [endBatch] runs the update once if any was requested. Batches don't nest. | ||
| * - [flush] runs the update right away, inside a batch too, and drops the posted run. | ||
| * | ||
| * The scheduler only decides when; [updateCallback] decides what, from the container's own | ||
| * invalidation state, and must do nothing when nothing is invalidated: [flush] runs it whether or | ||
| * not an update was requested. A request raised while [updateCallback] runs schedules another run. | ||
| * | ||
| * Main thread only. | ||
| */ | ||
| internal class ContainerUpdateScheduler( | ||
| private val updateCallback: () -> Unit, | ||
| ) { | ||
| private val handler = Handler(Looper.getMainLooper()) | ||
|
|
||
| // A run posted before a batch began waits for the batch to end. | ||
| private val postedUpdate = | ||
| Runnable { | ||
| if (!isInBatch) { | ||
| flush() | ||
| } | ||
| } | ||
|
|
||
| private var isUpdateRequested = false | ||
|
|
||
| private var isInBatch = false | ||
|
|
||
| fun requestUpdate() { | ||
| if (isUpdateRequested) { | ||
| return | ||
| } | ||
| isUpdateRequested = true | ||
| if (!isInBatch) { | ||
| handler.post(postedUpdate) | ||
| } | ||
| } | ||
|
|
||
| fun flush() { | ||
| cancel() | ||
| updateCallback() | ||
| } | ||
|
|
||
| /** | ||
| * Drops the requested update without running it. | ||
| */ | ||
| fun cancel() { | ||
| isUpdateRequested = false | ||
| handler.removeCallbacks(postedUpdate) | ||
| } | ||
|
|
||
| fun beginBatch() { | ||
| isInBatch = true | ||
| } | ||
|
|
||
| fun endBatch() { | ||
| isInBatch = false | ||
| if (isUpdateRequested) { | ||
| flush() | ||
| } | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the batches do not nest it would be beneficial to make this internal (for read) with private setter, so that a caller can detect whether the scheduler is already batching or not.