Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
87 lines (77 loc) · 3.18 KB

File metadata and controls

87 lines (77 loc) · 3.18 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import JavaScriptKit
/// A `JSFunction` wrapper that enables async-function calls.
/// Exceptions produced by JavaScript functions will be thrown as `JSValue`.
///
/// ```swift
/// let fetch = JSObject.global.fetch.function!.async
/// let result = await try! fetch("https://api.github.com/zen")
/// ```
public class JSAsyncFunction {
private let base: JSFunction
public init(_ base: JSFunction) {
self.base = base
}
/// Call this function with given `arguments` and binding given `this` as context.
/// - Parameters:
/// - this: The value to be passed as the `this` parameter to this function.
/// - arguments: Arguments to be passed to this function.
/// - Returns: The result of this call.
@discardableResult
public func callAsFunction(this: JSObject? = nil, arguments: [ConvertibleToJSValue]) async throws -> JSValue {
let result = base.callAsFunction(this: this, arguments: arguments)
guard let object = result.object, let promise = JSPromise(object) else {
fatalError("'\(result)' should be Promise object")
}
return await try promise.await()
}
/// A variadic arguments version of `callAsFunction`.
@discardableResult
public func callAsFunction(this: JSObject? = nil, _ arguments: ConvertibleToJSValue...) async throws -> JSValue {
await try callAsFunction(this: this, arguments: arguments)
}
}
public extension JSFunction {
/// A modifier to call this function as a async function
///
/// ```swift
/// let fetch = JSObject.global.fetch.function!.async
/// let result = await try! fetch("https://api.github.com/zen")
/// ```
var `async`: JSAsyncFunction {
JSAsyncFunction(self)
}
}
/// A `JSObject` wrapper that enables async method calls capturing `this`.
/// Exceptions produced by JavaScript functions will be thrown as `JSValue`.
@dynamicMemberLookup
public class JSAsyncingObject {
private let base: JSObject
public init(_ base: JSObject) {
self.base = base
}
/// Returns the `name` member method binding this object as `this` context.
/// - Parameter name: The name of this object's member to access.
/// - Returns: The `name` member method binding this object as `this` context.
public subscript(_ name: String) -> ((ConvertibleToJSValue...) async throws -> JSValue)? {
guard let function = base[name].function?.async else { return nil }
return { [base] (arguments: ConvertibleToJSValue...) in
await try function(this: base, arguments: arguments)
}
}
/// A convenience method of `subscript(_ name: String) -> ((ConvertibleToJSValue...) throws -> JSValue)?`
/// to access the member through Dynamic Member Lookup.
public subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) async throws -> JSValue)? {
self[name]
}
}
public extension JSObject {
/// A modifier to call methods as async methods capturing `this`
///
/// ```swift
/// let fetch = JSObject.global.fetch.function!.async
/// let result = await try! fetch("https://api.github.com/zen")
/// ```
var asyncing: JSAsyncingObject {
JSAsyncingObject(self)
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.