Skip to content

Commit 16f52a6

Browse files
Richard WhalingRichard Whaling
authored andcommitted
added scalajs compat
1 parent 3388ea9 commit 16f52a6

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

scalajs-compat/Handles.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Scala.js (https://www.scala-js.org/)
3+
*
4+
* Copyright EPFL.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (https://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.scalajs.js.timers
14+
15+
import scalanative.unsafe.Zone
16+
import scalanative.loop.LibUV.TimerHandle
17+
18+
/** <span class="badge badge-non-std" style="float: right;">Non-Standard</span>
19+
* A handle returned from a call to
20+
* [[setTimeout(interval:scala\.concurrent\.duration\.FiniteDuration)* setTimeout]].
21+
*
22+
* May only be used to pass to [[clearTimeout]].
23+
*/
24+
trait SetTimeoutHandle {
25+
private[timers] val timerHandle: TimerHandle
26+
}
27+
28+
/** <span class="badge badge-non-std" style="float: right;">Non-Standard</span>
29+
* A handle returned from a call to
30+
* [[setInterval(interval:scala\.concurrent\.duration\.FiniteDuration)* setInterval]].
31+
*
32+
* May only be used to pass to [[clearInterval]].
33+
*/
34+
trait SetIntervalHandle {
35+
private[timers] val timerHandle: TimerHandle
36+
}

scalajs-compat/RawTimers.scala

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Scala.js (https://www.scala-js.org/)
3+
*
4+
* Copyright EPFL.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (https://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.scalajs.js.timers
14+
15+
import scalanative.unsafe.Zone
16+
import scalanative.libc.stdlib
17+
import scalanative.loop.Timer
18+
import scalanative.loop.LibUV.{TimerHandle, uv_timer_stop}
19+
import scalanative.loop.LibUVConstants.check
20+
21+
/**
22+
* <span class="badge badge-non-std" style="float: right;">Non-Standard</span>
23+
* Raw JavaScript timer methods.
24+
*
25+
* The methods on this object expose the raw JavaScript methods for timers. In
26+
* general it is more advisable to use the methods directly defined on
27+
* [[timers]] as they are more Scala-like.
28+
*/
29+
object RawTimers {
30+
31+
/** Schedule `handler` for execution in `interval` milliseconds.
32+
*
33+
* @param handler the function to call after `interval` has passed
34+
* @param interval duration in milliseconds to wait
35+
* @return A handle that can be used to cancel the timeout by passing it
36+
* to [[clearTimeout]].
37+
*/
38+
def setTimeout(handler: () => Unit, interval: Double): SetTimeoutHandle = {
39+
val t = Timer.oneTime(interval, handler)
40+
new SetTimeoutHandle {
41+
val timerHandle = t
42+
}
43+
}
44+
45+
/** Cancel a timeout execution
46+
* @param handle The handle returned by [[setTimeout]]
47+
*/
48+
def clearTimeout(handle: SetTimeoutHandle): Unit = {
49+
if (handle.timerHandle != null) {
50+
check(uv_timer_stop(handle.timerHandle),"uv_timer_stop")
51+
stdlib.free(handle.timerHandle)
52+
}
53+
}
54+
55+
/** Schedule `handler` for repeated execution every `interval`
56+
* milliseconds.
57+
*
58+
* @param handler the function to call after each `interval`
59+
* @param interval duration in milliseconds between executions
60+
* @return A handle that can be used to cancel the interval by passing it
61+
* to [[clearInterval]].
62+
*/
63+
def setInterval(handler: () => Unit, interval: Double): SetIntervalHandle = {
64+
val t = Timer.repeat(interval, handler)
65+
new SetIntervalHandle {
66+
val timerHandle = t
67+
}
68+
}
69+
70+
/** Cancel an interval execution
71+
* @param handle The handle returned by [[setInterval]]
72+
*/
73+
def clearInterval(handle: SetIntervalHandle): Unit = {
74+
if (handle.timerHandle != null) {
75+
check(uv_timer_stop(handle.timerHandle),"uv_timer_stop")
76+
stdlib.free(handle.timerHandle)
77+
}
78+
}
79+
}

scalajs-compat/package.scala

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Scala.js (https://www.scala-js.org/)
3+
*
4+
* Copyright EPFL.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (https://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.scalajs.js
14+
15+
import scala.concurrent.duration.FiniteDuration
16+
17+
/**
18+
* <span class="badge badge-non-std" style="float: right;">Non-Standard</span>
19+
* Non-standard, but in general well supported methods to schedule asynchronous
20+
* execution.
21+
*
22+
* The methods in this package work in all JavaScript virtual machines
23+
* supporting `setTimeout` and `setInterval`.
24+
*/
25+
package object timers {
26+
27+
/** Schedule something for execution in `interval` milliseconds.
28+
*
29+
* @param interval duration in milliseconds to wait
30+
* @param body code to execute after `interval` has passed
31+
* @return A handle that can be used to cancel the timeout by passing it
32+
* to [[clearTimeout]].
33+
* @note Uses JavaScript's non-standard `setTimeout`
34+
*/
35+
def setTimeout(interval: Double)(body: => Unit): SetTimeoutHandle =
36+
RawTimers.setTimeout(() => body, interval)
37+
38+
/** Schedule something for execution after a duration.
39+
*
40+
* @param interval duration to wait
41+
* @param body code to execute after `interval` has passed
42+
* @return A handle that can be used to cancel the timeout by passing it
43+
* to [[clearTimeout]].
44+
* @note Uses JavaScript's non-standard `setTimeout`
45+
*/
46+
def setTimeout(interval: FiniteDuration)(body: => Unit): SetTimeoutHandle =
47+
RawTimers.setTimeout(() => body, interval.toMillis.toDouble)
48+
49+
/** Cancel a timeout execution
50+
* @param handle The handle returned by
51+
* [[setTimeout(interval:scala\.concurrent\.duration\.FiniteDuration)* setTimeout]].
52+
* @note Uses JavaScript's non-standard `clearTimeout`
53+
*/
54+
def clearTimeout(handle: SetTimeoutHandle): Unit =
55+
RawTimers.clearTimeout(handle)
56+
57+
/** Schedule something for repeated execution every `interval` milliseconds.
58+
*
59+
* @param interval duration in milliseconds between executions
60+
* @param body code to execute after each `interval`
61+
* @return A handle that can be used to cancel the interval by passing it
62+
* to [[clearInterval]].
63+
* @note Uses JavaScript's non-standard `setInterval`
64+
*/
65+
def setInterval(interval: Double)(body: => Unit): SetIntervalHandle =
66+
RawTimers.setInterval(() => body, interval)
67+
68+
/** Schedule something for repeated execution every duration.
69+
*
70+
* @param interval duration between executions
71+
* @param body code to execute after each `interval`
72+
* @return A handle that can be used to cancel the interval by passing it
73+
* to [[clearInterval]].
74+
* @note Uses JavaScript's non-standard `setInterval`
75+
*/
76+
def setInterval(interval: FiniteDuration)(body: => Unit): SetIntervalHandle =
77+
RawTimers.setInterval(() => body, interval.toMillis.toDouble)
78+
79+
/** Cancel an interval execution
80+
* @param handle The handle returned by
81+
* [[setInterval(interval:scala\.concurrent\.duration\.FiniteDuration)* setInterval]].
82+
* @note Uses JavaScript's non-standard `clearInterval`
83+
*/
84+
def clearInterval(handle: SetIntervalHandle): Unit =
85+
RawTimers.clearInterval(handle)
86+
87+
}

0 commit comments

Comments
 (0)