Skip to content
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

AND-8881 [Tokens] Improved the logic for observing token list flow #3622

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.tangem.domain.core.utils.lceContent
import com.tangem.domain.core.utils.lceError
import com.tangem.domain.core.utils.lceLoading
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.channels.ProducerScope
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -38,21 +37,16 @@ class LceFlowScope<E : Any, C : Any> @PublishedApi internal constructor(
val isLoading: AtomicBoolean = AtomicBoolean(value = true)

/**
* Sends a error of type [E] within the [ProducerScope] and then closes it for send.
* All subsequent sends will be ignored.
* Sends an error of type [E] within the [ProducerScope] without closing.
*
* This method blocks the coroutine until a error is handled by the receiver.
*
* If the [ProducerScope] is already closed for send (e.g. after rising another error), it just raises [r]
* without closing.
* This method blocks the coroutine until an error is handled by the receiver.
*
* @param r Error to raise.
*/
override fun raise(r: E): Nothing {
isLoading.set(false)

producerScope.trySendBlocking(r.lceError())
producerScope.close()

raise.raise(r.lceError())
}
Expand Down Expand Up @@ -88,14 +82,9 @@ class LceFlowScope<E : Any, C : Any> @PublishedApi internal constructor(
*
* This method suspends until the [Lce] instance is handled by the receiver.
*
* If the [ProducerScope] is closed for send (e.g. after rising a error), it does nothing.
*
* @param value The [Lce] instance to send.
*/
@OptIn(DelicateCoroutinesApi::class)
suspend fun send(value: Lce<E, C>) {
if (producerScope.isClosedForSend) return

isLoading.set(value.isLoading())

producerScope.send(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ sealed class TokenList {
) : TokenList()

/** Represents a state where the token list is empty. */
object Empty : TokenList() {
data object Empty : TokenList() {

override val totalFiatBalance: TotalFiatBalance = TotalFiatBalance.Loaded(
amount = BigDecimal.ZERO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ internal class CurrenciesStatusesLceOperations(
::createCurrenciesStatuses,
)
.distinctUntilChanged()
.mapLatest { maybeCurrenciesStatuses ->
.collectLatest { maybeCurrenciesStatuses ->
send(maybeCurrenciesStatuses)
}
.launchIn(scope = this)
}
}

Expand Down
Loading