Skip to content

Commit

Permalink
Some renaming within FormulaContext.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Sep 13, 2021
1 parent a8d45b1 commit 70d3553
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
7 changes: 6 additions & 1 deletion formula/src/main/java/com/instacart/formula/Evaluation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ data class Evaluation<out Output>(
) {

companion object {
@Deprecated("Replace `updates` with `actions`.")
@Deprecated(
"Replace `updates` with `actions`.", ReplaceWith(
"Evaluation(output = output, actions = updates)",
"com.instacart.formula.Evaluation"
)
)
operator fun <Output> invoke(output: Output, updates: List<BoundAction<*>>): Evaluation<Output> {
return Evaluation(
output = output,
Expand Down
58 changes: 33 additions & 25 deletions formula/src/main/java/com/instacart/formula/FormulaContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ abstract class FormulaContext<State> internal constructor(
/**
* Provides an [UpdateBuilder] that enables [Formula] to declare various events and effects.
*/
abstract fun updates(init: UpdateBuilder<State>.() -> Unit): List<BoundAction<*>>
fun updates(init: UpdateBuilder<State>.() -> Unit): List<BoundAction<*>> = actions(init)

/**
* Provides an [UpdateBuilder] that enables [Formula] to declare various events and effects.
*/
abstract fun actions(init: UpdateBuilder<State>.() -> Unit): List<BoundAction<*>>

/**
* Scopes [create] block with a [key].
Expand All @@ -121,65 +126,68 @@ abstract class FormulaContext<State> internal constructor(
class UpdateBuilder<State>(
@PublishedApi internal val transitionCallback: (Transition<State>) -> Unit
) {
internal val updates = mutableListOf<BoundAction<*>>()
internal val actions = mutableListOf<BoundAction<*>>()

/**
* Adds a [Stream] as part of this [Evaluation]. [Stream] will be subscribed when it is initially added
* and unsubscribed when it is not returned as part of [Evaluation].
* Adds a [DisposableAction] as part of this [Evaluation]. [DisposableAction] will be
* subscribed when it is initially added and unsubscribed when it is not
* returned as part of [Evaluation].
*
* @param transition Callback invoked when [Stream] sends us a [Message].
* @param transition Callback invoked when [DisposableAction] sends us a [Message].
*/
inline fun <Message> events(
stream: Stream<Message>,
action: DisposableAction<Message>,
crossinline transition: Transition.Factory.(Message) -> Transition<State>
) {
add(createConnection(stream, transition))
add(toBoundAction(action, transition))
}

/**
* Adds a [Stream] as part of this [Evaluation]. [Stream] will be subscribed when it is initially added
* and unsubscribed when it is not returned as part of [Evaluation].
* Adds a [DisposableAction] as part of this [Evaluation]. [DisposableAction] will be
* subscribed when it is initially added and unsubscribed when it is not returned
* as part of [Evaluation].
*
* @param transition Callback invoked when [Stream] sends us a [Message].
* @param transition Callback invoked when [DisposableAction] sends us a [Message].
*/
inline fun <Message> onEvent(
stream: Stream<Message>,
action: DisposableAction<Message>,
avoidParameterClash: Any = this,
crossinline transition: Transition.Factory.(Message) -> Transition<State>
) {
add(createConnection(stream, transition))
add(toBoundAction(action, transition))
}

/**
* Adds a [Stream] as part of this [Evaluation]. [Stream] will be subscribed when it is initially added
* and unsubscribed when it is not returned as part of [Evaluation].
* Adds a [DisposableAction] as part of this [Evaluation]. [DisposableAction] will be
* subscribed when it is initially added and unsubscribed when it is not returned
* as part of [Evaluation].
*
* @param transition Callback invoked when [Stream] sends us a [Message].
* @param transition Callback invoked when [DisposableAction] sends us a [Message].
*
* Example:
* ```
* Stream.onInit().onEvent {
* DisposableAction.onInit().onEvent {
* transition { /* */ }
* }
* ```
*/
inline fun <Message> Stream<Message>.onEvent(
inline fun <Message> DisposableAction<Message>.onEvent(
crossinline transition: Transition.Factory.(Message) -> Transition<State>
) {
val stream = this
this@UpdateBuilder.events(stream, transition)
}

@PublishedApi internal fun add(connection: BoundAction<*>) {
if (updates.contains(connection)) {
throw IllegalStateException("duplicate stream with key: ${connection.keyAsString()}")
@PublishedApi internal fun add(action: BoundAction<*>) {
if (actions.contains(action)) {
throw IllegalStateException("duplicate stream with key: ${action.keyAsString()}")
}

updates.add(connection)
actions.add(action)
}

@PublishedApi internal inline fun <Message> createConnection(
stream: Stream<Message>,
@PublishedApi internal inline fun <Message> toBoundAction(
action: DisposableAction<Message>,
crossinline transition: Transition.Factory.(Message) -> Transition<State>
): BoundAction<Message> {
val callback: (Message) -> Unit = {
Expand All @@ -188,8 +196,8 @@ abstract class FormulaContext<State> internal constructor(
}

return BoundAction(
key = JoinedKey(stream.key(), callback::class),
stream = stream,
key = JoinedKey(action.key(), callback::class),
stream = action,
listener = callback
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class FormulaContextImpl<State> internal constructor(
transitionCallback.invoke(transition)
}

override fun updates(init: UpdateBuilder<State>.() -> Unit): List<BoundAction<*>> {
override fun actions(init: UpdateBuilder<State>.() -> Unit): List<BoundAction<*>> {
ensureNotRunning()
val builder = UpdateBuilder(transitionCallback)
builder.init()
return builder.updates
return builder.actions
}

override fun <ChildInput, ChildOutput> child(
Expand Down

0 comments on commit 70d3553

Please sign in to comment.