Skip to content

WiP Separate the scalalib from the Scala.js library. #4787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use a private copy of QueueExecutionContext in `ExecutionContext.gl…
…obal`.

This is required not to depend on the Scala.js library from
`ExecutionContext`.
  • Loading branch information
sjrd committed Oct 27, 2023
commit c8fe6d05af1cbf3ad5eade58fa5bc35ae2e7c618
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ trait ExecutionContext {
/** Prepares for the execution of a task. Returns the prepared
* execution context. The recommended implementation of
* `prepare` is to return `this`.
*
*
* This method should no longer be overridden or called. It was
* originally expected that `prepare` would be called by
* all libraries that consume ExecutionContexts, in order to
Expand Down Expand Up @@ -130,7 +130,7 @@ object ExecutionContext {
* [[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- available processors]].
*/
implicit lazy val global: ExecutionContext =
scala.scalajs.concurrent.JSExecutionContext.queue
JSGlobalExecutionContext()
}

/** Creates an `ExecutionContext` from the given `ExecutorService`.
Expand Down Expand Up @@ -179,5 +179,3 @@ object ExecutionContext {
*/
def defaultReporter: Throwable => Unit = _.printStackTrace()
}


Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ object ExecutionContext {
* @return the global `ExecutionContext`
*/
final lazy val global: ExecutionContextExecutor =
scala.scalajs.concurrent.JSExecutionContext.queue
JSGlobalExecutionContext()

/**
* WARNING: Only ever execute logic which will quickly return control to the caller.
Expand Down
55 changes: 55 additions & 0 deletions scalalib/overrides/scala/concurrent/JSGlobalExecutionContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Scala.js (https://www.scala-js.org/)
*
* Copyright EPFL.
*
* Licensed under Apache License 2.0
* (https://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.concurrent

import scala.scalajs.js
import scala.scalajs.js.|

private[concurrent] object JSGlobalExecutionContext {
def apply(): ExecutionContextExecutor =
if (js.typeOf(js.Dynamic.global.Promise) == "undefined") new TimeoutsExecutionContext
else new PromisesExecutionContext

private final class TimeoutsExecutionContext extends ExecutionContextExecutor {
def execute(runnable: Runnable): Unit = {
js.Dynamic.global.setTimeout({ () =>
try {
runnable.run()
} catch {
case t: Throwable => reportFailure(t)
}
}: js.Function0[Unit], 0)
}

def reportFailure(t: Throwable): Unit =
t.printStackTrace()
}

private final class PromisesExecutionContext extends ExecutionContextExecutor {
private val resolvedUnitPromise = js.Promise.resolve[Unit](())

def execute(runnable: Runnable): Unit = {
resolvedUnitPromise.`then` { (_: Unit) =>
try {
runnable.run()
} catch {
case t: Throwable => reportFailure(t)
}
(): Unit | js.Thenable[Unit]
}
}

def reportFailure(t: Throwable): Unit =
t.printStackTrace()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ class AsyncTest {
queueExecOrderTests { () =>
tick(1)
}(ExecutionContext.global)

assertSame(JSExecutionContext.queue, ExecutionContext.global)
}
}

Expand Down