Skip to content
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

Allow to specify default dispatcher for formula. #348

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

Laimiux
Copy link
Collaborator

@Laimiux Laimiux commented Mar 4, 2024

What

By default, we process formula updates on a thread that emits the event. This pull request adds functionality to specify a default dispatcher which enables us to globally move all processing to a background thread.

You can specify a default dispatcher at the plugin level which will be applied to all formulas:

val myPlugin = object : Plugin {
    override fun defaultDispatcher(): Dispatcher = Dispatcher.Background 
}
FormulaPlugins.setPlugin(myPlugin)

Alternatively, you can specify default dispatcher for a single formula via toObservable or toFlow:

val runtimeConfig = RuntimeConfig(defaultDispatcher = Dispatcher.Background) 
val observable = formula.toObservable(input, runtimeConfig)

Not part of this pull request

In a follow-up pull request, we will add ability to control dispatcher for individual transitions.

Within an individual formula, a developer can specify Transition.ExecutionType such as Transition.Immediate to override the default behavior.

ButtonOutput(
    onSelected = context.callback(Transition.Immediate) { 
        transition { navigateToScreen() }
    }
)

Alternatively, it can also specify Transition.Background execution type for certain events

RxAction.fromObservable {}.onEvent(Transition.Background) { event ->
    val items = event.items.map { computeSomething() }
    transition(items)
}

@carrotkite
Copy link

carrotkite commented Mar 4, 2024

3 Warnings
⚠️ No coverage data found for com/instacart/formula/Action
⚠️ No coverage data found for com/instacart/formula/plugin/Plugin
⚠️ No coverage data found for com/instacart/formula/plugin/Dispatcher

JaCoCo Code Coverage 80.55% ✅

Class Covered Meta Status
com/instacart/formula/coroutines/FlowRuntime 100% 0%
com/instacart/formula/internal/FormulaManagerImpl 87% 0%
com/instacart/formula/internal/ChildrenManager 75% 0%
com/instacart/formula/internal/ListenerImpl 75% 0%
com/instacart/formula/FormulaPlugins 80% 0%
com/instacart/formula/FormulaRuntime 78% 0%
com/instacart/formula/RuntimeConfig 80% 0%
com/instacart/formula/Effect 100% 0%
com/instacart/formula/Action No coverage data found : -% No coverage data found : -% 🃏
com/instacart/formula/test/RxJavaFormulaTestDelegate 90% 0%
com/instacart/formula/plugin/Plugin No coverage data found : -% No coverage data found : -% 🃏
com/instacart/formula/plugin/Dispatcher No coverage data found : -% No coverage data found : -% 🃏

Generated by 🚫 Danger

@Laimiux Laimiux force-pushed the laimonas/explore-default-dispatcher branch from 2769c43 to ae59bbf Compare March 4, 2024 22:02
@@ -31,10 +31,10 @@ object FormulaPlugins {
}

fun mainThreadDispatcher(): Dispatcher {
return plugin?.mainThreadDispatcher() ?: Dispatcher.NoOp
return plugin?.mainThreadDispatcher() ?: Dispatcher.None
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed from NoOp to None

@Laimiux Laimiux force-pushed the laimonas/explore-default-dispatcher branch from ae59bbf to 8dee4fb Compare March 4, 2024 22:05
Copy link

@jasonostrander jasonostrander left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Laimiux Laimiux force-pushed the laimonas/explore-default-dispatcher branch from 8dee4fb to 990134b Compare March 5, 2024 19:45
@Laimiux Laimiux marked this pull request as ready for review March 5, 2024 20:40
@Laimiux Laimiux merged commit 14e216c into master Mar 5, 2024
3 checks passed
@Laimiux Laimiux deleted the laimonas/explore-default-dispatcher branch March 5, 2024 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants