Skip to content

Commit efd633e

Browse files
Use JSOneshotClosure in JSTimer
1 parent 3b93924 commit efd633e

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

Diff for: IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,29 @@ try test("Date") {
488488
}
489489

490490
// make the timers global to prevent early deallocation
491-
var timeout: JSTimer?
491+
var timeouts: [JSTimer] = []
492492
var interval: JSTimer?
493493

494494
try test("Timer") {
495495
let start = JSDate().valueOf()
496496
let timeoutMilliseconds = 5.0
497-
497+
var timeout: JSTimer!
498498
timeout = JSTimer(millisecondsDelay: timeoutMilliseconds, isRepeating: false) {
499499
// verify that at least `timeoutMilliseconds` passed since the `timeout` timer started
500500
try! expectEqual(start + timeoutMilliseconds <= JSDate().valueOf(), true)
501501
}
502+
timeouts += [timeout]
503+
504+
timeout = JSTimer(millisecondsDelay: timeoutMilliseconds, isRepeating: false) {
505+
fatalError("timer should be cancelled")
506+
}
507+
timeout = nil
502508

503509
var count = 0.0
504510
let maxCount = 5.0
505511
interval = JSTimer(millisecondsDelay: 5, isRepeating: true) {
512+
// ensure that JSTimer is living
513+
try! expectNotNil(interval)
506514
// verify that at least `timeoutMilliseconds * count` passed since the `timeout`
507515
// timer started
508516
try! expectEqual(start + timeoutMilliseconds * count <= JSDate().valueOf(), true)

Diff for: Sources/JavaScriptKit/BasicObjects/JSTimer.swift

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class JSTimer {
1414
/// Indicates whether this timer instance calls its callback repeatedly at a given delay.
1515
public let isRepeating: Bool
1616

17-
private let closure: JSClosure
17+
private let closure: JSOneshotClosure
1818

1919
/** Node.js and browser APIs are slightly different. `setTimeout`/`setInterval` return an object
2020
in Node.js, while browsers return a number. Fortunately, clearTimeout and clearInterval take
@@ -34,9 +34,16 @@ public final class JSTimer {
3434
- callback: the closure to be executed after a given `millisecondsDelay` interval.
3535
*/
3636
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+
}
4047
}
4148
self.isRepeating = isRepeating
4249
if isRepeating {

0 commit comments

Comments
 (0)