Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ac7745

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

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Sources/JavaScriptEventLoop/JavaScriptEventLoop+ExecutorFactory.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,33 @@ 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(job, on: self, at: clock.now.advanced(by: delay),
46+
tolerance: tolerance)
47+
return
48+
}
49+
let milliseconds = Self.delayInMilliseconds(from: duration)
3850
self.enqueue(
3951
UnownedJob(job),
4052
withDelay: milliseconds
4153
)
4254
}
4355

44-
private static func delayInMilliseconds<C: Clock>(from duration: C.Duration, clock: C) -> Double {
45-
let swiftDuration = clock.convert(from: duration)!
56+
private static func delayInMilliseconds(from swiftDuration: Duration) -> Double {
4657
let (seconds, attoseconds) = swiftDuration.components
4758
return Double(seconds) * 1_000 + (Double(attoseconds) / 1_000_000_000_000_000)
4859
}
@@ -52,7 +63,7 @@ extension JavaScriptEventLoop: SchedulableExecutor {
5263
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *)
5364
extension JavaScriptEventLoop: ExecutorFactory {
5465
// Forward all operations to the current thread's JavaScriptEventLoop instance
55-
final class CurrentThread: TaskExecutor, SchedulableExecutor, MainExecutor, SerialExecutor {
66+
final class CurrentThread: TaskExecutor, SchedulingExecutor, MainExecutor, SerialExecutor {
5667
func checkIsolated() {}
5768

5869
func enqueue(_ job: consuming ExecutorJob) {

0 commit comments

Comments
 (0)