Skip to content

Commit

Permalink
Merge pull request #245 from vivid-money/store-listener-improve
Browse files Browse the repository at this point in the history
Store listener improve
  • Loading branch information
opengamer29 authored Sep 12, 2023
2 parents e2aa608 + a962cde commit f532a0f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ElmStore<Event : Any, State : Any, Effect : Any, Command : Any>(
private fun dispatchEvent(event: Event) {
scope.launch {
try {
storeListeners.forEach { it.onEvent(key, event) }
storeListeners.forEach { it.onBeforeEvent(key, event, statesFlow.value) }
logger.debug(
message = "New event: $event",
tag = key,
Expand All @@ -76,7 +76,9 @@ class ElmStore<Event : Any, State : Any, Effect : Any, Command : Any>(
val result = reducer.reduce(event, statesFlow.value)
val newState = result.state
statesFlow.value = newState
storeListeners.forEach { it.onStateChanged(key, newState, oldState, event) }
storeListeners.forEach {
it.onAfterEvent(key, newState, oldState, event)
}
result
}
effects.forEach { effect -> if (isActive) dispatchEffect(effect) }
Expand All @@ -95,7 +97,7 @@ class ElmStore<Event : Any, State : Any, Effect : Any, Command : Any>(
}

private suspend fun dispatchEffect(effect: Effect) {
storeListeners.forEach { it.onEffect(key, effect) }
storeListeners.forEach { it.onEffect(key, effect, statesFlow.value) }
logger.debug(
message = "New effect: $effect",
tag = key,
Expand All @@ -105,7 +107,7 @@ class ElmStore<Event : Any, State : Any, Effect : Any, Command : Any>(

private fun executeCommand(command: Command) {
scope.launch {
storeListeners.forEach { it.onCommand(key, command) }
storeListeners.forEach { it.onCommand(key, command, statesFlow.value) }
logger.debug(
message = "Executing command: $command",
tag = key,
Expand All @@ -117,7 +119,7 @@ class ElmStore<Event : Any, State : Any, Effect : Any, Command : Any>(
}
.cancellable()
.catch { throwable ->
storeListeners.forEach { it.onCommandError(key, throwable, command) }
storeListeners.forEach { it.onActorError(key, throwable, command) }
logger.nonfatal(
message = "Unhandled exception inside the command $command",
tag = key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package vivid.money.elmslie.core.store

interface StoreListener<Event : Any, State : Any, Effect : Any, Command : Any> {

fun onEvent(key: String, event: Event) {}
fun onEffect(key: String, effect: Effect) {}
fun onCommand(key: String, command: Command) {}
fun onStateChanged(key: String, newState: State, oldState: State, eventCause: Event) {}
fun onBeforeEvent(key: String, event: Event, currentState: State) {}
fun onAfterEvent(key: String, newState: State, oldState: State, eventCause: Event) {}
fun onEffect(key: String, effect: Effect, state: State) {}
fun onCommand(key: String, command: Command, state: State) {}

fun onReducerError(key: String, throwable: Throwable, event: Event) {}
fun onCommandError(key: String, throwable: Throwable, command: Command) {}
fun onActorError(key: String, throwable: Throwable, command: Command) {}
}

0 comments on commit f532a0f

Please sign in to comment.