-
-
Notifications
You must be signed in to change notification settings - Fork 35
Added Store#stateFlow(Lifecycle)
and Store#labelsChannel(Lifecycle)
API, promoted to stable
#148
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
Merged
Changes from all commits
Commits
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
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
106 changes: 106 additions & 0 deletions
106
...est/kotlin/com/arkivanov/mvikotlin/extensions/coroutines/LabelChannelWithLifecycleTest.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,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 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
80 changes: 80 additions & 0 deletions
80
...onTest/kotlin/com/arkivanov/mvikotlin/extensions/coroutines/StateFlowWithLifecycleTest.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,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 } | ||
} | ||
arkivanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.