|
1 | 1 | /** This timer is an abstraction over [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval)
|
2 |
| -/ [`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval) and |
| 2 | +/ [`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval) and |
3 | 3 | [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)
|
4 | 4 | / [`clearTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)
|
5 | 5 | JavaScript functions. It intentionally doesn't match the JavaScript API, as a special care is
|
6 | 6 | needed to hold a reference to the timer closure and to call `JSClosure.release()` on it when the
|
7 | 7 | timer is deallocated. As a user, you have to hold a reference to a `JSTimer` instance for it to stay
|
8 | 8 | valid. The `JSTimer` API is also intentionally trivial, the timer is started right away, and the
|
9 | 9 | only way to invalidate the timer is to bring the reference count of the `JSTimer` instance to zero.
|
10 |
| -For invalidation you should either store the timer in an optional property and assign `nil` to it, |
| 10 | +For invalidation you should either store the timer in an optional property and assign `nil` to it, |
11 | 11 | or deallocate the object that owns the timer.
|
12 | 12 | */
|
13 | 13 | public final class JSTimer {
|
14 |
| - /// Indicates whether this timer instance calls its callback repeatedly at a given delay. |
15 |
| - public let isRepeating: Bool |
| 14 | + /// Indicates whether this timer instance calls its callback repeatedly at a given delay. |
| 15 | + public let isRepeating: Bool |
16 | 16 |
|
17 |
| - private let closure: JSOneshotClosure |
| 17 | + private let closure: JSClosureProtocol |
18 | 18 |
|
19 |
| - /** Node.js and browser APIs are slightly different. `setTimeout`/`setInterval` return an object |
20 |
| - in Node.js, while browsers return a number. Fortunately, clearTimeout and clearInterval take |
21 |
| - corresponding types as their arguments, and we can store either as JSValue, so we can treat both |
22 |
| - cases uniformly. |
23 |
| - */ |
24 |
| - private let value: JSValue |
25 |
| - private let global = JSObject.global |
| 19 | + /** Node.js and browser APIs are slightly different. `setTimeout`/`setInterval` return an object |
| 20 | + in Node.js, while browsers return a number. Fortunately, clearTimeout and clearInterval take |
| 21 | + corresponding types as their arguments, and we can store either as JSValue, so we can treat both |
| 22 | + cases uniformly. |
| 23 | + */ |
| 24 | + private let value: JSValue |
| 25 | + private let global = JSObject.global |
26 | 26 |
|
27 |
| - /** |
28 |
| - Creates a new timer instance that calls `setInterval` or `setTimeout` JavaScript functions for you |
29 |
| - under the hood. |
30 |
| - - Parameters: |
31 |
| - - millisecondsDelay: the amount of milliseconds before the `callback` closure is executed. |
32 |
| - - isRepeating: when `true` the `callback` closure is executed repeatedly at given |
33 |
| - `millisecondsDelay` intervals indefinitely until the timer is deallocated. |
34 |
| - - callback: the closure to be executed after a given `millisecondsDelay` interval. |
35 |
| - */ |
36 |
| - public init(millisecondsDelay: Double, isRepeating: Bool = false, callback: @escaping () -> ()) { |
37 |
| - if isRepeating { |
38 |
| - closure = JSClosure { _ in |
39 |
| - callback() |
40 |
| - return .undefined |
| 27 | + /** |
| 28 | + Creates a new timer instance that calls `setInterval` or `setTimeout` JavaScript functions for you |
| 29 | + under the hood. |
| 30 | + - Parameters: |
| 31 | + - millisecondsDelay: the amount of milliseconds before the `callback` closure is executed. |
| 32 | + - isRepeating: when `true` the `callback` closure is executed repeatedly at given |
| 33 | + `millisecondsDelay` intervals indefinitely until the timer is deallocated. |
| 34 | + - callback: the closure to be executed after a given `millisecondsDelay` interval. |
| 35 | + */ |
| 36 | + public init(millisecondsDelay: Double, isRepeating: Bool = false, callback: @escaping () -> ()) { |
| 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 | + } |
41 | 47 | }
|
42 |
| - } else { |
43 |
| - closure = JSOneshotClosure { _ in |
44 |
| - callback() |
45 |
| - return .undefined |
| 48 | + self.isRepeating = isRepeating |
| 49 | + if isRepeating { |
| 50 | + value = global.setInterval.function!(closure, millisecondsDelay) |
| 51 | + } else { |
| 52 | + value = global.setTimeout.function!(closure, millisecondsDelay) |
46 | 53 | }
|
47 | 54 | }
|
48 |
| - self.isRepeating = isRepeating |
49 |
| - if isRepeating { |
50 |
| - value = global.setInterval.function!(closure, millisecondsDelay) |
51 |
| - } else { |
52 |
| - value = global.setTimeout.function!(closure, millisecondsDelay) |
53 |
| - } |
54 |
| - } |
55 | 55 |
|
56 |
| - /** Makes a corresponding `clearTimeout` or `clearInterval` call, depending on whether this timer |
57 |
| - instance is repeating. The `closure` instance is released manually here, as it is required for |
58 |
| - bridged closure instances. |
59 |
| - */ |
60 |
| - deinit { |
61 |
| - if isRepeating { |
62 |
| - global.clearInterval.function!(value) |
63 |
| - } else { |
64 |
| - global.clearTimeout.function!(value) |
| 56 | + /** Makes a corresponding `clearTimeout` or `clearInterval` call, depending on whether this timer |
| 57 | + instance is repeating. The `closure` instance is released manually here, as it is required for |
| 58 | + bridged closure instances. |
| 59 | + */ |
| 60 | + deinit { |
| 61 | + if isRepeating { |
| 62 | + global.clearInterval.function!(value) |
| 63 | + } else { |
| 64 | + global.clearTimeout.function!(value) |
| 65 | + } |
| 66 | + closure.release() |
65 | 67 | }
|
66 |
| - closure.release() |
67 |
| - } |
68 | 68 | }
|
0 commit comments