-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
Store#stateFlow(Lifecycle)
and `Store#labelsChannel(Lifecycle…
…)` API, promoted to stable
- Loading branch information
Showing
8 changed files
with
328 additions
and
12 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
106 changes: 106 additions & 0 deletions
106
...est/kotlin/com/arkivanov/mvikotlin/extensions/coroutines/LabelChannelWithLifecycleTest.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,106 @@ | ||
package com.arkivanov.mvikotlin.extensions.coroutines | ||
|
||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.lifecycle.LifecycleRegistry | ||
import com.arkivanov.essenty.lifecycle.destroy | ||
import com.arkivanov.mvikotlin.core.rx.Disposable | ||
import com.arkivanov.mvikotlin.core.rx.Observer | ||
import com.arkivanov.mvikotlin.core.store.Store | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertNull | ||
import kotlin.test.assertTrue | ||
|
||
@Suppress("TestFunctionName") | ||
class LabelChannelWithLifecycleTest { | ||
|
||
@Test | ||
fun WHEN_label_emitted_THEN_label_collected() { | ||
val store = TestStore() | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val channel = store.labelsChannel(LifecycleRegistry()) | ||
val labels = ArrayList<Int>() | ||
|
||
store.labelObserver?.onNext(1) | ||
|
||
scope.launch { | ||
for (label in channel) { | ||
labels += label | ||
} | ||
} | ||
|
||
store.labelObserver?.onNext(2) | ||
store.labelObserver?.onNext(3) | ||
|
||
assertContentEquals(listOf(1, 2, 3), labels) | ||
} | ||
|
||
@Test | ||
fun WHEN_lifecycle_destroyed_THEN_unsubscribed_from_store() { | ||
val store = TestStore() | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val lifecycle = LifecycleRegistry(Lifecycle.State.CREATED) | ||
val channel = store.labelsChannel(lifecycle) | ||
|
||
scope.launch { | ||
while (true) { | ||
channel.receive() | ||
} | ||
} | ||
|
||
lifecycle.destroy() | ||
|
||
assertNull(store.labelObserver) | ||
} | ||
|
||
@OptIn(DelicateCoroutinesApi::class) | ||
@Test | ||
fun WHEN_lifecycle_destroyed_THEN_channel_cancelled() { | ||
val store = TestStore() | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val lifecycle = LifecycleRegistry(Lifecycle.State.CREATED) | ||
val channel = store.labelsChannel(lifecycle) | ||
|
||
scope.launch { | ||
while (true) { | ||
channel.receive() | ||
} | ||
} | ||
|
||
lifecycle.destroy() | ||
|
||
assertTrue(channel.isClosedForReceive) | ||
} | ||
|
||
private class TestStore : Store<Int, Int, Int> { | ||
override val state: Int = 0 | ||
override val isDisposed: Boolean = false | ||
|
||
var labelObserver: Observer<Int>? = null | ||
private set | ||
|
||
override fun states(observer: Observer<Int>): Disposable = error("Not required") | ||
|
||
override fun labels(observer: Observer<Int>): Disposable { | ||
labelObserver = observer | ||
|
||
return Disposable { labelObserver = null } | ||
} | ||
|
||
override fun accept(intent: Int) { | ||
// no-op | ||
} | ||
|
||
override fun init() { | ||
// no-op | ||
} | ||
|
||
override fun dispose() { | ||
// no-op | ||
} | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
...onTest/kotlin/com/arkivanov/mvikotlin/extensions/coroutines/StateFlowWithLifecycleTest.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,80 @@ | ||
package com.arkivanov.mvikotlin.extensions.coroutines | ||
|
||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.lifecycle.LifecycleRegistry | ||
import com.arkivanov.essenty.lifecycle.destroy | ||
import com.arkivanov.mvikotlin.core.rx.Disposable | ||
import com.arkivanov.mvikotlin.core.rx.Observer | ||
import com.arkivanov.mvikotlin.core.store.Store | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertNull | ||
|
||
@Suppress("TestFunctionName") | ||
class StateFlowWithLifecycleTest { | ||
|
||
@Test | ||
fun WHEN_state_emitted_THEN_state_collected() { | ||
val store = TestStore() | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val flow = store.stateFlow(LifecycleRegistry()) | ||
val items = ArrayList<Int>() | ||
|
||
scope.launch { | ||
flow.collect { items += it } | ||
} | ||
|
||
store.stateObserver?.onNext(1) | ||
store.stateObserver?.onNext(2) | ||
store.stateObserver?.onNext(3) | ||
|
||
assertContentEquals(listOf(0, 1, 2, 3), items) | ||
} | ||
|
||
@Test | ||
fun WHEN_lifecycle_destroyed_THEN_unsubscribed_from_store() { | ||
val store = TestStore() | ||
val lifecycle = LifecycleRegistry(Lifecycle.State.CREATED) | ||
val scope = CoroutineScope(Dispatchers.Unconfined) | ||
val flow = store.stateFlow(lifecycle) | ||
|
||
scope.launch { | ||
flow.collect {} | ||
} | ||
|
||
lifecycle.destroy() | ||
|
||
assertNull(store.stateObserver) | ||
} | ||
|
||
private class TestStore : Store<Int, Int, Int> { | ||
override val state: Int = 0 | ||
override val isDisposed: Boolean = false | ||
|
||
var stateObserver: Observer<Int>? = null | ||
private set | ||
|
||
override fun states(observer: Observer<Int>): Disposable { | ||
stateObserver = observer | ||
|
||
return Disposable { stateObserver = null } | ||
} | ||
|
||
override fun labels(observer: Observer<Int>): Disposable = error("Not required") | ||
|
||
override fun accept(intent: Int) { | ||
// no-op | ||
} | ||
|
||
override fun init() { | ||
// no-op | ||
} | ||
|
||
override fun dispose() { | ||
// no-op | ||
} | ||
} | ||
} |
Oops, something went wrong.