-
Notifications
You must be signed in to change notification settings - Fork 264
[WIP] Introduce Arrow Fx #821
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,20 @@ | ||
| package io.github.droidkaigi.confsched2019.session.ui.actioncreator | ||
|
|
||
| import androidx.lifecycle.Lifecycle | ||
| import arrow.Kind | ||
| import arrow.core.Either | ||
| import arrow.core.getOrHandle | ||
| import arrow.core.left | ||
| import arrow.core.right | ||
| import arrow.core.success | ||
takahirom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import arrow.effects.ForIO | ||
| import arrow.effects.IO | ||
| import arrow.effects.extensions.io.async.async | ||
| import arrow.effects.extensions.io.effect.runAsync | ||
takahirom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import arrow.effects.extensions.io.fx.fx | ||
| import arrow.effects.extensions.io.unsafeRun.runNonBlocking | ||
takahirom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import arrow.effects.fix | ||
| import arrow.unsafe | ||
takahirom marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import io.github.droidkaigi.confsched2019.action.Action | ||
| import io.github.droidkaigi.confsched2019.data.repository.SessionRepository | ||
| import io.github.droidkaigi.confsched2019.di.PageScope | ||
|
|
@@ -13,6 +27,7 @@ import io.github.droidkaigi.confsched2019.session.R | |
| import io.github.droidkaigi.confsched2019.system.actioncreator.ErrorHandler | ||
| import io.github.droidkaigi.confsched2019.util.SessionAlarm | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.InternalCoroutinesApi | ||
| import kotlinx.coroutines.launch | ||
| import timber.log.Timber | ||
| import timber.log.debug | ||
|
|
@@ -66,27 +81,48 @@ class SessionContentsActionCreator @Inject constructor( | |
| } | ||
|
|
||
| fun toggleFavorite(session: Session) { | ||
| launch { | ||
| try { | ||
| fx { | ||
| !effect { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose to use fx? 🤔 If you want to just integrate with coroutine, then arrow supports coroutine extension with DefferedK maybe be like above.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Probably .k and DeferredK will be removed. 😇
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After diving into arrow fx, I found a cause. The current fx is for Try monad and it doesn't take care of asynchronous operations. In FP world, IO monad is what you need to use.
Then, follow the style below. |
||
| dispatcher.dispatchLoadingState(LoadingState.LOADING) | ||
| sessionRepository.toggleFavorite(session) | ||
| sessionAlarm.toggleRegister(session) | ||
| val sessionContents = sessionRepository.sessionContents() | ||
| dispatcher.dispatch(Action.SessionContentsLoaded(sessionContents)) | ||
| dispatcher.dispatchLoadingState(LoadingState.LOADED) | ||
| } catch (e: Exception) { | ||
| dispatcher.dispatch( | ||
| LoadingState.LOADED | ||
| } | ||
| }.runAndHold { result -> | ||
| val state = result.getOrHandle { | ||
| dispatcher.launchAndDispatch( | ||
| Action.ShowProcessingMessage( | ||
| Message.of( | ||
| R.string.session_favorite_connection_error | ||
| ) | ||
| ) | ||
| ) | ||
| LoadingState.INITIALIZED | ||
| } | ||
| dispatcher.launchAndDispatchLoadingState(state) | ||
| } | ||
| } | ||
|
|
||
| @UseExperimental(InternalCoroutinesApi::class) | ||
| private fun <A> Kind<ForIO, A>.runAndHold(result: (Either<Throwable, A>) -> Unit) { | ||
| IO.async().run { | ||
| defer(coroutineContext) { | ||
| this@runAndHold | ||
| } | ||
| }.fix().attempt().unsafeRunAsync { | ||
| result.invoke(it.getOrHandle { it.left() }) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun Dispatcher.dispatchLoadingState(loadingState: LoadingState) { | ||
| println("Fragment: Dispatch $loadingState") | ||
| dispatch(Action.SessionLoadingStateChanged(loadingState)) | ||
| } | ||
|
|
||
| private fun Dispatcher.launchAndDispatchLoadingState(loadingState: LoadingState) { | ||
| println("Fragment: Dispatch $loadingState") | ||
| launchAndDispatch(Action.SessionLoadingStateChanged(loadingState)) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.