diff --git a/formula/src/main/java/com/instacart/formula/Inspector.kt b/formula/src/main/java/com/instacart/formula/Inspector.kt index dcf20e43..5027a7aa 100644 --- a/formula/src/main/java/com/instacart/formula/Inspector.kt +++ b/formula/src/main/java/com/instacart/formula/Inspector.kt @@ -65,10 +65,11 @@ interface Inspector { * Called when a state change happens * * @param formulaType Formula type used to filter for specific events. + * @param event Event that triggered this state change * @param old Previous state value * @param new New state value */ - fun onStateChanged(formulaType: KClass<*>, old: Any?, new: Any?) = Unit + fun onStateChanged(formulaType: KClass<*>, event: Any?, old: Any?, new: Any?) = Unit /** * Called when [FormulaRuntime.run] is called. This method in combination with [onRunFinished] diff --git a/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt b/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt index c880738b..f53433a3 100644 --- a/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt +++ b/formula/src/main/java/com/instacart/formula/internal/FormulaManagerImpl.kt @@ -78,7 +78,7 @@ internal class FormulaManagerImpl( return terminated } - fun handleTransitionResult(result: Transition.Result) { + fun handleTransitionResult(event: Any?, result: Transition.Result) { val effects = result.effects if (terminated) { // State transitions are ignored, let's just execute side-effects. @@ -93,7 +93,12 @@ internal class FormulaManagerImpl( globalEvaluationId += 1 - inspector?.onStateChanged(loggingType, old, result.state) + inspector?.onStateChanged( + formulaType = loggingType, + event = event, + old = old, + new = result.state, + ) } } @@ -180,7 +185,13 @@ internal class FormulaManagerImpl( } } - val snapshot = SnapshotImpl(input, state, evaluationId, listeners, this) + val snapshot = SnapshotImpl( + input = input, + state = state, + associatedEvaluationId = evaluationId, + listeners = listeners, + delegate = this, + ) val result = formula.evaluate(snapshot) if (isValidationEnabled) { diff --git a/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt b/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt index cedc9ae6..b72370e2 100644 --- a/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt +++ b/formula/src/main/java/com/instacart/formula/internal/ListInspector.kt @@ -35,8 +35,8 @@ internal class ListInspector( forEachInspector { onActionFinished(formulaType, action) } } - override fun onStateChanged(formulaType: KClass<*>, old: Any?, new: Any?) { - forEachInspector { onStateChanged(formulaType, old, new) } + override fun onStateChanged(formulaType: KClass<*>, event: Any?, old: Any?, new: Any?) { + forEachInspector { onStateChanged(formulaType, event, old, new) } } override fun onRunStarted(evaluate: Boolean) { diff --git a/formula/src/main/java/com/instacart/formula/internal/SnapshotImpl.kt b/formula/src/main/java/com/instacart/formula/internal/SnapshotImpl.kt index 01cefbd4..a8fa7985 100644 --- a/formula/src/main/java/com/instacart/formula/internal/SnapshotImpl.kt +++ b/formula/src/main/java/com/instacart/formula/internal/SnapshotImpl.kt @@ -102,7 +102,7 @@ internal class SnapshotImpl internal constructor( return } - delegate.handleTransitionResult(result) + delegate.handleTransitionResult(event, result) } private fun ensureNotRunning() { diff --git a/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt b/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt index 4d2cfffc..2af104ec 100644 --- a/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt +++ b/formula/src/test/java/com/instacart/formula/internal/TestInspector.kt @@ -36,7 +36,7 @@ class TestInspector : Inspector { events.add("action-finished: ${formulaType.qualifiedName}") } - override fun onStateChanged(formulaType: KClass<*>, old: Any?, new: Any?) { + override fun onStateChanged(formulaType: KClass<*>, event: Any?, old: Any?, new: Any?) { events.add("state-changed: ${formulaType.qualifiedName}") }