1616
1717package org .scalajs .macrotaskexecutor
1818
19- import scala . collection . mutable
19+ import java . util . HashMap
2020import scala .concurrent .{ExecutionContext , ExecutionContextExecutor }
2121import scala .scalajs .js
2222import scala .util .Random
@@ -37,7 +37,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
3737 private [this ] val setImmediate : Runnable => Unit = {
3838 if (js.typeOf(js.Dynamic .global.setImmediate) == Undefined ) {
3939 var nextHandle = 1
40- val tasksByHandle = mutable. Map [Int , Runnable ]()
40+ val tasksByHandle = new HashMap [Int , Runnable ]
4141 var currentlyRunningATask = false
4242
4343 def canUsePostMessage (): Boolean = {
@@ -67,17 +67,15 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
6767 if (currentlyRunningATask) {
6868 js.Dynamic .global.setTimeout(() => runIfPresent(handle), 0 )
6969 } 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 =>
70+ val task = tasksByHandle.get(handle)
71+ if (task ne null ) {
72+ currentlyRunningATask = true
73+ try {
74+ task.run()
75+ } finally {
76+ tasksByHandle.remove(handle)
77+ currentlyRunningATask = false
78+ }
8179 }
8280 }
8381
@@ -129,7 +127,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
129127 val handle = nextHandle
130128 nextHandle += 1
131129
132- tasksByHandle += (handle -> k)
130+ tasksByHandle.put (handle, k)
133131 js.Dynamic .global.postMessage(messagePrefix + handle, " *" )
134132 ()
135133 }
@@ -144,7 +142,7 @@ object MacrotaskExecutor extends ExecutionContextExecutor {
144142 val handle = nextHandle
145143 nextHandle += 1
146144
147- tasksByHandle += (handle -> k)
145+ tasksByHandle.put (handle, k)
148146 channel.port2.postMessage(handle)
149147 ()
150148 }
0 commit comments