-
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.
* Add a new global plugin API. * Add test. * Feedback.
- Loading branch information
Showing
7 changed files
with
177 additions
and
25 deletions.
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) | ||
} | ||
} | ||
} | ||
} | ||
} |