tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

HistoryStorageMiddlewareExample.kt (1522B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // This is example code for the 'Simplified Example' section of
      6 // /docs/architecture-overview.md
      7 class HistoryStorageMiddleware(
      8    private val storage: HistoryStorage
      9    private val scope: CoroutineScope,
     10 ) : Middleware<HistoryState, HistoryAction> {
     11    override fun invoke(
     12        context: MiddlewareContext<HistoryState, HistoryAction>,
     13        next: (HistoryAction) -> Unit,
     14        action: HistoryAction,
     15    ) {
     16        // This middleware won't need to manipulate the action, so the action can be passed through
     17        // the middleware chain before the side-effects are initiated
     18        next(action)
     19        when(action) {
     20            is HistoryAction.Init -> {
     21                scope.launch {
     22                    val history = storage.load()
     23                    context.store.dispatch(HistoryAction.ItemsChanged(history))
     24                }
     25            }
     26            is HistoryAction.DeleteItems -> {
     27                scope.launch {
     28                    val currentItems = context.state.items
     29                    if (storage.delete(action.items) is HistoryStorage.Success) {
     30                        context.store.dispatch(
     31                            HistoryAction.DeleteFinished()
     32                        )
     33                    }
     34                }
     35            }
     36            else -> Unit
     37        }
     38    }
     39 }