Skip to content

Commit

Permalink
[Inspector] Expose event in onStateChanged event. (#316)
Browse files Browse the repository at this point in the history
* [Inspector] Expose event in onStateChanged event.

* Fix test compilation.
  • Loading branch information
Laimiux authored Dec 6, 2023
1 parent d014e18 commit 6374812
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CountingInspector : Inspector {
actionsStarted.add(formulaType.java)
}

override fun onStateChanged(formulaType: KClass<*>, old: Any?, new: Any?) {
override fun onStateChanged(formulaType: KClass<*>, event: Any?, old: Any?, new: Any?) {
stateTransitions.add(formulaType.java)
}

Expand Down
3 changes: 2 additions & 1 deletion formula/src/main/java/com/instacart/formula/Inspector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal class FormulaManagerImpl<Input, State, Output>(
return terminated
}

fun handleTransitionResult(result: Transition.Result<State>) {
fun handleTransitionResult(event: Any?, result: Transition.Result<State>) {
val effects = result.effects
if (terminated) {
// State transitions are ignored, let's just execute side-effects.
Expand All @@ -93,7 +93,12 @@ internal class FormulaManagerImpl<Input, State, Output>(

globalEvaluationId += 1

inspector?.onStateChanged(loggingType, old, result.state)
inspector?.onStateChanged(
formulaType = loggingType,
event = event,
old = old,
new = result.state,
)
}
}

Expand Down Expand Up @@ -180,7 +185,13 @@ internal class FormulaManagerImpl<Input, State, Output>(
}
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal class SnapshotImpl<out Input, State> internal constructor(
return
}

delegate.handleTransitionResult(result)
delegate.handleTransitionResult(event, result)
}

private fun ensureNotRunning() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}

Expand Down

0 comments on commit 6374812

Please sign in to comment.