Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 84b4dee

Browse files
committedFeb 26, 2025·
Use ordinary JS object instead of Scala Map
1 parent 632478a commit 84b4dee

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed
 

‎core/src/main/scala/org/scalajs/macrotaskexecutor/MacrotaskExecutor.scala

+21-15
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package org.scalajs.macrotaskexecutor
1818

19-
import scala.collection.mutable
2019
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
2120
import scala.scalajs.js
21+
import scala.scalajs.js.annotation._
2222
import scala.util.Random
2323
import scala.util.control.NonFatal
2424

@@ -34,10 +34,18 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
3434
def reportFailure(cause: Throwable): Unit =
3535
cause.printStackTrace()
3636

37+
@js.native
38+
private[this] trait TaskMap extends js.Object {
39+
@JSBracketAccess
40+
def apply(handle: Int): Runnable
41+
@JSBracketAccess
42+
def update(handle: Int, task: Runnable): Unit
43+
}
44+
3745
private[this] val setImmediate: Runnable => Unit = {
3846
if (js.typeOf(js.Dynamic.global.setImmediate) == Undefined) {
3947
var nextHandle = 1
40-
val tasksByHandle = mutable.Map[Int, Runnable]()
48+
val tasksByHandle = (new js.Object).asInstanceOf[TaskMap]
4149
var currentlyRunningATask = false
4250

4351
def canUsePostMessage(): Boolean = {
@@ -67,17 +75,15 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
6775
if (currentlyRunningATask) {
6876
js.Dynamic.global.setTimeout(() => runIfPresent(handle), 0)
6977
} else {
70-
tasksByHandle.get(handle) match {
71-
case Some(task) =>
72-
currentlyRunningATask = true
73-
try {
74-
task.run()
75-
} finally {
76-
tasksByHandle -= handle
77-
currentlyRunningATask = false
78-
}
79-
80-
case None =>
78+
val task = tasksByHandle(handle)
79+
if (!js.isUndefined(task)) {
80+
currentlyRunningATask = true
81+
try {
82+
task.asInstanceOf[Runnable].run()
83+
} finally {
84+
js.special.delete(tasksByHandle, handle)
85+
currentlyRunningATask = false
86+
}
8187
}
8288
}
8389

@@ -129,7 +135,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
129135
val handle = nextHandle
130136
nextHandle += 1
131137

132-
tasksByHandle += (handle -> k)
138+
tasksByHandle(handle) = k
133139
js.Dynamic.global.postMessage(messagePrefix + handle, "*")
134140
()
135141
}
@@ -144,7 +150,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
144150
val handle = nextHandle
145151
nextHandle += 1
146152

147-
tasksByHandle += (handle -> k)
153+
tasksByHandle(handle) = k
148154
channel.port2.postMessage(handle)
149155
()
150156
}

0 commit comments

Comments
 (0)
Please sign in to comment.