Skip to content

Commit

Permalink
Merge pull request #1457 from Daenyth/get-ec
Browse files Browse the repository at this point in the history
Allow `IOApp` to access the default ExecutionContext
  • Loading branch information
djspiewak authored Nov 28, 2020
2 parents 14dceaa + 6f0c822 commit 2ff1331
Show file tree
Hide file tree
Showing 4 changed files with 29 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
19 changes: 19 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,24 @@ 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`
*
* @note To use a different `ExecutionContext`, consider extending `IOApp.WithContext`,
* which will do it more comprehensively.
*/
protected def executionContext: ExecutionContext =
IOAppPlatform.defaultExecutionContext
}

object IOApp extends IOAppCompanionPlatform

0 comments on commit 2ff1331

Please sign in to comment.