From a962cde97306773bb69dc4f43dafbef4945cc7b0 Mon Sep 17 00:00:00 2001 From: Eugene Komarov Date: Tue, 12 Sep 2023 12:40:36 +0200 Subject: [PATCH] add state param to store listener methods --- .../java/vivid/money/elmslie/core/store/ElmStore.kt | 12 +++++++----- .../vivid/money/elmslie/core/store/StoreListener.kt | 10 +++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/elmslie-core/src/main/java/vivid/money/elmslie/core/store/ElmStore.kt b/elmslie-core/src/main/java/vivid/money/elmslie/core/store/ElmStore.kt index b221c740..778a55be 100644 --- a/elmslie-core/src/main/java/vivid/money/elmslie/core/store/ElmStore.kt +++ b/elmslie-core/src/main/java/vivid/money/elmslie/core/store/ElmStore.kt @@ -65,7 +65,7 @@ class ElmStore( 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, @@ -76,7 +76,9 @@ class ElmStore( 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) } @@ -95,7 +97,7 @@ class ElmStore( } 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, @@ -105,7 +107,7 @@ class ElmStore( 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, @@ -117,7 +119,7 @@ class ElmStore( } .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, diff --git a/elmslie-core/src/main/java/vivid/money/elmslie/core/store/StoreListener.kt b/elmslie-core/src/main/java/vivid/money/elmslie/core/store/StoreListener.kt index 3b15a65c..43e56499 100644 --- a/elmslie-core/src/main/java/vivid/money/elmslie/core/store/StoreListener.kt +++ b/elmslie-core/src/main/java/vivid/money/elmslie/core/store/StoreListener.kt @@ -2,11 +2,11 @@ package vivid.money.elmslie.core.store interface StoreListener { - 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) {} }