-
-
Notifications
You must be signed in to change notification settings - Fork 27
Implement withLatestFrom for multiple flows (3-10 flows) with optimized Array-based implementation #370
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
Open
Copilot
wants to merge
4
commits into
master
Choose a base branch
from
copilot/fix-e8dd3d61-6634-4252-85ed-cf1f70241acc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement withLatestFrom for multiple flows (3-10 flows) with optimized Array-based implementation #370
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8e08ca9
Initial plan
Copilot cc7c17d
Implement withLatestFrom for 3-6 flows with comprehensive tests
Copilot d9818e8
Complete withLatestFrom implementation for 3-10 flows with comprehens…
Copilot b468010
Refactor withLatestFrom overloads to eliminate duplication using Arra…
Copilot 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
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 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -72,3 +72,311 @@ public fun <A, B, R> Flow<A>.withLatestFrom( | |||||||||||||||||||||||||||||||||
| @Suppress("NOTHING_TO_INLINE") | ||||||||||||||||||||||||||||||||||
| public inline fun <A, B> Flow<A>.withLatestFrom(other: Flow<B>): Flow<Pair<A, B>> = | ||||||||||||||||||||||||||||||||||
| withLatestFrom(other, ::Pair) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Merges multiple [Flow]s into one [Flow] by combining each value from self with the latest values from the other [Flow]s, if any. | ||||||||||||||||||||||||||||||||||
| * Values emitted by self before all other [Flow]s have emitted any values will be omitted. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @param others Array of other [Flow]s | ||||||||||||||||||||||||||||||||||
| * @param transform A transform function to apply to each value from self combined with the latest values from the other [Flow]s, if any. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public fun <A, R> Flow<A>.withLatestFrom( | ||||||||||||||||||||||||||||||||||
| others: Array<out Flow<*>>, | ||||||||||||||||||||||||||||||||||
| transform: suspend (A, Array<Any?>) -> R, | ||||||||||||||||||||||||||||||||||
| ): Flow<R> { | ||||||||||||||||||||||||||||||||||
| return flow { | ||||||||||||||||||||||||||||||||||
| val refs = Array<AtomicRef<Any?>>(others.size) { AtomicRef(null) } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| coroutineScope { | ||||||||||||||||||||||||||||||||||
| val jobs = others.mapIndexed { index, flow -> | ||||||||||||||||||||||||||||||||||
| launch(start = CoroutineStart.UNDISPATCHED) { | ||||||||||||||||||||||||||||||||||
| flow.collect { refs[index].value = it ?: INTERNAL_NULL_VALUE } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| collect { value -> | ||||||||||||||||||||||||||||||||||
| val values = Array<Any?>(refs.size) { index -> | ||||||||||||||||||||||||||||||||||
| refs[index].value ?: return@collect | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| val unboxedValues = Array<Any?>(values.size) { index -> | ||||||||||||||||||||||||||||||||||
| INTERNAL_NULL_VALUE.unbox(values[index]) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| emit(transform(value, unboxedValues)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| jobs.forEach { it.cancelAndJoin() } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||
| refs.forEach { it.value = null } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Merges three [Flow]s into one [Flow] by combining each value from self with the latest values from the other [Flow]s, if any. | ||||||||||||||||||||||||||||||||||
| * Values emitted by self before all other [Flow]s have emitted any values will be omitted. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * @param other2 Second [Flow] | ||||||||||||||||||||||||||||||||||
| * @param other3 Third [Flow] | ||||||||||||||||||||||||||||||||||
| * @param transform A transform function to apply to each value from self combined with the latest values from the other [Flow]s, if any. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public fun <A, B, C, R> Flow<A>.withLatestFrom( | ||||||||||||||||||||||||||||||||||
| other2: Flow<B>, | ||||||||||||||||||||||||||||||||||
| other3: Flow<C>, | ||||||||||||||||||||||||||||||||||
| transform: suspend (A, B, C) -> R, | ||||||||||||||||||||||||||||||||||
| ): Flow<R> { | ||||||||||||||||||||||||||||||||||
| return withLatestFrom(arrayOf(other2, other3)) { value, others -> | ||||||||||||||||||||||||||||||||||
| transform( | ||||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||||
| others[0] as B, | ||||||||||||||||||||||||||||||||||
| others[1] as C, | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+132
to
+135
|
||||||||||||||||||||||||||||||||||
| transform( | |
| value, | |
| others[0] as B, | |
| others[1] as C, | |
| val b = others[0] | |
| val c = others[1] | |
| if (b !is B) { | |
| throw IllegalStateException("Expected others[0] to be of type B, but was ${b?.let { it::class }}") | |
| } | |
| if (c !is C) { | |
| throw IllegalStateException("Expected others[1] to be of type C, but was ${c?.let { it::class }}") | |
| } | |
| transform( | |
| value, | |
| b, | |
| c, |
Oops, something went wrong.
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.
The code creates an intermediate
valuesarray and then immediately creates anotherunboxedValuesarray. This could be optimized by combining both operations into a single array creation to reduce memory allocations.