-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathWebWorkerDedicatedExecutor.swift
62 lines (55 loc) · 1.89 KB
/
WebWorkerDedicatedExecutor.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import JavaScriptKit
import _CJavaScriptEventLoop
#if canImport(Synchronization)
import Synchronization
#endif
#if canImport(wasi_pthread)
import wasi_pthread
import WASILibc
#endif
/// A serial executor that runs on a dedicated web worker thread.
///
/// This executor is useful for running actors on a dedicated web worker thread.
///
/// ## Usage
///
/// ```swift
/// actor MyActor {
/// let executor: WebWorkerDedicatedExecutor
/// nonisolated var unownedExecutor: UnownedSerialExecutor {
/// self.executor.asUnownedSerialExecutor()
/// }
/// init(executor: WebWorkerDedicatedExecutor) {
/// self.executor = executor
/// }
/// }
///
/// let executor = try await WebWorkerDedicatedExecutor()
/// let actor = MyActor(executor: executor)
/// ```
///
/// - SeeAlso: ``WebWorkerTaskExecutor``
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
public final class WebWorkerDedicatedExecutor: SerialExecutor {
private let underlying: WebWorkerTaskExecutor
/// - Parameters:
/// - timeout: The maximum time to wait for all worker threads to be started. Default is 3 seconds.
/// - checkInterval: The interval to check if all worker threads are started. Default is 5 microseconds.
/// - Throws: An error if any worker thread fails to initialize within the timeout period.
public init(timeout: Duration = .seconds(3), checkInterval: Duration = .microseconds(5)) async throws {
let underlying = try await WebWorkerTaskExecutor(
numberOfThreads: 1,
timeout: timeout,
checkInterval: checkInterval
)
self.underlying = underlying
}
/// Terminates the worker thread.
public func terminate() {
self.underlying.terminate()
}
// MARK: - SerialExecutor conformance
public func enqueue(_ job: consuming ExecutorJob) {
self.underlying.enqueue(job)
}
}