Commit 6b1a7795 authored by Arturo Mejia's avatar Arturo Mejia
Browse files

Closes #8271: Fix DownloadMiddleware intermittent test

parent 874688df
...@@ -21,7 +21,6 @@ import mozilla.components.browser.state.state.content.DownloadState.Status.CANCE ...@@ -21,7 +21,6 @@ import mozilla.components.browser.state.state.content.DownloadState.Status.CANCE
import mozilla.components.browser.state.state.content.DownloadState.Status.COMPLETED import mozilla.components.browser.state.state.content.DownloadState.Status.COMPLETED
import mozilla.components.lib.state.Middleware import mozilla.components.lib.state.Middleware
import mozilla.components.lib.state.MiddlewareContext import mozilla.components.lib.state.MiddlewareContext
import mozilla.components.lib.state.Store
import mozilla.components.support.base.log.logger.Logger import mozilla.components.support.base.log.logger.Logger
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
...@@ -49,10 +48,10 @@ class DownloadMiddleware( ...@@ -49,10 +48,10 @@ class DownloadMiddleware(
action: BrowserAction action: BrowserAction
) { ) {
when (action) { when (action) {
is DownloadAction.RemoveDownloadAction -> removeDownload(action.downloadId, context.store) is DownloadAction.RemoveDownloadAction -> removeDownload(action.downloadId, context)
is DownloadAction.RemoveAllDownloadsAction -> removeDownloads() is DownloadAction.RemoveAllDownloadsAction -> removeDownloads()
is DownloadAction.UpdateDownloadAction -> updateDownload(action.download, context.store) is DownloadAction.UpdateDownloadAction -> updateDownload(action.download, context)
is DownloadAction.RestoreDownloadsStateAction -> restoreDownloads(context.store) is DownloadAction.RestoreDownloadsStateAction -> restoreDownloads(context)
} }
next(action) next(action)
...@@ -71,8 +70,11 @@ class DownloadMiddleware( ...@@ -71,8 +70,11 @@ class DownloadMiddleware(
} }
} }
private fun removeDownload(downloadId: String, store: Store<BrowserState, BrowserAction>) = scope.launch { private fun removeDownload(
store.state.downloads[downloadId]?.let { downloadId: String,
context: MiddlewareContext<BrowserState, BrowserAction>
) = scope.launch {
context.state.downloads[downloadId]?.let {
downloadStorage.remove(it) downloadStorage.remove(it)
logger.debug("Removed download ${it.fileName} from the storage") logger.debug("Removed download ${it.fileName} from the storage")
} }
...@@ -82,8 +84,8 @@ class DownloadMiddleware( ...@@ -82,8 +84,8 @@ class DownloadMiddleware(
downloadStorage.removeAllDownloads() downloadStorage.removeAllDownloads()
} }
private fun updateDownload(updated: DownloadState, store: Store<BrowserState, BrowserAction>) { private fun updateDownload(updated: DownloadState, context: MiddlewareContext<BrowserState, BrowserAction>) {
store.state.downloads[updated.id]?.let { old -> context.state.downloads[updated.id]?.let { old ->
// To not overwhelm the storage, we only send updates that are relevant, // To not overwhelm the storage, we only send updates that are relevant,
// we only care about properties, that we are stored on the storage. // we only care about properties, that we are stored on the storage.
if (!DownloadStorage.isSameDownload(old, updated)) { if (!DownloadStorage.isSameDownload(old, updated)) {
...@@ -95,11 +97,11 @@ class DownloadMiddleware( ...@@ -95,11 +97,11 @@ class DownloadMiddleware(
} }
} }
private fun restoreDownloads(store: Store<BrowserState, BrowserAction>) = scope.launch { private fun restoreDownloads(context: MiddlewareContext<BrowserState, BrowserAction>) = scope.launch {
downloadStorage.getDownloads().collect { downloads -> downloadStorage.getDownloads().collect { downloads ->
downloads.forEach { download -> downloads.forEach { download ->
if (!store.state.downloads.containsKey(download.id)) { if (!context.state.downloads.containsKey(download.id)) {
store.dispatch(DownloadAction.RestoreDownloadStateAction(download)) context.dispatch(DownloadAction.RestoreDownloadStateAction(download))
logger.debug("Download restarted from the storage ${download.fileName}") logger.debug("Download restarted from the storage ${download.fileName}")
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment