-
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
Add a new global plugin API. #311
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
21 changes: 21 additions & 0 deletions
21
formula/src/main/java/com/instacart/formula/FormulaPlugins.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,21 @@ | ||
package com.instacart.formula | ||
|
||
import com.instacart.formula.internal.ListInspector | ||
import kotlin.reflect.KClass | ||
|
||
object FormulaPlugins { | ||
private var plugin: Plugin? = null | ||
|
||
fun setPlugin(plugin: Plugin?) { | ||
this.plugin = plugin | ||
} | ||
|
||
fun inspector(type: KClass<*>, local: Inspector?): Inspector? { | ||
val global = plugin?.inspector(type) | ||
return when { | ||
global == null -> local | ||
local == null -> global | ||
else -> ListInspector(listOf(global, local)) | ||
} | ||
} | ||
} |
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,15 @@ | ||
package com.instacart.formula | ||
|
||
import kotlin.reflect.KClass | ||
|
||
interface Plugin { | ||
/** | ||
* A global callback to create [Inspector] for any formula. This will be called once when | ||
* formula is initially started. | ||
* | ||
* @param type Formula type. | ||
*/ | ||
fun inspector(type: KClass<*>): Inspector? { | ||
return null | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
formula/src/main/java/com/instacart/formula/internal/ListInspector.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,55 @@ | ||
package com.instacart.formula.internal | ||
|
||
import com.instacart.formula.DeferredAction | ||
import com.instacart.formula.Inspector | ||
import kotlin.reflect.KClass | ||
|
||
internal class ListInspector( | ||
private val inspectors: List<Inspector>, | ||
) : Inspector { | ||
override fun onFormulaStarted(formulaType: KClass<*>) { | ||
forEachInspector { onFormulaStarted(formulaType) } | ||
} | ||
|
||
override fun onFormulaFinished(formulaType: KClass<*>) { | ||
forEachInspector { onFormulaFinished(formulaType) } | ||
} | ||
|
||
override fun onEvaluateStarted(formulaType: KClass<*>, state: Any?) { | ||
forEachInspector { onEvaluateStarted(formulaType, state) } | ||
} | ||
|
||
override fun onInputChanged(formulaType: KClass<*>, prevInput: Any?, newInput: Any?) { | ||
forEachInspector { onInputChanged(formulaType, prevInput, newInput) } | ||
} | ||
|
||
override fun onEvaluateFinished(formulaType: KClass<*>, output: Any?, evaluated: Boolean) { | ||
forEachInspector { onEvaluateFinished(formulaType, output, evaluated) } | ||
} | ||
|
||
override fun onActionStarted(formulaType: KClass<*>, action: DeferredAction<*>) { | ||
forEachInspector { onActionStarted(formulaType, action) } | ||
} | ||
|
||
override fun onActionFinished(formulaType: KClass<*>, action: DeferredAction<*>) { | ||
forEachInspector { onActionFinished(formulaType, action) } | ||
} | ||
|
||
override fun onStateChanged(formulaType: KClass<*>, old: Any?, new: Any?) { | ||
forEachInspector { onStateChanged(formulaType, old, new) } | ||
} | ||
|
||
override fun onRunStarted(evaluate: Boolean) { | ||
forEachInspector { onRunStarted(evaluate) } | ||
} | ||
|
||
override fun onRunFinished() { | ||
forEachInspector { onRunFinished() } | ||
} | ||
|
||
private inline fun forEachInspector(callback: Inspector.() -> Unit) { | ||
for (inspector in inspectors) { | ||
inspector.callback() | ||
} | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
formula/src/test/java/com/instacart/formula/internal/ClearPluginsRule.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,21 @@ | ||
package com.instacart.formula.internal | ||
|
||
import com.instacart.formula.FormulaPlugins | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
class ClearPluginsRule : TestRule { | ||
override fun apply(base: Statement, description: Description): Statement { | ||
return object : Statement() { | ||
override fun evaluate() { | ||
FormulaPlugins.setPlugin(null) | ||
try { | ||
base.evaluate() | ||
} finally { | ||
FormulaPlugins.setPlugin(null) | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
nit: should we have test cases for any possible (global, local) permutation, not only when both are not null?
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.
We have some tests for local only, added a new test for global only.