Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
75 changes: 50 additions & 25 deletions core/shared/src/main/scala/zio/Runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ trait Runtime[+R] { self =>
trait UnsafeAPI {

/**
* Executes the effect synchronously and returns its result as a
* [[zio.Exit]] value. May fail on Scala.js if the effect cannot be entirely
* run synchronously.
* Executes the effect asynchronously, returning a fiber whose methods can
* await the exit value of the fiber or interrupt the fiber.
*
* This method is effectful and should only be used at the edges of your
* application.
*/
def run[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): Exit[E, A]
def fork[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): Fiber.Runtime[E, A]

/**
* Executes the effect asynchronously, returning a fiber whose methods can
* await the exit value of the fiber or interrupt the fiber.
* Executes the effect synchronously and returns its result as a
* [[zio.Exit]] value. May fail on Scala.js if the effect cannot be entirely
* run synchronously.
*
* This method is effectful and should only be used at the edges of your
* application.
*/
def fork[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): Fiber.Runtime[E, A]
def run[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): Exit[E, A]

/**
* Executes the effect asynchronously, returning a Future that will be
Expand All @@ -102,11 +102,47 @@ trait Runtime[+R] { self =>
)(implicit trace: Trace, unsafe: Unsafe): CancelableFuture[A]
}

def unsafe: UnsafeAPI =
trait UnsafeAPI3 {

/**
* Attempts to execute the effect synchronously and returns its result as a
* [[zio.Exit]] value. If the effect cannot be entirely run synchronously,
* the effect will be forked and the fiber will be returned.
*
* This method is effectful and should only be used at the edges of your
* application.
*/
def runOrFork[E, A](
zio: ZIO[R, E, A]
)(implicit trace: Trace, unsafe: Unsafe): Either[Fiber.Runtime[E, A], Exit[E, A]]
}

def unsafe: UnsafeAPI with UnsafeAPI3 =
new UnsafeAPIV1 {}

protected abstract class UnsafeAPIV1 extends UnsafeAPI {
def run[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): Exit[E, A] = {
protected abstract class UnsafeAPIV1 extends UnsafeAPI with UnsafeAPI3 {

def fork[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): internal.FiberRuntime[E, A] = {
val fiber = makeFiber(zio)
fiber.start[R](zio)
fiber
}

def run[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): Exit[E, A] =
runOrFork(zio) match {
case Left(fiber) =>
import internal.{FiberMessage, OneShot}
val result = OneShot.make[Exit[E, A]]
fiber.tell(
FiberMessage.Stateful((fiber, _) => fiber.addObserver(exit => result.set(exit.asInstanceOf[Exit[E, A]])))
)
result.get()
case Right(exit) => exit
}

def runOrFork[E, A](
zio: ZIO[R, E, A]
)(implicit trace: Trace, unsafe: Unsafe): Either[internal.FiberRuntime[E, A], Exit[E, A]] = {
import internal.FiberRuntime

val fiberId = FiberId.make(trace)
Expand All @@ -121,26 +157,15 @@ trait Runtime[+R] { self =>
fiber.addObserver(exit => supervisor.onEnd(exit, fiber))
}

val fastExit = fiber.start[R](zio)
val exit = fiber.start[R](zio)

if (fastExit != null) fastExit
if (exit != null) Right(exit)
else {
import internal.{FiberMessage, OneShot}
FiberScope.global.add(null, runtimeFlags, fiber)
val result = OneShot.make[Exit[E, A]]
fiber.tell(
FiberMessage.Stateful((fiber, _) => fiber.addObserver(exit => result.set(exit.asInstanceOf[Exit[E, A]])))
)
result.get()
Left(fiber)
}
}

def fork[E, A](zio: ZIO[R, E, A])(implicit trace: Trace, unsafe: Unsafe): internal.FiberRuntime[E, A] = {
val fiber = makeFiber(zio)
fiber.start[R](zio)
fiber
}

def runToFuture[E <: Throwable, A](
zio: ZIO[R, E, A]
)(implicit trace: Trace, unsafe: Unsafe): CancelableFuture[A] = {
Expand Down Expand Up @@ -331,7 +356,7 @@ object Runtime extends RuntimePlatformSpecific {
def shutdown()(implicit unsafe: Unsafe): Unit
}

override def unsafe: UnsafeAPI with UnsafeAPI2 =
override def unsafe: UnsafeAPI with UnsafeAPI2 with UnsafeAPI3 =
new UnsafeAPIV2 {}

protected abstract class UnsafeAPIV2 extends UnsafeAPIV1 with UnsafeAPI2 {
Expand Down