Advertisement
Guest User

Untitled

a guest
Apr 9th, 2022
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.64 KB | None | 0 0
  1. interface MyStore : Store<Intent, State, Label> {
  2.     sealed interface Intent {
  3.         object Load : Intent
  4.         data class Set(val value: String) : Intent
  5.     }
  6.  
  7.     data class State(val value: String = "")
  8.  
  9.     sealed interface Label {
  10.         object Loaded : Label
  11.     }
  12. }
  13.  
  14. fun MyStore(
  15.     storeFactory: StoreFactory,
  16.     mainContext: CoroutineContext,
  17.     ioContext: CoroutineContext,
  18. ): MyStore =
  19.     object : MyStore, Store<Intent, State, Label> by storeFactory.create<Intent, Unit, Msg, State, Label>(
  20.         name = "MyStore",
  21.         initialState = State(),
  22.         bootstrapper = bootstrapper(mainContext = mainContext) {
  23.             scope.launch {
  24.                 delay(1000L) // Do something async
  25.                 dispatch(Unit)
  26.             }
  27.         },
  28.         executorFactory = executorFactory(mainContext = mainContext) {
  29.             onAction<Unit> { _, _ ->
  30.                 dispatch(Msg.Value(value = "mda"))
  31.             }
  32.  
  33.             onIntent<Intent.Load> { _, _ ->
  34.                 scope.launch {
  35.                     withContext(ioContext) {
  36.                         delay(1000L) // Loading
  37.                     }
  38.  
  39.                     dispatch(Msg.Value(value = "heh"))
  40.                     publish(Label.Loaded)
  41.                 }
  42.             }
  43.  
  44.             onIntent<Intent.Set> { intent, _ ->
  45.                 dispatch(Msg.Value(value = intent.value))
  46.             }
  47.         },
  48.         reducer = { msg ->
  49.             when (msg) {
  50.                 is Msg.Value -> copy(value = msg.value)
  51.             }
  52.         },
  53.     ) {}
  54.  
  55. private sealed interface Msg {
  56.     data class Value(val value: String) : Msg
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement