Skip to content

Commit 66bb0c6

Browse files
Mugurellplingurar@mozilla.com
authored andcommitted
Bug 1986002 - Don't block the current thread while trying to emit new state r=Roger,jonalmeida,android-reviewers
This comes to avoid a potential deadlock with the Main thread while dispatching new actions or observing state changes. Differential Revision: https://phabricator.services.mozilla.com/D263132
1 parent 78a41aa commit 66bb0c6

File tree

1 file changed

+2
-20
lines changed
  • mobile/android/android-components/components/lib/state/src/main/java/mozilla/components/lib/state/ext

1 file changed

+2
-20
lines changed

mobile/android/android-components/components/lib/state/src/main/java/mozilla/components/lib/state/ext/StoreExtensions.kt

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import androidx.lifecycle.DefaultLifecycleObserver
1010
import androidx.lifecycle.Lifecycle
1111
import androidx.lifecycle.LifecycleOwner
1212
import androidx.lifecycle.ProcessLifecycleOwner
13-
import kotlinx.coroutines.CancellationException
1413
import kotlinx.coroutines.CoroutineScope
1514
import kotlinx.coroutines.MainScope
1615
import kotlinx.coroutines.channels.Channel
@@ -20,7 +19,6 @@ import kotlinx.coroutines.flow.Flow
2019
import kotlinx.coroutines.flow.buffer
2120
import kotlinx.coroutines.flow.channelFlow
2221
import kotlinx.coroutines.launch
23-
import kotlinx.coroutines.runBlocking
2422
import mozilla.components.lib.state.Action
2523
import mozilla.components.lib.state.Observer
2624
import mozilla.components.lib.state.State
@@ -119,15 +117,7 @@ fun <S : State, A : Action> Store<S, A>.channel(
119117
val channel = Channel<S>(Channel.CONFLATED)
120118

121119
val subscription = observeManually { state ->
122-
runBlocking {
123-
try {
124-
channel.send(state)
125-
} catch (e: CancellationException) {
126-
// It's possible for this channel to have been closed concurrently before
127-
// we had a chance to unsubscribe. In this case we can just ignore this
128-
// one subscription and keep going.
129-
}
130-
}
120+
channel.trySend(state)
131121
}
132122

133123
subscription.binding = SubscriptionLifecycleBinding(owner, subscription).apply {
@@ -171,15 +161,7 @@ fun <S : State, A : Action> Store<S, A>.flow(
171161
owner?.lifecycle?.removeObserver(ownerDestroyedObserver)
172162

173163
val subscription = observeManually { state ->
174-
runBlocking {
175-
try {
176-
send(state)
177-
} catch (e: CancellationException) {
178-
// It's possible for this channel to have been closed concurrently before
179-
// we had a chance to unsubscribe. In this case we can just ignore this
180-
// one subscription and keep going.
181-
}
182-
}
164+
trySend(state)
183165
}
184166

185167
if (owner == null) {

0 commit comments

Comments
 (0)