Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async closure support #159

Merged
merged 15 commits into from
Jun 5, 2022
Prev Previous commit
Next Next commit
Async JSOneshotClosure
  • Loading branch information
j-f1 committed Jan 28, 2022
commit 86ab3551f696b710bacb15c95d7e48413eb55953
42 changes: 26 additions & 16 deletions Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
})
}

#if compiler(>=5.5)
static func async(_ body: @escaping ([JSValue]) async throws -> JSValue) -> JSOneshotClosure {
JSOneshotClosure(makeAsyncClosure(body))
}
#endif

/// Release this function resource.
/// After calling `release`, calling this function from JavaScript will fail.
public func release() {
Expand Down Expand Up @@ -86,22 +92,7 @@ public class JSClosure: JSObject, JSClosureProtocol {

#if compiler(>=5.5)
static func async(_ body: @escaping ([JSValue]) async throws -> JSValue) -> JSClosure {
JSClosure { arguments in
JSPromise { resolver in
Task {
do {
let result = try await body(arguments)
resolver(.success(result))
} catch {
if let jsError = error as? JSError {
resolver(.failure(jsError.jsValue()))
} else {
resolver(.failure(JSError(message: String(describing: error)).jsValue()))
}
}
}
}.jsValue()
}
JSClosure(makeAsyncClosure(body))
}
#endif

Expand All @@ -114,6 +105,25 @@ public class JSClosure: JSObject, JSClosureProtocol {
#endif
}

private func makeAsyncClosure(_ body: @escaping ([JSValue]) async throws -> JSValue) -> (([JSValue]) -> JSValue) {
{ arguments in
JSPromise { resolver in
Task {
do {
let result = try await body(arguments)
resolver(.success(result))
} catch {
if let jsError = error as? JSError {
resolver(.failure(jsError.jsValue()))
} else {
resolver(.failure(JSError(message: String(describing: error)).jsValue()))
}
}
}
}.jsValue()
}
}


// MARK: - `JSClosure` mechanism note
//
Expand Down