-
Notifications
You must be signed in to change notification settings - Fork 673
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
Remove dependency on Dispatchers.Main.immediate for immediate dispatching in Compose. #2725
Open
colinrtwhite
wants to merge
30
commits into
main
Choose a base branch
from
colin/delayed_dispatch
base: main
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.
+281
−74
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
042751b
Remove dependency on Dispatchers.Main.immediate for immediate dispatc…
colinrtwhite faeb7be
Fix API.
colinrtwhite a906110
Docs.
colinrtwhite f70aed3
Add tests.
colinrtwhite e586ac5
Change strategy.
colinrtwhite 0a872e7
Rename dispatcher.
colinrtwhite eb00783
Slight tweak.
colinrtwhite d167d51
Rename and change implementation.
colinrtwhite 8146de6
Remove Android test.
colinrtwhite 10169b3
Fix tests.
colinrtwhite 080198a
Docs.
colinrtwhite 3ff04e5
Improve test coverage.
colinrtwhite 94661bb
Fix test.
colinrtwhite 6d4a8e3
Remove note.
colinrtwhite 5378aba
Improve tests.
colinrtwhite 7b2fd2b
Fix tests.
colinrtwhite 2346e27
Fix mutating shared variable across contexts.
colinrtwhite 829d7f9
Fix tests.
colinrtwhite dbded59
Fix tests.
colinrtwhite 394b22d
Tweak solution.
colinrtwhite 553e527
Fix style.
colinrtwhite 8356177
Guard against race conditions.
colinrtwhite f193279
Fix build.
colinrtwhite 2b11ee5
Improve solution.
colinrtwhite 9d3381c
Docs.
colinrtwhite 0e7aa84
Remove unnecessary optimization.
colinrtwhite 8ac95cc
Pass context through.
colinrtwhite ebddb12
Docs.
colinrtwhite 63c3f6c
Fix missing join.
colinrtwhite 5e53a1b
Don't pay for dispatch.
colinrtwhite 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 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 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
36 changes: 36 additions & 0 deletions
36
coil-compose-core/src/commonMain/kotlin/coil3/compose/internal/ForwardingCoroutineContext.kt
This file contains 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,36 @@ | ||
package coil3.compose.internal | ||
|
||
import kotlin.coroutines.CoroutineContext | ||
|
||
/** | ||
* A special [CoroutineContext] implementation that lets us observe changes to its elements. | ||
*/ | ||
internal class ForwardingCoroutineContext( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oo this is a neat trick. |
||
private val delegate: CoroutineContext, | ||
private val onNewContext: (old: CoroutineContext, new: CoroutineContext) -> Unit, | ||
) : CoroutineContext by delegate { | ||
|
||
override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext { | ||
val new = delegate.minusKey(key) | ||
onNewContext(this, new) | ||
return ForwardingCoroutineContext(new, onNewContext) | ||
} | ||
|
||
override operator fun plus(context: CoroutineContext): CoroutineContext { | ||
val new = delegate + context | ||
onNewContext(this, new) | ||
return ForwardingCoroutineContext(new, onNewContext) | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
return delegate == other | ||
} | ||
|
||
override fun hashCode(): Int { | ||
return delegate.hashCode() | ||
} | ||
|
||
override fun toString(): String { | ||
return delegate.toString() | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...e-core/src/commonMain/kotlin/coil3/compose/internal/ForwardingUnconfinedCoroutineScope.kt
This file contains 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,61 @@ | ||
package coil3.compose.internal | ||
|
||
import kotlin.coroutines.CoroutineContext | ||
import kotlinx.atomicfu.atomic | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.InternalCoroutinesApi | ||
import kotlinx.coroutines.Runnable | ||
|
||
/** | ||
* A [CoroutineScope] with a special [CoroutineContext] that enables [ForwardingUnconfinedCoroutineDispatcher] | ||
* to enable dispatching after it is replaced in the context. | ||
*/ | ||
internal fun ForwardingUnconfinedCoroutineScope( | ||
context: CoroutineContext, | ||
) = CoroutineScope( | ||
context = ForwardingCoroutineContext(context) { old, new -> | ||
val oldDispatcher = old.dispatcher | ||
val newDispatcher = new.dispatcher | ||
if (oldDispatcher is ForwardingUnconfinedCoroutineDispatcher && oldDispatcher != newDispatcher) { | ||
oldDispatcher.unconfined = oldDispatcher.unconfined && | ||
(newDispatcher == null || newDispatcher == Dispatchers.Unconfined) | ||
} | ||
} | ||
) | ||
|
||
/** | ||
* A [CoroutineDispatcher] that delegates to [Dispatchers.Unconfined] while [unconfined] is true | ||
* and [delegate] when [unconfined] is false. | ||
*/ | ||
internal class ForwardingUnconfinedCoroutineDispatcher( | ||
private val delegate: CoroutineDispatcher, | ||
) : CoroutineDispatcher() { | ||
private val _unconfined = atomic(true) | ||
var unconfined by _unconfined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider documenting that after setting this to false, it's ignored even if set to true again. |
||
|
||
private val currentDispatcher: CoroutineDispatcher | ||
get() = if (_unconfined.value) Dispatchers.Unconfined else delegate | ||
|
||
override fun isDispatchNeeded(context: CoroutineContext): Boolean { | ||
return currentDispatcher.isDispatchNeeded(context) | ||
} | ||
|
||
override fun limitedParallelism(parallelism: Int, name: String?): CoroutineDispatcher { | ||
return currentDispatcher.limitedParallelism(parallelism, name) | ||
} | ||
|
||
override fun dispatch(context: CoroutineContext, block: Runnable) { | ||
currentDispatcher.dispatch(context, block) | ||
} | ||
|
||
@InternalCoroutinesApi | ||
override fun dispatchYield(context: CoroutineContext, block: Runnable) { | ||
currentDispatcher.dispatchYield(context, block) | ||
} | ||
|
||
override fun toString(): String { | ||
return "ForwardingUnconfinedCoroutineDispatcher(delegate=$delegate)" | ||
} | ||
} |
This file contains 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.
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.
Took me a while to understand how
ForwardingUnconfinedCoroutineScope
andForwardingUnconfinedCoroutineDispatcher
interact. Could the code that wires them up with the launch be extracted into a helper function? That logic seems mostly unrelated to the other logic in this function, which is more high-level, and pulling it out might make this a bit more readable.Eg. (names are arbitrary):
but that might be a bit over-engineered, just an idea.