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

Add SuspendAction to create an action from a suspend function #408

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.instacart.formula.coroutines

import com.instacart.formula.Action
import com.instacart.formula.Cancelable
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* Adapter which allows creating Formula [Action] from a Kotlin suspend function. Take a
* look at [SuspendAction.from] to create an [Action] from [suspend] type.
*/
interface SuspendAction<Event> : Action<Event> {

companion object {
/**
* Creates an [Action] which will launch a [suspend] created by factory function [create]
*
* ```
* SuspendAction.from { locationManager.currentLocation() }.onEvent { event ->
* transition()
* }
* ```
*/
fun <Event> from(
create: suspend () -> Event
): SuspendAction<Event> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it should be Result<Error> to ensure that we emit errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you mean Result<Event>, but shouldn't exception handling be up to the consumer? The consumer can handle wrapping their function in a try/catch and return a Result if they would like, but I'm not sure requiring that should be necessary. How do FlowAction and RxAction handle errors? It looks to me like unhandled errors in the stream would result in a crash.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's a good callout, I need to think about it more globally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like having an api (onError()) on ActionBuilder might be a good idea.

return SuspendActionImpl(null, create)
}

/**
* Creates an [Action] which will launch a [suspend] created by factory function [create].
*
* ```
* SuspendAction.from(itemId) { repo.fetchItem(itemId) }.onEvent { event ->
* transition()
* }
* ```
*
* @param key Used to distinguish this [Action] from other actions.
*/
fun <Event> from(
key: Any?,
create: suspend () -> Event
): SuspendAction<Event> {
return SuspendActionImpl(key, create)
}
}

suspend fun execute(): Event

@OptIn(DelicateCoroutinesApi::class)
override fun start(send: (Event) -> Unit): Cancelable? {
val job = GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) {
withContext(Dispatchers.Unconfined) {
val event = execute()
send(event)
}
}
return Cancelable(job::cancel)
}
}

private data class SuspendActionImpl<Event>(
private val key: Any?,
private val factory: suspend () -> Event
) : SuspendAction<Event> {

override suspend fun execute(): Event = factory()

override fun key(): Any? = key
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.instacart.formula.coroutines

import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.delay
import org.junit.Test

class SuspendActionTest {

@Test
fun `default key is null`() {
val action = SuspendAction.from { delay(100) }
assertThat(action.key()).isNull()
}

@Test
fun `specified key`() {
val action = SuspendAction.from("unique-key") { delay(100) }
assertThat(action.key()).isEqualTo("unique-key")
}
}
Loading