Skip to content

Commit 2d5e1fa

Browse files
Use the new ExecutorFactory protocol to provide a default executor
1 parent 0896c7d commit 2d5e1fa

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *)
2+
extension JavaScriptEventLoop: MainExecutor {
3+
public func run() throws {}
4+
public func stop() {}
5+
}
6+
7+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
8+
extension JavaScriptEventLoop: TaskExecutor {}
9+
10+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *)
11+
extension JavaScriptEventLoop: SchedulableExecutor {
12+
public func enqueue<C: Clock>(
13+
_ job: consuming ExecutorJob,
14+
after delay: C.Duration,
15+
tolerance: C.Duration?,
16+
clock: C
17+
) {
18+
self.enqueue(
19+
UnownedJob(job),
20+
withDelay: Self.delayInMilliseconds(from: delay, clock: clock)
21+
)
22+
}
23+
24+
private static func delayInMilliseconds<C: Clock>(from duration: C.Duration, clock: C) -> UInt64 {
25+
let swiftDuration = clock.convert(from: duration)!
26+
let (seconds, attoseconds) = swiftDuration.components
27+
return UInt64(seconds) * 1_000 + (UInt64(attoseconds) / 1_000_000_000)
28+
}
29+
}
30+
31+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *)
32+
extension JavaScriptEventLoop: ExecutorFactory {
33+
public static var mainExecutor: any MainExecutor {
34+
JavaScriptEventLoop.shared
35+
}
36+
public static var defaultExecutor: any TaskExecutor {
37+
JavaScriptEventLoop.shared
38+
}
39+
}

Sources/JavaScriptEventLoop/JavaScriptEventLoop+LegacyHooks.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ extension JavaScriptEventLoop {
3232
UInt64, UnownedJob, swift_task_enqueueGlobalWithDelay_original
3333
) -> Void
3434
let swift_task_enqueueGlobalWithDelay_hook_impl: swift_task_enqueueGlobalWithDelay_hook_Fn = {
35-
delay,
35+
nanoseconds,
3636
job,
3737
original in
38-
JavaScriptEventLoop.shared.enqueue(job, withDelay: delay)
38+
let milliseconds = nanoseconds / 1_000_000
39+
JavaScriptEventLoop.shared.enqueue(job, withDelay: milliseconds)
3940
}
4041
swift_task_enqueueGlobalWithDelay_hook = unsafeBitCast(
4142
swift_task_enqueueGlobalWithDelay_hook_impl,

Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,19 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
120120

121121
@MainActor private static func installGlobalExecutorIsolated() {
122122
guard !didInstallGlobalExecutor else { return }
123+
#if compiler(>=5.2)
124+
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999, *) {
125+
_createExecutors(factory: JavaScriptEventLoop.self)
126+
} else {
127+
installByLegacyHook()
128+
}
129+
#else
123130
installByLegacyHook()
131+
#endif
124132
didInstallGlobalExecutor = true
125133
}
126134

127-
internal func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) {
128-
let milliseconds = nanoseconds / 1_000_000
135+
internal func enqueue(_ job: UnownedJob, withDelay milliseconds: UInt64) {
129136
setTimeout(
130137
Double(milliseconds),
131138
{

0 commit comments

Comments
 (0)