JavaScriptKit Documentation

Class Java​Script​Event​Loop

@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:

  1. Make sure that your target depends on JavaScriptEventLoop in your Packages.swift:
.target(
    name: "JavaScriptKitExample",
    dependencies: [
        "JavaScriptKit",
        .product(name: "JavaScriptEventLoop", package: "JavaScriptKit")
    ]
)
  1. Add an explicit import in the code that executes *before you start using await and/or Task APIs (most likely in main.swift):
import JavaScriptEventLoop
  1. Run this function *before you start using await and/or Task APIs (again, most likely in main.swift):
JavaScriptEventLoop.installGlobalExecutor()
%69 JavaScriptEventLoop JavaScriptEventLoop @unchecked Sendable @unchecked Sendable JavaScriptEventLoop->@unchecked Sendable SerialExecutor SerialExecutor JavaScriptEventLoop->SerialExecutor

Conforms To

@unchecked Sendable
SerialExecutor

Properties

queue​Microtask

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

set​Timeout

public var setTimeout: @Sendable (Double, @escaping () -> Void) -> Void

A function that invokes a given closure after a specified number of milliseconds.

shared

public static let shared: JavaScriptEventLoop  

A singleton instance of the Executor

Methods

install​Global​Executor()

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)  

as​Unowned​Serial​Executor()

public func asUnownedSerialExecutor() -> UnownedSerialExecutor