Skip to content

Commit fa77908

Browse files
Concurrency: Remove @Sendable requirement from scheduling primitives
They are accessed from a single thread, so there is no need to enforce `@Sendable` requirement on them. And also the following code is not working with `@Sendable` requirement because the captured `JSPromise` is not `Sendable`. ``` let promise = JSPromise(resolver: { resolver -> Void in resolver(.success(.undefined)) }) let setTimeout = JSObject.global.setTimeout.function! let eventLoop = JavaScriptEventLoop( queueTask: { job in // TODO(katei): Should prefer `queueMicrotask` if available? // We should measure if there is performance advantage. promise.then { _ in job() return JSValue.undefined } }, setTimeout: { delay, job in setTimeout(JSOneshotClosure { _ in job() return JSValue.undefined }, delay) } ) ```
1 parent 9f0197d commit fa77908

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
4040

4141
/// A function that queues a given closure as a microtask into JavaScript event loop.
4242
/// See also: https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide
43-
public var queueMicrotask: @Sendable (@escaping () -> Void) -> Void
43+
public var queueMicrotask: (@escaping () -> Void) -> Void
4444
/// A function that invokes a given closure after a specified number of milliseconds.
45-
public var setTimeout: @Sendable (Double, @escaping () -> Void) -> Void
45+
public var setTimeout: (Double, @escaping () -> Void) -> Void
4646

4747
/// A mutable state to manage internal job queue
4848
/// Note that this should be guarded atomically when supporting multi-threaded environment.
4949
var queueState = QueueState()
5050

5151
private init(
52-
queueTask: @Sendable @escaping (@escaping () -> Void) -> Void,
53-
setTimeout: @Sendable @escaping (Double, @escaping () -> Void) -> Void
52+
queueTask: @escaping (@escaping () -> Void) -> Void,
53+
setTimeout: @escaping (Double, @escaping () -> Void) -> Void
5454
) {
5555
self.queueMicrotask = queueTask
5656
self.setTimeout = setTimeout

0 commit comments

Comments
 (0)