-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move key changes into FormulaRuntime. #333
Merged
+102
−80
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -15,11 +15,15 @@ class FormulaRuntime<Input : Any, Output : Any>( | |
private val onOutput: (Output) -> Unit, | ||
private val onError: (Throwable) -> Unit, | ||
private val isValidationEnabled: Boolean = false, | ||
private val inspector: Inspector? = null, | ||
inspector: Inspector? = null, | ||
) : ManagerDelegate { | ||
private val implementation = formula.implementation() | ||
private var manager: FormulaManagerImpl<Input, *, Output>? = null | ||
private var hasInitialFinished = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed anymore - previously it was used to ensure that on formula start, we wait for all actions to start running before emitting a value. Currently, it's done within |
||
private val inspector = FormulaPlugins.inspector( | ||
type = formula.type(), | ||
local = inspector, | ||
) | ||
|
||
private var emitOutput = false | ||
private var lastOutput: Output? = null | ||
|
||
|
@@ -50,34 +54,62 @@ class FormulaRuntime<Input : Any, Output : Any>( | |
*/ | ||
private var isExecutingEffects: Boolean = false | ||
|
||
fun isKeyValid(input: Input): Boolean { | ||
/** | ||
* This is a global termination flag that indicates that upstream has disposed of the | ||
* this [FormulaRuntime] instance. We will not accept any more [onInput] changes and will | ||
* not emit any new [Output] events. | ||
*/ | ||
private var isRuntimeTerminated: Boolean = false | ||
|
||
private fun isKeyValid(input: Input): Boolean { | ||
return this.input == null || key == formula.key(input) | ||
} | ||
|
||
fun onInput(input: Input) { | ||
val initialization = this.input == null | ||
threadChecker.check("Input arrived on a wrong thread.") | ||
|
||
if (isRuntimeTerminated) return | ||
|
||
val isKeyValid = isKeyValid(input) | ||
|
||
this.input = input | ||
this.key = formula.key(input) | ||
|
||
if (initialization) { | ||
manager = FormulaManagerImpl( | ||
delegate = this, | ||
formula = implementation, | ||
initialInput = input, | ||
loggingType = formula::class, | ||
inspector = inspector, | ||
) | ||
run() | ||
val current = manager | ||
if (current == null) { | ||
// First input arrived, need to start a formula manager | ||
startNewManager(input) | ||
} else if (!isKeyValid) { | ||
// Formula key changed, need to reset the formula state. We mark old manager as | ||
// terminated, will perform termination effects and then start a new manager. | ||
current.markAsTerminated() | ||
|
||
// Input changed, increment the id | ||
inputId += 1 | ||
|
||
if (isRunning) { | ||
// Since we are already running, we let that function to take over. | ||
// No need to do anything more here | ||
} else { | ||
// Let's first execute side-effects | ||
current.performTerminationSideEffects() | ||
|
||
hasInitialFinished = true | ||
emitOutputIfNeeded(isInitialRun = true) | ||
// Start new manager | ||
startNewManager(input) | ||
} | ||
} else { | ||
// Input changed, need to re-run | ||
inputId += 1 | ||
run() | ||
} | ||
} | ||
|
||
fun terminate() { | ||
threadChecker.check("Need to unsubscribe on the main thread.") | ||
|
||
if (isRuntimeTerminated) return | ||
isRuntimeTerminated = true | ||
|
||
manager?.apply { | ||
markAsTerminated() | ||
|
||
|
@@ -89,7 +121,7 @@ class FormulaRuntime<Input : Any, Output : Any>( | |
* This way, we let runFormula() exit out before we terminate everything. | ||
*/ | ||
if (!isRunning) { | ||
performTermination() | ||
terminateManager(this) | ||
} | ||
} | ||
} | ||
|
@@ -135,7 +167,18 @@ class FormulaRuntime<Input : Any, Output : Any>( | |
*/ | ||
if (manager.isTerminated()) { | ||
shouldRun = false | ||
performTermination() | ||
terminateManager(manager) | ||
|
||
// If runtime has been terminated, we are stopping and do | ||
// not need to do anything else. | ||
if (!isRuntimeTerminated) { | ||
// Terminated manager with input change indicates that formula | ||
// key changed and we are resetting formula state. We need to | ||
// start a new formula manager. | ||
if (localInputId != inputId) { | ||
input?.let(this::startNewManager) | ||
} | ||
} | ||
} else { | ||
shouldRun = localInputId != inputId | ||
} | ||
|
@@ -149,15 +192,14 @@ class FormulaRuntime<Input : Any, Output : Any>( | |
executeTransitionEffects() | ||
|
||
if (!manager.isTerminated()) { | ||
emitOutputIfNeeded(isInitialRun = false) | ||
emitOutputIfNeeded() | ||
} | ||
} catch (e: Throwable) { | ||
isRunning = false | ||
|
||
manager?.markAsTerminated() | ||
onError(e) | ||
|
||
performTermination() | ||
manager?.let(this::terminateManager) | ||
} | ||
} | ||
|
||
|
@@ -202,22 +244,38 @@ class FormulaRuntime<Input : Any, Output : Any>( | |
/** | ||
* Emits output to the formula subscriber. | ||
*/ | ||
private fun emitOutputIfNeeded(isInitialRun: Boolean) { | ||
if (isInitialRun) { | ||
lastOutput?.let(onOutput) | ||
} else if (hasInitialFinished && emitOutput) { | ||
private fun emitOutputIfNeeded() { | ||
if (emitOutput && !isRuntimeTerminated) { | ||
emitOutput = false | ||
onOutput(checkNotNull(lastOutput)) | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new formula manager and runs it. | ||
*/ | ||
private fun startNewManager(initialInput: Input) { | ||
manager = initManager(initialInput) | ||
run() | ||
} | ||
|
||
/** | ||
* Performs formula termination effects and executes transition effects if needed. | ||
*/ | ||
private fun performTermination() { | ||
manager?.performTerminationSideEffects() | ||
private fun terminateManager(manager: FormulaManager<Input, Output>) { | ||
manager.performTerminationSideEffects() | ||
if (!isExecutingEffects) { | ||
executeTransitionEffects() | ||
} | ||
} | ||
|
||
private fun initManager(initialInput: Input): FormulaManagerImpl<Input, *, Output> { | ||
return FormulaManagerImpl( | ||
delegate = this, | ||
formula = implementation, | ||
initialInput = initialInput, | ||
loggingType = formula::class, | ||
inspector = inspector, | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was a bit weird that we recreate the runtime if key changes. Instead, the runtime will handle state reset internally.