Skip to content
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

Fix JS feature detection so that it no longer crashes #2188

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@ import scala.util.Random
* Based on https://github.com/YuzuJS/setImmediate
*/
private[unsafe] object PolyfillExecutionContext extends ExecutionContext {
private[this] val Undefined = "undefined"

def execute(runnable: Runnable): Unit =
setImmediate(() => runnable.run())

def reportFailure(cause: Throwable): Unit =
cause.printStackTrace()

private[this] def isAvailable(a: => js.Dynamic): Boolean =
try {
a.asInstanceOf[js.UndefOr[Any]].isDefined
} catch {
case _: Throwable => false
}
Comment on lines +35 to +40
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about !js.isUndefined(a)? Is that the same?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I tried this when I implemented the web worker polyfill and ran into problems, but maybe I did it wrong.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No js.isUndefined wouldn't work because it only tests whether a strict/eager value is undefined or not, but the problem is an exception is being thrown when attempting to resolve that value. Personally I think it's silly behaviour but sjrd is very, very adamant in these things. It is what it is now so when testing for optional JS features we need to always be ready to catch a (Fatal) exception.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, good to know. Thanks.


private[this] val setImmediate: (() => Unit) => Unit = {
if (js.typeOf(js.Dynamic.global.setImmediate) == Undefined) {
if (!isAvailable(js.Dynamic.global.setImmediate)) {
var nextHandle = 1
val tasksByHandle = mutable.Map[Int, () => Unit]()
var currentlyRunningATask = false

def canUsePostMessage(): Boolean = {
// The test against `importScripts` prevents this implementation from being installed inside a web worker,
// where `global.postMessage` means something completely different and can't be used for this purpose.
if (js.typeOf(js.Dynamic.global.postMessage) != Undefined && js.typeOf(
js.Dynamic.global.importScripts) == Undefined) {
if (isAvailable(js.Dynamic.global.postMessage) && !isAvailable(js.Dynamic.global.importScripts)) {
var postMessageIsAsynchronous = true
val oldOnMessage = js.Dynamic.global.onmessage

Expand Down Expand Up @@ -99,7 +104,7 @@ private[unsafe] object PolyfillExecutionContext extends ExecutionContext {
}
}

if (js.typeOf(js.Dynamic.global.addEventListener) != Undefined) {
if (isAvailable(js.Dynamic.global.addEventListener)) {
js.Dynamic.global.addEventListener("message", onGlobalMessage _, false)
} else {
js.Dynamic.global.attachEvent("onmessage", onGlobalMessage _)
Expand All @@ -113,7 +118,7 @@ private[unsafe] object PolyfillExecutionContext extends ExecutionContext {
js.Dynamic.global.postMessage(messagePrefix + handle, "*")
()
}
} else if (js.typeOf(js.Dynamic.global.MessageChannel) != Undefined) {
} else if (isAvailable(js.Dynamic.global.MessageChannel)) {
val channel = js.Dynamic.newInstance(js.Dynamic.global.MessageChannel)()

channel.port1.onmessage = { (event: js.Dynamic) =>
Expand Down