-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement to provide ComposeCoroutineScope
- Loading branch information
1 parent
d7d95a5
commit adba37c
Showing
13 changed files
with
226 additions
and
15 deletions.
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
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
13 changes: 13 additions & 0 deletions
13
...ompose-core/src/commonMain/kotlin/com/moriatsushi/koject/compose/ComposeCoroutineScope.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,13 @@ | ||
package com.moriatsushi.koject.compose | ||
|
||
import androidx.compose.runtime.rememberCoroutineScope | ||
import com.moriatsushi.koject.Qualifier | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
/** | ||
* Get a Composable [CoroutineScope] that the same as [rememberCoroutineScope]. | ||
*/ | ||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
@MustBeDocumented | ||
annotation class ComposeCoroutineScope |
45 changes: 45 additions & 0 deletions
45
...compose-core/src/commonMain/kotlin/com/moriatsushi/koject/compose/CoroutineScopeHolder.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,45 @@ | ||
package com.moriatsushi.koject.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.RememberObserver | ||
import androidx.compose.runtime.remember | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancel | ||
|
||
internal interface CoroutineScopeHolder { | ||
val coroutineScope: CoroutineScope | ||
} | ||
|
||
private class CoroutineScopeHolderImpl( | ||
private val coroutineContext: CoroutineContext, | ||
) : CoroutineScopeHolder, RememberObserver { | ||
private var _coroutineScope: CoroutineScope? = null | ||
override val coroutineScope: CoroutineScope | ||
get() = _coroutineScope ?: CoroutineScope(coroutineContext).also { | ||
_coroutineScope = it | ||
} | ||
|
||
override fun onRemembered() { | ||
// no op | ||
} | ||
|
||
override fun onForgotten() { | ||
_coroutineScope?.cancel() | ||
} | ||
|
||
override fun onAbandoned() { | ||
_coroutineScope?.cancel() | ||
} | ||
} | ||
|
||
@Composable | ||
internal fun rememberCoroutineScopeHolder(): CoroutineScopeHolder { | ||
return remember { | ||
CoroutineScopeHolderImpl( | ||
Job() + Dispatchers.Main.immediate, | ||
) | ||
} | ||
} |
17 changes: 13 additions & 4 deletions
17
...e/src/desktopMain/kotlin/com/moriatsushi/koject/compose/ComposeComponentExtras.desktop.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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
package com.moriatsushi.koject.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import com.moriatsushi.koject.ExperimentalKojectApi | ||
import com.moriatsushi.koject.component.ComponentExtras | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
@OptIn(ExperimentalKojectApi::class) | ||
internal actual class ComposeComponentExtras : ComponentExtras<ComposeComponent> | ||
|
||
private val instance = ComposeComponentExtras() | ||
internal actual class ComposeComponentExtras( | ||
private val coroutineScopeHolder: CoroutineScopeHolder, | ||
) : ComponentExtras<ComposeComponent> { | ||
@ComposeCoroutineScope | ||
actual val coroutineScope: CoroutineScope | ||
get() = coroutineScopeHolder.coroutineScope | ||
} | ||
|
||
@PublishedApi | ||
@Composable | ||
internal actual fun composeComponentExtras(): ComposeComponentExtras { | ||
return instance | ||
val coroutineScopeHolder = rememberCoroutineScopeHolder() | ||
return remember { | ||
ComposeComponentExtras(coroutineScopeHolder) | ||
} | ||
} |
17 changes: 13 additions & 4 deletions
17
...t-compose-core/src/jsMain/kotlin/com/moriatsushi/koject/compose/ComposeComponentExtras.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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
package com.moriatsushi.koject.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import com.moriatsushi.koject.ExperimentalKojectApi | ||
import com.moriatsushi.koject.component.ComponentExtras | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
@OptIn(ExperimentalKojectApi::class) | ||
internal actual class ComposeComponentExtras : ComponentExtras<ComposeComponent> | ||
|
||
private val instance = ComposeComponentExtras() | ||
internal actual class ComposeComponentExtras( | ||
private val coroutineScopeHolder: CoroutineScopeHolder, | ||
) : ComponentExtras<ComposeComponent> { | ||
@ComposeCoroutineScope | ||
actual val coroutineScope: CoroutineScope | ||
get() = coroutineScopeHolder.coroutineScope | ||
} | ||
|
||
@PublishedApi | ||
@Composable | ||
internal actual fun composeComponentExtras(): ComposeComponentExtras { | ||
return instance | ||
val coroutineScopeHolder = rememberCoroutineScopeHolder() | ||
return remember { | ||
ComposeComponentExtras(coroutineScopeHolder) | ||
} | ||
} |
17 changes: 13 additions & 4 deletions
17
...mpose-core/src/nativeMain/kotlin/com/moriatsushi/koject/compose/ComposeComponentExtras.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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
package com.moriatsushi.koject.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import com.moriatsushi.koject.ExperimentalKojectApi | ||
import com.moriatsushi.koject.component.ComponentExtras | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
@OptIn(ExperimentalKojectApi::class) | ||
internal actual class ComposeComponentExtras : ComponentExtras<ComposeComponent> | ||
|
||
private val instance = ComposeComponentExtras() | ||
internal actual class ComposeComponentExtras( | ||
private val coroutineScopeHolder: CoroutineScopeHolder, | ||
) : ComponentExtras<ComposeComponent> { | ||
@ComposeCoroutineScope | ||
actual val coroutineScope: CoroutineScope | ||
get() = coroutineScopeHolder.coroutineScope | ||
} | ||
|
||
@PublishedApi | ||
@Composable | ||
internal actual fun composeComponentExtras(): ComposeComponentExtras { | ||
return instance | ||
val coroutineScopeHolder = rememberCoroutineScopeHolder() | ||
return remember { | ||
ComposeComponentExtras(coroutineScopeHolder) | ||
} | ||
} |
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
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
51 changes: 51 additions & 0 deletions
51
...jsTest/kotlin/com/moriatsushi/koject/integrationtest/compose/ComposeCoroutineScopeTest.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,51 @@ | ||
package com.moriatsushi.koject.integrationtest.compose | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import com.moriatsushi.koject.Koject | ||
import com.moriatsushi.koject.compose.rememberInject | ||
import kotlin.test.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
import kotlinx.coroutines.isActive | ||
import org.jetbrains.compose.web.testutils.ComposeWebExperimentalTestsApi | ||
import org.jetbrains.compose.web.testutils.runTest | ||
|
||
@OptIn(ComposeWebExperimentalTestsApi::class) | ||
class ComposeCoroutineScopeTest { | ||
@Test | ||
fun successInject() = Koject.runTest { | ||
runTest { | ||
var value: ForComposeWithCoroutineScope? = null | ||
|
||
composition { | ||
value = rememberInject() | ||
} | ||
|
||
assertNotNull(value) | ||
} | ||
} | ||
|
||
@Test | ||
fun cancelScope_whenHidden() = Koject.runTest { | ||
runTest { | ||
var visible by mutableStateOf(true) | ||
var value: ForComposeWithCoroutineScope? = null | ||
|
||
composition { | ||
if (visible) { | ||
value = rememberInject() | ||
} | ||
} | ||
|
||
assertTrue(value!!.scope.isActive) | ||
|
||
visible = false | ||
waitForRecompositionComplete() | ||
|
||
assertFalse(value!!.scope.isActive) | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...vmTest/kotlin/com/moriatsushi/koject/integrationtest/compose/ComposeCoroutineScopeTest.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,51 @@ | ||
package com.moriatsushi.koject.integrationtest.compose | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import com.moriatsushi.koject.Koject | ||
import com.moriatsushi.koject.compose.rememberInject | ||
import com.moriatsushi.koject.integrationtest.compose.junit.RunWith | ||
import com.moriatsushi.koject.integrationtest.compose.junit.UITestRunner | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertTrue | ||
import kotlinx.coroutines.isActive | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
@RunWith(UITestRunner::class) | ||
class ComposeCoroutineScopeTest { | ||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun successInject() = Koject.runTest { | ||
var value: ForComposeWithCoroutineScope? = null | ||
|
||
composeTestRule.setContent { | ||
value = rememberInject() | ||
} | ||
|
||
assertNotNull(value) | ||
} | ||
|
||
@Test | ||
fun cancelScope_whenHidden() = Koject.runTest { | ||
var visible by mutableStateOf(true) | ||
var value: ForComposeWithCoroutineScope? = null | ||
|
||
composeTestRule.setContent { | ||
if (visible) { | ||
value = rememberInject() | ||
} | ||
} | ||
|
||
assertTrue(value!!.scope.isActive) | ||
|
||
visible = false | ||
composeTestRule.waitForIdle() | ||
|
||
assertFalse(value!!.scope.isActive) | ||
} | ||
} |