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

Skip to content
Merged
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
2 changes: 2 additions & 0 deletions core/js/src/main/scala/zio/internal/PlatformSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private[internal] trait PlatformSpecific {
new Platform {
val executor = executor0

val yieldOnStart = false

def fatal(t: Throwable): Boolean = false

def reportFatal(t: Throwable): Nothing = {
Expand Down
2 changes: 2 additions & 0 deletions core/jvm/src/main/scala/zio/internal/PlatformSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ private[internal] trait PlatformSpecific {
new Platform {
val executor = executor0

val yieldOnStart = true

val tracing = Tracing(Tracer.globallyCached(new AkkaLineNumbersTracer), TracingConfig.enabled)

def fatal(t: Throwable): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ private[internal] trait PlatformSpecific {
new Platform {
val executor = executor0

val yieldOnStart = false

def fatal(t: Throwable): Boolean = false

def reportFatal(t: Throwable): Nothing = {
Expand Down
7 changes: 6 additions & 1 deletion core/shared/src/main/scala/zio/Runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ trait Runtime[+R] {
* This method is effectful and should only be invoked at the edges of your program.
*/
final def unsafeRunAsyncCancelable[E, A](zio: => ZIO[R, E, A])(k: Exit[E, A] => Any): Fiber.Id => Exit[E, A] = {
lazy val curZio = if (Platform.isJVM) ZIO.yieldNow *> zio else zio
lazy val curZio = if (platform.yieldOnStart) ZIO.yieldNow *> zio else zio
val canceler = unsafeRunWith(curZio)(k)
fiberId => {
val result = internal.OneShot.make[Exit[E, A]]
Expand Down Expand Up @@ -168,6 +168,11 @@ trait Runtime[+R] {
*/
def withTracing(t: Tracing): Runtime[R] = mapPlatform(_.withTracing(t))

/**
* Constructs a new `Runtime` with the specified yield strategy.
*/
def withYieldOnStart(cond: Boolean): Runtime[R] = mapPlatform(_.withYieldOnStart(cond))

/**
* Constructs a new `Runtime` with the specified tracing configuration.
*/
Expand Down
14 changes: 14 additions & 0 deletions core/shared/src/main/scala/zio/internal/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ abstract class Platform { self =>
*/
def executor: Executor

/**
* Specifies if ZIO should yield immediately or not.
*/
def yieldOnStart: Boolean

def withExecutor(e: Executor): Platform =
new Platform.Proxy(self) {
override def executor: Executor = e
Expand All @@ -50,6 +55,14 @@ abstract class Platform { self =>
override val tracing: Tracing = self.tracing.copy(tracingConfig = config)
}

/**
* Determines if the `Runtime` should yield right at the beginning of the evaluation.
*/
def withYieldOnStart(cond: Boolean): Platform =
new Platform.Proxy(self) {
override val yieldOnStart: Boolean = cond
}

/**
* Determines if a throwable is fatal or not. It is important to identify
* these as it is not recommended to catch, and try to recover from, any
Expand Down Expand Up @@ -97,5 +110,6 @@ object Platform extends PlatformSpecific {
def reportFatal(t: Throwable): Nothing = self.reportFatal(t)
def reportFailure(cause: Cause[Any]): Unit = self.reportFailure(cause)
def supervisor: Supervisor[Any] = self.supervisor
def yieldOnStart: Boolean = self.yieldOnStart
}
}