Skip to content

Commit

Permalink
Implement termination without executing local transition queue and do…
Browse files Browse the repository at this point in the history
…cs update
  • Loading branch information
jhowens89 committed Jun 5, 2024
1 parent 15351c1 commit 8355fdb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
14 changes: 14 additions & 0 deletions formula/src/main/java/com/instacart/formula/FormulaContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ abstract class FormulaContext<out Input, State> internal constructor(
return child(formula, Unit)
}

/**
* A convenience method to run a formula that takes no input. Returns the latest output of
* the [child] formula or null if the child formula has encountered an exception. Child formulas
* that encounter exceptions will be be terminated and will not be run again. Formula runtime
* ensures the [formula] is running, manages its internal state and will trigger `evaluate`
* if needed.
*/
fun <ChildOutput> child(
formula: IFormula<Unit, ChildOutput>,
onError: (Throwable) -> Unit,
): ChildOutput? {
return child(formula, Unit, onError)
}

/**
* Returns the latest output of the [child] formula. Formula runtime ensures the [child]
* is running, manages its internal state and will trigger `evaluate` if needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ internal class ChildrenManager(
children?.forEachValue { it.markAsTerminated() }
}

fun performTerminationSideEffects() {
children?.forEachValue { it.performTerminationSideEffects() }
fun performTerminationSideEffects(executeTransitionQueue: Boolean) {
children?.forEachValue { it.performTerminationSideEffects(executeTransitionQueue) }
}

fun <ChildInput, ChildOutput> findOrInitChild(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ interface FormulaManager<Input, Output> {

/**
* Called when we are ready to perform termination side-effects.
* @param executeTransitionQueue whether the formula should execute remaining transitions
* that accumulated while it was running. False is passed in the case where the
* formula threw an exception and we want to avoid executubg transitions that have
* an elevated chance of also throwing an exception. Other termination side
* effects such as listener and action termination still happen.
*/
fun performTerminationSideEffects()
fun performTerminationSideEffects(executeTransitionQueue: Boolean = true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ internal class FormulaManagerImpl<Input, State, Output>(
manager.setValidationRun(isValidationEnabled)

return try {
// If manager.run(input) previously threw an exception it would be marked as
// terminated in the catch block. We avoid running it again because there is a decent
// chance it will continue to throw the same exception and cause performance issues
if (!manager.isTerminated()) {
manager.run(input).output
} else {
Expand All @@ -285,13 +288,17 @@ internal class FormulaManagerImpl<Input, State, Output>(
childrenManager?.markAsTerminated()
}

override fun performTerminationSideEffects() {
childrenManager?.performTerminationSideEffects()
override fun performTerminationSideEffects(executeTransitionQueue: Boolean) {
childrenManager?.performTerminationSideEffects(executeTransitionQueue)
actionManager.terminate()

// Execute deferred transitions
while (transitionQueue.isNotEmpty()) {
transitionQueue.pollFirst().execute()
if (executeTransitionQueue) {
// Execute deferred transitions
while (transitionQueue.isNotEmpty()) {
transitionQueue.pollFirst().execute()
}
} else {
transitionQueue.clear()
}

listeners.disableAll()
Expand Down

0 comments on commit 8355fdb

Please sign in to comment.