-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
leave only Dsl way to write state reducer
- Loading branch information
Dmitriy Berdnikov
committed
Feb 19, 2024
1 parent
fec3f6f
commit 4553dcd
Showing
12 changed files
with
175 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
elmslie-core/src/main/kotlin/money/vivid/elmslie/core/store/ScreenReducer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package money.vivid.elmslie.core.store | ||
|
||
import kotlin.reflect.KClass | ||
|
||
abstract class ScreenReducer<Event : Any, Ui : Any, Internal : Any, State : Any, Effect : Any, Command : Any>( | ||
private val uiEventClass: KClass<Ui>, | ||
private val internalEventClass: KClass<Internal> | ||
) : StateReducer<Event, State, Effect, Command>() { | ||
|
||
|
||
protected abstract fun Result.ui(event: Ui): Any? | ||
|
||
protected abstract fun Result.internal(event: Internal): Any? | ||
|
||
override fun Result.reduce(event: Event) { | ||
@Suppress("UNCHECKED_CAST") | ||
when { | ||
uiEventClass.isInstance(event) -> ui(event as Ui) | ||
internalEventClass.isInstance(event) -> internal(event as Internal) | ||
else -> error("Event ${event::class} is neither UI nor Internal") | ||
} | ||
} | ||
} |
11 changes: 9 additions & 2 deletions
11
elmslie-core/src/main/kotlin/money/vivid/elmslie/core/store/StateReducer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
package money.vivid.elmslie.core.store | ||
|
||
fun interface StateReducer<Event : Any, State : Any, Effect : Any, Command : Any> { | ||
import money.vivid.elmslie.core.store.dsl.ResultBuilder | ||
|
||
fun reduce(event: Event, state: State): Result<State, Effect, Command> | ||
abstract class StateReducer<Event : Any, State : Any, Effect : Any, Command : Any> { | ||
|
||
// Needed to type less code | ||
protected inner class Result(state: State) : ResultBuilder<State, Effect, Command>(state) | ||
|
||
protected abstract fun Result.reduce(event: Event) | ||
|
||
fun reduce(event: Event, state: State) = Result(state).apply { reduce(event) }.build() | ||
} |
14 changes: 0 additions & 14 deletions
14
elmslie-core/src/main/kotlin/money/vivid/elmslie/core/store/dsl/DslReducer.kt
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
elmslie-core/src/main/kotlin/money/vivid/elmslie/core/store/dsl/ScreenDslReducer.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
elmslie-core/src/test/kotlin/money/vivid/elmslie/core/store/dsl/DslReducerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.