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

[WIP] Introduce Arrow Fx #821

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

[WIP] Introduce Arrow Fx #821

wants to merge 5 commits into from

Conversation

takahirom
Copy link
Member

Overview (Required)

  • It is still not working 😭. Probably because It is not released yet.
 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.github.droidkaigi.confsched2019.debug, PID: 14255
    kotlin.UninitializedPropertyAccessException: lateinit property retVal has not been initialized
        at arrow.typeclasses.suspended.BlockingContinuation.getRetVal(MonadSyntax.kt:24)
        at arrow.typeclasses.suspended.MonadSyntax$DefaultImpls.effect(MonadSyntax.kt:12)
        at arrow.typeclasses.MonadContinuation.effect(MonadContinuations.kt:16)
        at io.github.droidkaigi.confsched2019.session.ui.actioncreator.SessionContentsActionCreator$toggleFavorite$1.invokeSuspend(SessionContentsActionCreator.kt:71)
        at io.github.droidkaigi.confsched2019.session.ui.actioncreator.SessionContentsActionCreator$toggleFavorite$1.invoke(Unknown Source:10)
        at arrow.typeclasses.Monad$fx$wrapReturn$1.invokeSuspend(Monad.kt:80)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
        at kotlin.coroutines.ContinuationKt.startCoroutine(Continuation.kt:128)
        at arrow.typeclasses.Monad$DefaultImpls.fx(Monad.kt:81)
        at arrow.core.extensions.TryMonad$DefaultImpls.fx(Unknown Source:8)
        at arrow.core.extensions.try.monad.TryMonadKt$monad$1.fx(TryMonad.kt:195)
        at arrow.core.extensions.try.monad.TryMonadKt.fx(TryMonad.kt:178)
        at io.github.droidkaigi.confsched2019.session.ui.actioncreator.SessionContentsActionCreator.toggleFavorite(SessionContentsActionCreator.kt:70)
        at io.github.droidkaigi.confsched2019.session.ui.item.SpeechSessionItem$bind$$inlined$with$lambda$2.onClick(SpeechSessionItem.kt:80)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

@takahirom takahirom changed the title WIP Introduce arrow fx [WIP] Introduce arrow fx Feb 9, 2019
@jmatsu-bot
Copy link
Collaborator

launch {
try {
fx {
!effect {
Copy link
Contributor

@jmatsu jmatsu Feb 9, 2019

Choose a reason for hiding this comment

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

What's the purpose to use fx? 🤔
AFAIK, fx forces to represent side-effects by using suspend functions through compilation errors.

If you want to just integrate with coroutine, then arrow supports coroutine extension with DefferedK

val defferedK = async {
...
LoadingState.LOADED
}.k().handleErrorWith {
   dispatcher.dispatch(
                        Action.ShowProcessingMessage(
                            Message.of(
                                R.string.session_favorite_connection_error
                            )
                        )
                    )
  LoadingState.INITIALIZED
 }

dispatcher.dispatch(defferedK.unsafeAttemptSync())

maybe be like above.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

@takahirom takahirom Feb 9, 2019

Choose a reason for hiding this comment

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

Yes. Probably .k and DeferredK will be removed. 😇

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

  • Add io.arrow-kt:arrow-effects-io-extensions dependency
  • Import arrow.effects.extensions.io.fx.fx correctly

Then, follow the style below.

fun toggleFavorite(...) {
        unsafe {
            runNonBlocking({
                fx {
                    ....
                }
            }, { // Either monad will be coming
            })
        }
}

@takahirom takahirom changed the title [WIP] Introduce arrow fx [WIP] Introduce Arrow Fx Feb 10, 2019
@takahirom
Copy link
Member Author

Currently, the coroutines are not canceled when coroutineContext is canceled. 😭

@jmatsu-bot
Copy link
Collaborator

Apk comparision results

Property Summary
New File Size 8496751 Bytes. (8.1 MB)
File Size Change +36259 Bytes. (+35.41 KB)
Download Size Change +34935 Bytes. (+34.12 KB)
New Method Reference Count 63263
Method Reference Count Change 554
New Number of dex file(s) 1
Number of dex file(s) Change 0

Generated by 🚫 Danger

@jmatsu-bot
Copy link
Collaborator

Asserted successfully. 💯

Generated by 🚫 Danger

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