Class
JavaScriptEventLoop
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable
Singleton type responsible for integrating JavaScript event loop as a Swift concurrency executor, conforming to
SerialExecutor
protocol from the standard library. To utilize it:
- Make sure that your target depends on
JavaScriptEventLoop
in yourPackages.swift
:
.target(
name: "JavaScriptKitExample",
dependencies: [
"JavaScriptKit",
.product(name: "JavaScriptEventLoop", package: "JavaScriptKit")
]
)
- Add an explicit import in the code that executes *before you start using
await
and/orTask
APIs (most likely inmain.swift
):
import JavaScriptEventLoop
- Run this function *before you start using
await
and/orTask
APIs (again, most likely inmain.swift
):
JavaScriptEventLoop.installGlobalExecutor()
Relationships
Conforms To
@unchecked Sendable
SerialExecutor
Properties
queueMicrotask
public var queueMicrotask: @Sendable (@escaping () -> Void) -> Void
A function that queues a given closure as a microtask into JavaScript event loop. See also: https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide
setTimeout
public var setTimeout: @Sendable (Double, @escaping () -> Void) -> Void
A function that invokes a given closure after a specified number of milliseconds.
Methods
installGlobalExecutor()
public static func installGlobalExecutor()
Set JavaScript event loop based executor to be the global executor Note that this should be called before any of the jobs are created. This installation step will be unnecessary after custom executor are introduced officially. See also a draft proposal for custom executors
enqueue(_:)
public func enqueue(_ job: UnownedJob)
asUnownedSerialExecutor()
public func asUnownedSerialExecutor() -> UnownedSerialExecutor