File tree 2 files changed +21
-6
lines changed
IntegrationTests/TestSuites/Sources/PrimaryTests
Sources/JavaScriptKit/BasicObjects
2 files changed +21
-6
lines changed Original file line number Diff line number Diff line change @@ -488,21 +488,29 @@ try test("Date") {
488
488
}
489
489
490
490
// make the timers global to prevent early deallocation
491
- var timeout : JSTimer ?
491
+ var timeouts : [ JSTimer ] = [ ]
492
492
var interval : JSTimer ?
493
493
494
494
try test ( " Timer " ) {
495
495
let start = JSDate ( ) . valueOf ( )
496
496
let timeoutMilliseconds = 5.0
497
-
497
+ var timeout : JSTimer !
498
498
timeout = JSTimer ( millisecondsDelay: timeoutMilliseconds, isRepeating: false ) {
499
499
// verify that at least `timeoutMilliseconds` passed since the `timeout` timer started
500
500
try ! expectEqual ( start + timeoutMilliseconds <= JSDate ( ) . valueOf ( ) , true )
501
501
}
502
+ timeouts += [ timeout]
503
+
504
+ timeout = JSTimer ( millisecondsDelay: timeoutMilliseconds, isRepeating: false ) {
505
+ fatalError ( " timer should be cancelled " )
506
+ }
507
+ timeout = nil
502
508
503
509
var count = 0.0
504
510
let maxCount = 5.0
505
511
interval = JSTimer ( millisecondsDelay: 5 , isRepeating: true ) {
512
+ // ensure that JSTimer is living
513
+ try ! expectNotNil ( interval)
506
514
// verify that at least `timeoutMilliseconds * count` passed since the `timeout`
507
515
// timer started
508
516
try ! expectEqual ( start + timeoutMilliseconds * count <= JSDate ( ) . valueOf ( ) , true )
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ public final class JSTimer {
14
14
/// Indicates whether this timer instance calls its callback repeatedly at a given delay.
15
15
public let isRepeating : Bool
16
16
17
- private let closure : JSClosure
17
+ private let closure : JSOneshotClosure
18
18
19
19
/** Node.js and browser APIs are slightly different. `setTimeout`/`setInterval` return an object
20
20
in Node.js, while browsers return a number. Fortunately, clearTimeout and clearInterval take
@@ -34,9 +34,16 @@ public final class JSTimer {
34
34
- callback: the closure to be executed after a given `millisecondsDelay` interval.
35
35
*/
36
36
public init ( millisecondsDelay: Double , isRepeating: Bool = false , callback: @escaping ( ) -> ( ) ) {
37
- closure = JSClosure { _ in
38
- callback ( )
39
- return . undefined
37
+ if isRepeating {
38
+ closure = JSClosure { _ in
39
+ callback ( )
40
+ return . undefined
41
+ }
42
+ } else {
43
+ closure = JSOneshotClosure { _ in
44
+ callback ( )
45
+ return . undefined
46
+ }
40
47
}
41
48
self . isRepeating = isRepeating
42
49
if isRepeating {
You can’t perform that action at this time.
0 commit comments