1616
1717package org .scalajs .macrotaskexecutor
1818
19- import scala .collection .mutable
2019import scala .concurrent .{ExecutionContext , ExecutionContextExecutor }
2120import scala .scalajs .js
2221import scala .util .Random
@@ -37,7 +36,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
3736 private [this ] val setImmediate : Runnable => Unit = {
3837 if (js.typeOf(js.Dynamic .global.setImmediate) == Undefined ) {
3938 var nextHandle = 1
40- val tasksByHandle = mutable. Map [ Int , Runnable ]()
39+ val tasksByHandle = ( new js. Object ). asInstanceOf [js. Dynamic ]
4140 var currentlyRunningATask = false
4241
4342 def canUsePostMessage (): Boolean = {
@@ -67,17 +66,15 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
6766 if (currentlyRunningATask) {
6867 js.Dynamic .global.setTimeout(() => runIfPresent(handle), 0 )
6968 } 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 =>
69+ val task = tasksByHandle(handle).asInstanceOf [js.UndefOr [Runnable ]]
70+ if (task.isDefined) {
71+ currentlyRunningATask = true
72+ try {
73+ task.asInstanceOf [Runnable ].run()
74+ } finally {
75+ js.special.delete(tasksByHandle, handle)
76+ currentlyRunningATask = false
77+ }
8178 }
8279 }
8380
@@ -129,7 +126,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
129126 val handle = nextHandle
130127 nextHandle += 1
131128
132- tasksByHandle += (handle -> k)
129+ tasksByHandle(handle) = k. asInstanceOf [js. Any ]
133130 js.Dynamic .global.postMessage(messagePrefix + handle, " *" )
134131 ()
135132 }
@@ -144,7 +141,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
144141 val handle = nextHandle
145142 nextHandle += 1
146143
147- tasksByHandle += (handle -> k)
144+ tasksByHandle(handle) = k. asInstanceOf [js. Any ]
148145 channel.port2.postMessage(handle)
149146 ()
150147 }
0 commit comments