Skip to content

Commit 04c9e5e

Browse files
Following "Custom Main and Global Executors" Pitch 3
swiftlang/swift@6046286
1 parent 250af4d commit 04c9e5e

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

Sources/JavaScriptEventLoop/JavaScriptEventLoop+ExecutorFactory.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,37 @@ extension JavaScriptEventLoop: MainExecutor {
2727
extension JavaScriptEventLoop: TaskExecutor {}
2828

2929
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *)
30-
extension JavaScriptEventLoop: SchedulableExecutor {
30+
extension JavaScriptEventLoop: SchedulingExecutor {
3131
public func enqueue<C: Clock>(
3232
_ job: consuming ExecutorJob,
3333
after delay: C.Duration,
3434
tolerance: C.Duration?,
3535
clock: C
3636
) {
37-
let milliseconds = Self.delayInMilliseconds(from: delay, clock: clock)
37+
let duration: Duration
38+
// Handle clocks we know
39+
if let _ = clock as? ContinuousClock {
40+
duration = delay as! ContinuousClock.Duration
41+
} else if let _ = clock as? SuspendingClock {
42+
duration = delay as! SuspendingClock.Duration
43+
} else {
44+
// Hand-off the scheduling work to Clock implementation for unknown clocks
45+
clock.enqueue(
46+
job,
47+
on: self,
48+
at: clock.now.advanced(by: delay),
49+
tolerance: tolerance
50+
)
51+
return
52+
}
53+
let milliseconds = Self.delayInMilliseconds(from: duration)
3854
self.enqueue(
3955
UnownedJob(job),
4056
withDelay: milliseconds
4157
)
4258
}
4359

44-
private static func delayInMilliseconds<C: Clock>(from duration: C.Duration, clock: C) -> Double {
45-
let swiftDuration = clock.convert(from: duration)!
60+
private static func delayInMilliseconds(from swiftDuration: Duration) -> Double {
4661
let (seconds, attoseconds) = swiftDuration.components
4762
return Double(seconds) * 1_000 + (Double(attoseconds) / 1_000_000_000_000_000)
4863
}
@@ -52,7 +67,7 @@ extension JavaScriptEventLoop: SchedulableExecutor {
5267
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *)
5368
extension JavaScriptEventLoop: ExecutorFactory {
5469
// Forward all operations to the current thread's JavaScriptEventLoop instance
55-
final class CurrentThread: TaskExecutor, SchedulableExecutor, MainExecutor, SerialExecutor {
70+
final class CurrentThread: TaskExecutor, SchedulingExecutor, MainExecutor, SerialExecutor {
5671
func checkIsolated() {}
5772

5873
func enqueue(_ job: consuming ExecutorJob) {

0 commit comments

Comments
 (0)