Skip to content

Commit

Permalink
Allow IOApp to access the default ExecutionContext
Browse files Browse the repository at this point in the history
Currently it's not possible for users of `IOApp` to use the default
ExecutionContext that backs the ContextShift.

They need to either use `IOApp.WithContext`, or use two unrelated pools, or
override the default `ContextShift`.

This should remove some needless hoop-jumping, I think

Close #748
  • Loading branch information
Daenyth committed Nov 28, 2020
1 parent 14dceaa commit 94e02fa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package internals
import cats.syntax.all._
import scala.concurrent.duration._
import scala.scalajs.js
import scala.concurrent.ExecutionContext

private[effect] object IOAppPlatform {
def main(args: Array[String], cs: Eval[ContextShift[IO]], timer: Eval[Timer[IO]])(
Expand All @@ -41,8 +42,8 @@ private[effect] object IOAppPlatform {
/**
* Sets the exit code with `process.exitCode = code` for runtimes
* that support it. This allows a graceful shutdown with a specific
* exit code.
*
* exit code.
*
* If the call is not supported and the exit code is not Success,
* then it is logged.
*
Expand Down Expand Up @@ -82,6 +83,7 @@ private[effect] object IOAppPlatform {

val defaultTimer: Timer[IO] = IOTimer.global
val defaultContextShift: ContextShift[IO] = IOContextShift.global
val defaultExecutionContext: ExecutionContext = ExecutionContext.Implicits.global

private def installHandler(fiber: Fiber[IO, Int]): IO[Unit] = {
def handler(code: Int) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private[effect] trait IOAppCompanionPlatform {
* [[executionContextResource]]. Outside `run`, this context will
* reject all tasks.
*/
final protected def executionContext: ExecutionContext =
final override protected def executionContext: ExecutionContext =
currentContext.get().executionContext

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package cats
package effect
package internals

import scala.concurrent.ExecutionContext

private[effect] object IOAppPlatform {
def main(args: Array[String], contextShift: Eval[ContextShift[IO]], timer: Eval[Timer[IO]])(
run: List[String] => IO[ExitCode]
Expand Down Expand Up @@ -58,6 +60,9 @@ private[effect] object IOAppPlatform {
def defaultContextShift: ContextShift[IO] =
IOContextShift(PoolUtils.ioAppGlobal)

def defaultExecutionContext: ExecutionContext =
PoolUtils.ioAppGlobal

private def installHook(fiber: Fiber[IO, Int]): IO[Unit] =
IO {
sys.addShutdownHook {
Expand Down
16 changes: 16 additions & 0 deletions core/shared/src/main/scala/cats/effect/IOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cats
package effect

import cats.effect.internals.{IOAppCompanionPlatform, IOAppPlatform}
import scala.concurrent.ExecutionContext

/**
* `App` type that runs a [[cats.effect.IO]]. Shutdown occurs after
Expand Down Expand Up @@ -100,6 +101,21 @@ trait IOApp {
*/
implicit protected def timer: Timer[IO] =
IOAppPlatform.defaultTimer

/**
* Provides a default [[ExecutionContext]] for the app.
*
* The default on top of the JVM is lazily constructed as a fixed
* thread pool based on number available of available CPUs (see
* `PoolUtils`).
*
* On top of JavaScript, this will use the standard
* [[https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout setTimeout]].
*
* @note This is the same ExecutionContext that backs the default implicit `ContextShift`
*/
protected def executionContext: ExecutionContext =
IOAppPlatform.defaultExecutionContext
}

object IOApp extends IOAppCompanionPlatform

0 comments on commit 94e02fa

Please sign in to comment.