Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #11943: Fix intermittent coroutine failure (#12027)
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods authored Jun 26, 2020
1 parent df49db6 commit 4f6f078
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package org.mozilla.fenix.collections

import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
Expand Down Expand Up @@ -62,13 +61,21 @@ fun List<Tab>.toSessionBundle(sessionManager: SessionManager): List<Session> {
return this.mapNotNull { sessionManager.findSessionById(it.sessionId) }
}

/**
* @param store Store used to hold in-memory collection state.
* @param dismiss Callback to dismiss the collection creation dialog.
* @param metrics Controller that handles telemetry events.
* @param tabCollectionStorage Storage used to save tab collections to disk.
* @param sessionManager Used to query and serialize tabs.
* @param ioScope Coroutine scope that launches on the IO thread.
*/
class DefaultCollectionCreationController(
private val store: CollectionCreationStore,
private val dismiss: () -> Unit,
private val metrics: MetricController,
private val tabCollectionStorage: TabCollectionStorage,
private val sessionManager: SessionManager,
private val scope: CoroutineScope
private val ioScope: CoroutineScope
) : CollectionCreationController {

companion object {
Expand All @@ -80,7 +87,7 @@ class DefaultCollectionCreationController(
dismiss()

val sessionBundle = tabs.toSessionBundle(sessionManager)
scope.launch(IO) {
ioScope.launch {
tabCollectionStorage.createCollection(name, sessionBundle)
}

Expand All @@ -91,7 +98,7 @@ class DefaultCollectionCreationController(

override fun renameCollection(collection: TabCollection, name: String) {
dismiss()
scope.launch(IO) {
ioScope.launch {
tabCollectionStorage.renameCollection(collection, name)
}
metrics.track(Event.CollectionRenamed)
Expand Down Expand Up @@ -121,7 +128,7 @@ class DefaultCollectionCreationController(
override fun selectCollection(collection: TabCollection, tabs: List<Tab>) {
dismiss()
val sessionBundle = tabs.toList().toSessionBundle(sessionManager)
scope.launch(IO) {
ioScope.launch {
tabCollectionStorage
.addTabsToCollection(collection, sessionBundle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.navArgs
import kotlinx.android.synthetic.main.fragment_create_collection.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.plus
import mozilla.components.browser.session.SessionManager
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
Expand Down Expand Up @@ -77,7 +79,7 @@ class CollectionCreationFragment : DialogFragment() {
requireComponents.analytics.metrics,
requireComponents.core.tabCollectionStorage,
requireComponents.core.sessionManager,
scope = lifecycleScope
ioScope = lifecycleScope + Dispatchers.IO
)
)
collectionCreationView = CollectionCreationView(
Expand Down

0 comments on commit 4f6f078

Please sign in to comment.