All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kyo.server.internal.KyoUtil.scala Maven / Gradle / Ivy

There is a newer version: 0.15.1
Show newest version
package kyo.server.internal

import io.netty.channel.Channel
import io.netty.channel.ChannelFuture
import kyo.*

object KyoUtil:
    def nettyChannelFutureToScala(nettyFuture: ChannelFuture)(using Frame): Channel < Async =
        Promise.init[Nothing, Channel].map { p =>
            p.onComplete(_ => IO(nettyFuture.cancel(true)).unit).andThen {
                nettyFuture.addListener((future: ChannelFuture) =>
                    discard {
                        import AllowUnsafe.embrace.danger
                        if future.isSuccess then p.unsafe.complete(Result.success(future.channel()))
                        else p.unsafe.complete(Result.panic(future.cause()))
                    }
                )
                p.get
            }
        }

    def nettyFutureToScala[A](f: io.netty.util.concurrent.Future[A])(using Frame): A < Async =
        Promise.init[Nothing, A].map { p =>
            p.onComplete(_ => IO(f.cancel(true)).unit).andThen {
                f.addListener((future: io.netty.util.concurrent.Future[A]) =>
                    discard {
                        import AllowUnsafe.embrace.danger
                        if future.isSuccess then p.unsafe.complete(Result.success(future.getNow))
                        else p.unsafe.complete(Result.panic(future.cause()))
                    }
                )
                p.get
            }
        }
end KyoUtil




© 2015 - 2025 Weber Informatics LLC | Privacy Policy