-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create ChildrenManager. * Fix rebase issue.
- Loading branch information
Showing
2 changed files
with
116 additions
and
64 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
formula/src/main/java/com/instacart/formula/internal/ChildrenManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.instacart.formula.internal | ||
|
||
import com.instacart.formula.IFormula | ||
|
||
/** | ||
* Keeps track of child formula managers. | ||
*/ | ||
internal class ChildrenManager( | ||
private val childTransitionListener: TransitionListener, | ||
) { | ||
private var children: SingleRequestMap<Any, FormulaManager<*, *>>? = null | ||
private var pendingRemoval: MutableList<FormulaManager<*, *>>? = null | ||
|
||
fun updateTransitionId(transitionId: TransitionId) { | ||
children?.forEachValue { it.updateTransitionId(transitionId) } | ||
} | ||
|
||
fun evaluationFinished() { | ||
children?.clearUnrequested { | ||
pendingRemoval = pendingRemoval ?: mutableListOf() | ||
it.markAsTerminated() | ||
pendingRemoval?.add(it) | ||
} | ||
} | ||
|
||
fun terminateDetachedChildren(transitionId: TransitionId): Boolean { | ||
val local = pendingRemoval | ||
pendingRemoval = null | ||
local?.forEach { it.performTerminationSideEffects() } | ||
if (transitionId.hasTransitioned()) { | ||
return true | ||
} | ||
|
||
return children?.any { it.value.value.terminateDetachedChildren(transitionId) } ?: false | ||
} | ||
|
||
fun terminateOldUpdates(transitionId: TransitionId): Boolean { | ||
children?.forEachValue { | ||
if (it.terminateOldUpdates(transitionId)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
fun startNewUpdates(transitionId: TransitionId): Boolean { | ||
children?.forEachValue { | ||
if (it.startNewUpdates(transitionId)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
fun markAsTerminated() { | ||
children?.forEachValue { it.markAsTerminated() } | ||
} | ||
|
||
fun performTerminationSideEffects() { | ||
children?.forEachValue { it.performTerminationSideEffects() } | ||
} | ||
|
||
fun <ChildInput, ChildOutput> findOrInitChild( | ||
key: Any, | ||
formula: IFormula<ChildInput, ChildOutput>, | ||
input: ChildInput, | ||
): FormulaManager<ChildInput, ChildOutput> { | ||
@Suppress("UNCHECKED_CAST") | ||
val children = children ?: run { | ||
val initialized: SingleRequestMap<Any, FormulaManager<*, *>> = LinkedHashMap() | ||
this.children = initialized | ||
initialized | ||
} | ||
|
||
return children | ||
.findOrInit(key) { | ||
val implementation = formula.implementation() | ||
FormulaManagerImpl(implementation, input, childTransitionListener) | ||
} | ||
.requestAccess { | ||
throw IllegalStateException("There already is a child with same key: $key. Override [Formula.key] function.") | ||
} as FormulaManager<ChildInput, ChildOutput> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters