Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Bug 1877588 - TranslationsMiddleware Dispatcher Scope Issue
Browse files Browse the repository at this point in the history
This bug is to fix the `Must have a Handler` issue in the
TranslationsMiddleware when attempting to call using `Dispatcher.IO`.
  • Loading branch information
ohall-m authored and mergify[bot] committed Feb 5, 2024
1 parent 0a66e9a commit ea63f11
Showing 1 changed file with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package mozilla.components.browser.state.engine.middleware

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.components.browser.state.action.BrowserAction
import mozilla.components.browser.state.action.TranslationsAction
import mozilla.components.browser.state.action.TranslationsAction.TranslateExpectedAction
Expand Down Expand Up @@ -80,35 +78,33 @@ class TranslationsMiddleware(
* @param context Context to use to dispatch to the store.
* @param tabId Tab ID associated with the request.
*/
private suspend fun requestSupportedLanguages(
private fun requestSupportedLanguages(
context: MiddlewareContext<BrowserState, BrowserAction>,
tabId: String,
) = withContext(Dispatchers.IO) {
scope.launch {
engine.getSupportedTranslationLanguages(

onSuccess = {
context.store.dispatch(
TranslationsAction.TranslateSetLanguagesAction(
tabId = tabId,
supportedLanguages = it,
),
)
logger.info("Success requesting supported languages.")
},

onError = {
context.store.dispatch(
TranslationsAction.TranslateExceptionAction(
tabId = tabId,
operation = TranslationOperation.FETCH_LANGUAGES,
translationError = TranslationError.CouldNotLoadLanguagesError(it),
),
)
logger.error("Error requesting supported languages: ", it)
},
)
}
) {
engine.getSupportedTranslationLanguages(

onSuccess = {
context.store.dispatch(
TranslationsAction.TranslateSetLanguagesAction(
tabId = tabId,
supportedLanguages = it,
),
)
logger.info("Success requesting supported languages.")
},

onError = {
context.store.dispatch(
TranslationsAction.TranslateExceptionAction(
tabId = tabId,
operation = TranslationOperation.FETCH_LANGUAGES,
translationError = TranslationError.CouldNotLoadLanguagesError(it),
),
)
logger.error("Error requesting supported languages: ", it)
},
)
}

/**
Expand All @@ -122,7 +118,7 @@ class TranslationsMiddleware(
private suspend fun requestTranslationPageSettings(
context: MiddlewareContext<BrowserState, BrowserAction>,
tabId: String,
) = withContext(Dispatchers.IO) {
) {
// Always offer setting
val alwaysOfferPopup: Boolean = engine.getTranslationsOfferPopup()

Expand Down

0 comments on commit ea63f11

Please sign in to comment.