Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface MyStore : Store<Intent, State, Label> {
- sealed interface Intent {
- object Load : Intent
- data class Set(val value: String) : Intent
- }
- data class State(val value: String = "")
- sealed interface Label {
- object Loaded : Label
- }
- }
- fun MyStore(
- storeFactory: StoreFactory,
- mainContext: CoroutineContext,
- ioContext: CoroutineContext,
- ): MyStore =
- object : MyStore, Store<Intent, State, Label> by storeFactory.create<Intent, Unit, Msg, State, Label>(
- name = "MyStore",
- initialState = State(),
- bootstrapper = bootstrapper(mainContext = mainContext) {
- scope.launch {
- delay(1000L) // Do something async
- dispatch(Unit)
- }
- },
- executorFactory = executorFactory(mainContext = mainContext) {
- onAction<Unit> { _, _ ->
- dispatch(Msg.Value(value = "mda"))
- }
- onIntent<Intent.Load> { _, _ ->
- scope.launch {
- withContext(ioContext) {
- delay(1000L) // Loading
- }
- dispatch(Msg.Value(value = "heh"))
- publish(Label.Loaded)
- }
- }
- onIntent<Intent.Set> { intent, _ ->
- dispatch(Msg.Value(value = intent.value))
- }
- },
- reducer = { msg ->
- when (msg) {
- is Msg.Value -> copy(value = msg.value)
- }
- },
- ) {}
- private sealed interface Msg {
- data class Value(val value: String) : Msg
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement