Skip to content

Commit

Permalink
[Hygiene] Use Action.onEvent syntax. (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux authored Mar 5, 2024
1 parent 14e216c commit 203a391
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class FragmentFlowStore @PublishedApi internal constructor(
return Evaluation(
output = state,
actions = context.actions {
events(lifecycleEventStream) { event ->
lifecycleEventStream.onEvent { event ->
val fragmentId = event.fragmentId
when (event) {
is FragmentLifecycleEvent.Removed -> {
Expand Down Expand Up @@ -106,7 +106,7 @@ class FragmentFlowStore @PublishedApi internal constructor(
}
}

events(visibleContractEventStream) {
visibleContractEventStream.onEvent {
if (state.visibleIds.contains(it)) {
// TODO: should we log this duplicate visibility event?
none()
Expand All @@ -115,7 +115,7 @@ class FragmentFlowStore @PublishedApi internal constructor(
}
}

events(hiddenContractEventStream) {
hiddenContractEventStream.onEvent {
transition(state.copy(visibleIds = state.visibleIds.minus(it)))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal class CompositeBinding<ParentComponent, ScopedComponent>(
output = Unit,
actions = context.actions {
val isInScope = input.activeFragments.any { binds(it.key) }
events(Action.onData(isInScope)) {
Action.onData(isInScope).onEvent {
if (isInScope && component == null) {
transition(State(component = scopeFactory.invoke(input.component)))
} else if (!isInScope && component != null) {
Expand All @@ -60,7 +60,7 @@ internal class CompositeBinding<ParentComponent, ScopedComponent>(
}
}

events(Action.onTerminate()) {
Action.onTerminate().onEvent {
transition { component?.dispose() }
}
}
Expand Down
13 changes: 0 additions & 13 deletions formula/src/main/java/com/instacart/formula/ActionBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ abstract class ActionBuilder<out Input, State>(
transition: Transition<Input, State, Event>,
)

/**
* Adds an [Action] as part of this [Evaluation]. [Action] will be initialized
* when it is initially added and cleaned up when it is not returned
* as part of [Evaluation].
*
* @param transition A function that is invoked when [Action] emits an [Event].
*/
abstract fun <Event> onEvent(
action: Action<Event>,
avoidParameterClash: Any = this,
transition: Transition<Input, State, Event>,
)

/**
* Adds an [Action] as part of this [Evaluation]. [Action] will be initialized
* when it is initially added and cleaned up when it is not returned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,13 @@ internal class ActionBuilderImpl<out Input, State> internal constructor(
action: Action<Event>,
transition: Transition<Input, State, Event>,
) {
add(toBoundStream(action, transition))
}

override fun <Event> onEvent(
action: Action<Event>,
avoidParameterClash: Any,
transition: Transition<Input, State, Event>,
) {
add(toBoundStream(action, transition))
actions.add(toBoundStream(action, transition))
}

override fun <Event> Action<Event>.onEvent(
transition: Transition<Input, State, Event>,
) {
val stream = this
this@ActionBuilderImpl.events(stream, transition)
}

private fun add(action: DeferredAction<*>) {
if (actions.contains(action)) {
throw IllegalStateException("duplicate stream with key: ${action.keyAsString()}")
}

actions.add(action)
events(this, transition)
}

private fun <Event> toBoundStream(
Expand Down
26 changes: 13 additions & 13 deletions formula/src/test/java/com/instacart/formula/FormulaRuntimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,11 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
@Test
fun `same stream declarations are okay`() {
val formula = OnlyUpdateFormula<Unit> {
events(EmptyAction.init()) {
EmptyAction.init().onEvent {
transition(Unit)
}

events(EmptyAction.init()) {
EmptyAction.init().onEvent {
transition(Unit)
}
}
Expand All @@ -901,11 +901,11 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
@Test
fun `same observable declarations are okay`() {
val formula = OnlyUpdateFormula<Unit> {
events(RxAction.fromObservable("same") { Observable.just(1) }) {
RxAction.fromObservable("same") { Observable.just(1) }.onEvent {
none()
}

events(RxAction.fromObservable("same") { Observable.just(1) }) {
RxAction.fromObservable("same") { Observable.just(1) }.onEvent {
none()
}
}
Expand All @@ -918,7 +918,7 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
val formula = OnlyUpdateFormula<Unit> {
val list = listOf(1, 2, 3)
list.forEach {
events(EmptyAction.init()) {
EmptyAction.init().onEvent {
none()
}
}
Expand All @@ -933,7 +933,7 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
val formula = OnlyUpdateFormula<Unit> {
val list = listOf(1, 2, 3)
list.forEach {
events(EmptyAction.init(it)) {
EmptyAction.init(it).onEvent {
none()
}
}
Expand All @@ -946,13 +946,13 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
fun `multiple event streams without key`() {
var executed = 0
val formula = OnlyUpdateFormula<Unit> {
events(Action.onInit()) {
Action.onInit().onEvent {
transition {
executed += 1
}
}

events(Action.onInit()) {
Action.onInit().onEvent {
transition {
executed += 1
}
Expand All @@ -968,11 +968,11 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
fun `multiple events with input and without key`() {
var executed = 0
val formula = OnlyUpdateFormula<Int> {
events(Action.onData(it)) {
Action.onData(it).onEvent {
transition { executed += 1 }
}

events(Action.onData(it)) {
Action.onData(it).onEvent {
transition { executed += 1 }
}
}
Expand All @@ -987,7 +987,7 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
val formula = OnlyUpdateFormula<Unit> {
val list = listOf(0, 1, 2)
list.forEach {
events(Action.onInit()) {
Action.onInit().onEvent {
none()
}
}
Expand Down Expand Up @@ -1041,7 +1041,7 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
var emissions = 0
var terminateCallback = -1
val formula = OnlyUpdateFormula<Int> { input ->
events(Action.onTerminate()) {
Action.onTerminate().onEvent {
transition {
emissions += 1
terminateCallback = input
Expand Down Expand Up @@ -1204,7 +1204,7 @@ class FormulaRuntimeTest(val runtime: TestableRuntime, val name: String) {
@Test
fun `emit error`() {
val formula = OnlyUpdateFormula<Unit> {
events(Action.onInit()) {
Action.onInit().onEvent {
throw java.lang.IllegalStateException("crashed")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DynamicStreamSubject(runtime: TestableRuntime) {
output = Unit,
actions = context.actions {
input.forEach { key ->
events(action(key)) {
action(key).onEvent {
none()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EventFormula<EventT : Any>(
return Evaluation(
output = state,
actions = context.actions {
events(stream) {
stream.onEvent {
val newState = state + 1
transition(newState) {
captured(Event(newState, it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object ExtremelyNestedFormula {
return Evaluation(
output = state + childValue,
actions = context.actions {
events(Action.onInit()) {
Action.onInit().onEvent {
transition(state + 1)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FromObservableWithInputFormula : StatelessFormula<FromObservableWithInputF
val fetchItem = RxAction.fromObservable(key = input.itemId) {
repo.fetchItem(input.itemId)
}
events(fetchItem) {
fetchItem.onEvent() {
transition { input.onItem(it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RemovingTerminateStreamSendsNoMessagesFormula : StatelessFormula<RemovingT
actions = context.actions {
val onTerminate = input.onTerminate
if (onTerminate != null) {
events(Action.onTerminate()) {
Action.onTerminate().onEvent {
transition(onTerminate)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class StartStopFormula(runtime: TestableRuntime) : Formula<Unit, State, Output>(
return Evaluation(
actions = context.actions {
if (state.listenForEvents) {
events(incrementEvents.action()) {
incrementEvents.action().onEvent {
transition(state.copy(count = state.count + 1))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object SubscribesToAllUpdatesBeforeDeliveringMessages {
return Evaluation(
output = state,
actions = context.actions {
events(initial) {
initial.onEvent {
transition { incrementRelay.triggerEvent() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NetworkStateFormula(
status = "Network state: $isConnected"
),
actions = context.actions {
events(networkStateStream) {
networkStateStream.onEvent {
transition(state.copy(isOnline = it.isOnline))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StopwatchFormula : Formula<Unit, StopwatchFormula.State, StopwatchRenderMo
Observable.interval(1, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
}

events(incrementTimePassed) {
incrementTimePassed.onEvent {
transition(state.copy(timePassedInMillis = state.timePassedInMillis + 1))
}
}
Expand Down

0 comments on commit 203a391

Please sign in to comment.