Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Introduce state to indicate if restoring tabs is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
csadilek authored and mergify[bot] committed Oct 2, 2020
1 parent 0a57b87 commit 277e5ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ sealed class BrowserAction : Action
*/
object InitAction : BrowserAction()

/**
* [BrowserAction] to indicate that restoring [BrowserState] is complete.
*/
object RestoreCompleteAction : BrowserAction()

/**
* [BrowserAction] implementations to react to system events.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import mozilla.components.browser.state.action.SystemAction
import mozilla.components.browser.state.action.TabListAction
import mozilla.components.browser.state.action.LastAccessAction
import mozilla.components.browser.state.action.RecentlyClosedAction
import mozilla.components.browser.state.action.RestoreCompleteAction
import mozilla.components.browser.state.action.TrackingProtectionAction
import mozilla.components.browser.state.action.UndoAction
import mozilla.components.browser.state.action.WebExtensionAction
Expand All @@ -39,6 +40,7 @@ internal object BrowserStateReducer {
fun reduce(state: BrowserState, action: BrowserAction): BrowserState {
return when (action) {
is InitAction -> state
is RestoreCompleteAction -> state.copy(restoreComplete = true)
is ContainerAction -> ContainerReducer.reduce(state, action)
is RecentlyClosedAction -> RecentlyClosedReducer.reduce(state, action)
is ContentAction -> ContentStateReducer.reduce(state, action)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import mozilla.components.lib.state.State
* @property search the state of search for this browser state.
* @property downloads Downloads ([DownloadState]s) mapped to their IDs.
* @property undoHistory History of recently closed tabs to support "undo" (Requires UndoMiddleware).
* @property restoreComplete Whether or not restoring [BrowserState] has completed. This can be used
* on application startup e.g. as an indicator that tabs have been restored.
*/
data class BrowserState(
val tabs: List<TabSessionState> = emptyList(),
Expand All @@ -34,5 +36,6 @@ data class BrowserState(
val media: MediaState = MediaState(),
val downloads: Map<String, DownloadState> = emptyMap(),
val search: SearchState = SearchState(),
val undoHistory: UndoHistoryState = UndoHistoryState()
val undoHistory: UndoHistoryState = UndoHistoryState(),
val restoreComplete: Boolean = false
) : State
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ package mozilla.components.browser.state.store
import kotlinx.coroutines.runBlocking
import mozilla.components.browser.state.action.BrowserAction
import mozilla.components.browser.state.action.InitAction
import mozilla.components.browser.state.action.RestoreCompleteAction
import mozilla.components.browser.state.action.TabListAction
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.createTab
import mozilla.components.lib.state.Middleware
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
Expand Down Expand Up @@ -77,4 +80,16 @@ class BrowserStoreTest {
store.waitUntilIdle()
assertTrue(initActionObserved)
}

@Test
fun `RestoreCompleteAction updates state`() {
val store = BrowserStore()
assertFalse(store.state.restoreComplete)

store.dispatch(RestoreCompleteAction).joinBlocking()
assertTrue(store.state.restoreComplete)

store.dispatch(RestoreCompleteAction).joinBlocking()
assertTrue(store.state.restoreComplete)
}
}

0 comments on commit 277e5ae

Please sign in to comment.