|
| 1 | +// Clock and host task scheduling for `JavaScriptEventLoop.yieldInterval`. |
| 2 | + |
| 3 | +import JavaScriptKit |
| 4 | +import _Concurrency |
| 5 | + |
| 6 | +#if compiler(>=5.5) |
| 7 | + |
| 8 | +@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) |
| 9 | +extension JavaScriptEventLoop { |
| 10 | + |
| 11 | + #if hasFeature(Embedded) |
| 12 | + // `ContinuousClock` is unavailable in Embedded Swift; use `performance.now()` in milliseconds. |
| 13 | + typealias DrainInstant = Double |
| 14 | + |
| 15 | + func drainClockNow() -> DrainInstant { |
| 16 | + if performanceNow == nil { |
| 17 | + let performance = JSObject.global.performance.object! |
| 18 | + performanceNow = (performance, performance.now.function!) |
| 19 | + } |
| 20 | + return performanceNow!.now(this: performanceNow!.performance).number! |
| 21 | + } |
| 22 | + |
| 23 | + func hasRun(since start: DrainInstant, for interval: Duration) -> Bool { |
| 24 | + let (seconds, attoseconds) = interval.components |
| 25 | + let intervalMilliseconds = Double(seconds) * 1_000 + Double(attoseconds) / 1_000_000_000_000_000 |
| 26 | + return drainClockNow() - start >= intervalMilliseconds |
| 27 | + } |
| 28 | + #else |
| 29 | + typealias DrainInstant = ContinuousClock.Instant |
| 30 | + |
| 31 | + func drainClockNow() -> DrainInstant { |
| 32 | + ContinuousClock.now |
| 33 | + } |
| 34 | + |
| 35 | + func hasRun(since start: DrainInstant, for interval: Duration) -> Bool { |
| 36 | + start.duration(to: .now) >= interval |
| 37 | + } |
| 38 | + #endif |
| 39 | + |
| 40 | + /// Runs `body` in a new JavaScript task via `setImmediate`, `MessageChannel`, or `setTimeout`. |
| 41 | + func queueHostTask(_ body: @escaping () -> Void) { |
| 42 | + if hostTaskScheduler == nil { |
| 43 | + hostTaskScheduler = Self.makeHostTaskScheduler() |
| 44 | + } |
| 45 | + hostTaskScheduler!(body) |
| 46 | + } |
| 47 | + |
| 48 | + private static func makeHostTaskScheduler() -> (@escaping () -> Void) -> Void { |
| 49 | + let global = JSObject.global |
| 50 | + if let setImmediate = global.setImmediate.function { |
| 51 | + return { body in |
| 52 | + _ = setImmediate( |
| 53 | + JSOneshotClosure { _ in |
| 54 | + body() |
| 55 | + return .undefined |
| 56 | + } |
| 57 | + ) |
| 58 | + } |
| 59 | + } |
| 60 | + if let messageChannel = global.MessageChannel.function { |
| 61 | + // Unlike nested `setTimeout` calls, messages are not clamped to 4 ms. |
| 62 | + let channel = messageChannel.new(arguments: [JSValue]()) |
| 63 | + let sender = channel.port1.object! |
| 64 | + let postMessage = sender.postMessage.function! |
| 65 | + let pending = PendingHostTasks() |
| 66 | + let receiver = JSClosure { _ in |
| 67 | + pending.runAll() |
| 68 | + return .undefined |
| 69 | + } |
| 70 | + channel.port2.object!.onmessage = .object(receiver) |
| 71 | + return { body in |
| 72 | + pending.bodies.append(body) |
| 73 | + // Keep `receiver` alive as long as the scheduler. |
| 74 | + withExtendedLifetime(receiver) { |
| 75 | + _ = postMessage(this: sender, JSValue.undefined) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + let setTimeout = global.setTimeout.function! |
| 80 | + return { body in |
| 81 | + _ = setTimeout( |
| 82 | + JSOneshotClosure { _ in |
| 83 | + body() |
| 84 | + return .undefined |
| 85 | + }, |
| 86 | + 0 |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/// Bodies waiting for the next `MessageChannel` message. |
| 93 | +private final class PendingHostTasks { |
| 94 | + var bodies: [() -> Void] = [] |
| 95 | + |
| 96 | + func runAll() { |
| 97 | + let bodies = self.bodies |
| 98 | + self.bodies = [] |
| 99 | + for body in bodies { |
| 100 | + body() |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#endif |
0 commit comments