@tokamakdev/platform-ios-simulator-x64 0.1.1-beta.114 → 0.1.1-beta.117
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,20 +8,18 @@ import Foundation
|
|
|
8
8
|
/// bytes, errors, non-finite numbers, and records whose keys include `#`.
|
|
9
9
|
struct TokamakBinding {
|
|
10
10
|
private let resources: TokamakResources
|
|
11
|
-
private let request:
|
|
11
|
+
private let request: [String: Any]
|
|
12
12
|
private let reply: TokamakPluginBytesReply
|
|
13
13
|
|
|
14
14
|
init(
|
|
15
15
|
_ payload: Data, resources: TokamakResources, reply: @escaping TokamakPluginBytesReply
|
|
16
|
-
) {
|
|
16
|
+
) throws(TokamakPluginError) {
|
|
17
|
+
guard let request = try decodePluginValue(payload) as? [String: Any] else {
|
|
18
|
+
throw typeError("A binding request is an object")
|
|
19
|
+
}
|
|
17
20
|
self.resources = resources
|
|
21
|
+
self.request = request
|
|
18
22
|
self.reply = reply
|
|
19
|
-
request = Result { () throws(TokamakPluginError) in
|
|
20
|
-
guard let request = try decodePluginValue(payload) as? [String: Any] else {
|
|
21
|
-
throw typeError("A binding request is an object")
|
|
22
|
-
}
|
|
23
|
-
return request
|
|
24
|
-
}
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
/// The rejection of a member the running OS version lacks.
|
|
@@ -30,24 +28,22 @@ struct TokamakBinding {
|
|
|
30
28
|
|
|
31
29
|
/// Argument `index` as JSON.
|
|
32
30
|
subscript(index: Int) -> Any {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return arguments.indices.contains(index) ? arguments[index] : NSNull()
|
|
36
|
-
}
|
|
31
|
+
let arguments = request["arguments"] as? [Any] ?? []
|
|
32
|
+
return arguments.indices.contains(index) ? arguments[index] : NSNull()
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
/// The object the call is made on.
|
|
40
36
|
func target<T>(_ type: T.Type) throws -> T {
|
|
41
|
-
try object(request
|
|
37
|
+
try object(request["target"] as Any, as: type)
|
|
42
38
|
}
|
|
43
39
|
|
|
44
40
|
/// The handlers an implementation calls.
|
|
45
41
|
var methods: Set<String> {
|
|
46
|
-
Set(
|
|
42
|
+
Set(request["methods"] as? [String] ?? [])
|
|
47
43
|
}
|
|
48
44
|
|
|
49
45
|
private var scope: String {
|
|
50
|
-
|
|
46
|
+
request["scope"] as? String ?? ""
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
// MARK: Decoding
|
|
@@ -175,10 +171,7 @@ struct TokamakBinding {
|
|
|
175
171
|
func encode(_ value: UUID) -> Any { value.uuidString }
|
|
176
172
|
|
|
177
173
|
func encode(_ value: any Error) -> Any {
|
|
178
|
-
|
|
179
|
-
var encoded: [String: Any] = ["#": "error", "name": error.name, "message": error.message]
|
|
180
|
-
encoded["cause"] = error.cause
|
|
181
|
-
return encoded
|
|
174
|
+
TokamakPluginError(value).json.merging(["#": "error"]) { $1 }
|
|
182
175
|
}
|
|
183
176
|
|
|
184
177
|
/// Keeps `value` in the invocation's scope and refers to it, with the
|
|
@@ -240,28 +233,12 @@ struct TokamakBinding {
|
|
|
240
233
|
return { [resources] in resources.end(scope) }
|
|
241
234
|
}
|
|
242
235
|
|
|
243
|
-
/// Rejects the member the plugin does not bind.
|
|
244
|
-
func unsupported(_ member: String) -> (() -> Void)? {
|
|
245
|
-
fail(TokamakPluginError.notSupported("\(member) is not supported"))
|
|
246
|
-
return nil
|
|
247
|
-
}
|
|
248
|
-
|
|
249
236
|
func send(_ value: Any?, done: Bool) {
|
|
250
237
|
reply(TokamakPluginResult(encodePluginValue(value), done: done))
|
|
251
238
|
}
|
|
252
239
|
|
|
253
240
|
private func fail(_ error: any Error) {
|
|
254
|
-
reply(TokamakPluginResult(.failure(
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/// `error` for the Worker: an `OperationError` whose cause has the
|
|
258
|
-
/// platform error's domain and code.
|
|
259
|
-
static func failure(_ error: any Error) -> TokamakPluginError {
|
|
260
|
-
if let error = error as? TokamakPluginError { return error }
|
|
261
|
-
let error = error as NSError
|
|
262
|
-
return TokamakPluginError(
|
|
263
|
-
name: "OperationError", message: error.localizedDescription,
|
|
264
|
-
cause: ["domain": error.domain, "code": error.code])
|
|
241
|
+
reply(TokamakPluginResult(.failure(TokamakPluginError(error)), done: true))
|
|
265
242
|
}
|
|
266
243
|
|
|
267
244
|
/// The field `key` of `value` tagged `tag`.
|
|
@@ -85,10 +85,7 @@ final class TokamakCorePlugin: TokamakPlugin {
|
|
|
85
85
|
"window.dispatchEvent(new MessageEvent('message', {data, origin: location.origin})); return true;",
|
|
86
86
|
arguments: ["data": data], in: nil, in: .page
|
|
87
87
|
) { result in
|
|
88
|
-
reply(
|
|
89
|
-
result.map { _ in nil }.mapError {
|
|
90
|
-
TokamakPluginError(name: "OperationError", message: $0.localizedDescription)
|
|
91
|
-
})
|
|
88
|
+
reply(result.map { _ in nil }.mapError { TokamakPluginError($0) })
|
|
92
89
|
}
|
|
93
90
|
}
|
|
94
91
|
|
|
@@ -27,6 +27,28 @@ struct TokamakPluginError: Error {
|
|
|
27
27
|
)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
extension TokamakPluginError {
|
|
31
|
+
/// `error` for the Worker: itself, or an `OperationError` whose cause has
|
|
32
|
+
/// the platform error's domain and code.
|
|
33
|
+
init(_ error: any Error) {
|
|
34
|
+
if let error = error as? Self {
|
|
35
|
+
self = error
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
let error = error as NSError
|
|
39
|
+
self.init(
|
|
40
|
+
name: "OperationError", message: error.localizedDescription,
|
|
41
|
+
cause: ["domain": error.domain, "code": error.code])
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// The error as JSON: its name, message and cause.
|
|
45
|
+
var json: [String: Any] {
|
|
46
|
+
var json: [String: Any] = ["name": name, "message": message]
|
|
47
|
+
json["cause"] = cause
|
|
48
|
+
return json
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
30
52
|
typealias TokamakPluginReply = (Result<Any?, TokamakPluginError>) -> Void
|
|
31
53
|
typealias TokamakPluginBytesReply = (TokamakPluginResult) -> Void
|
|
32
54
|
|
|
@@ -48,9 +70,7 @@ struct TokamakPluginResult {
|
|
|
48
70
|
self.payload = payload
|
|
49
71
|
failed = false
|
|
50
72
|
case .failure(let error):
|
|
51
|
-
|
|
52
|
-
failure["cause"] = error.cause
|
|
53
|
-
payload = try! JSONSerialization.data(withJSONObject: failure)
|
|
73
|
+
payload = try! JSONSerialization.data(withJSONObject: error.json)
|
|
54
74
|
failed = true
|
|
55
75
|
}
|
|
56
76
|
}
|
|
@@ -69,22 +89,6 @@ func encodePluginValue(_ value: Any?) -> Result<Data, TokamakPluginError> {
|
|
|
69
89
|
return .success(json)
|
|
70
90
|
}
|
|
71
91
|
|
|
72
|
-
/// Decodes `payload` as JSON for `call`, and encodes its reply.
|
|
73
|
-
private func callJSON(
|
|
74
|
-
_ payload: Data,
|
|
75
|
-
_ reply: @escaping TokamakPluginBytesReply,
|
|
76
|
-
_ call: (Any, @escaping TokamakPluginReply) -> (() -> Void)?
|
|
77
|
-
) -> (() -> Void)? {
|
|
78
|
-
do {
|
|
79
|
-
return call(try decodePluginValue(payload)) {
|
|
80
|
-
reply(TokamakPluginResult($0.flatMap(encodePluginValue), done: true))
|
|
81
|
-
}
|
|
82
|
-
} catch {
|
|
83
|
-
reply(TokamakPluginResult(.failure(error), done: true))
|
|
84
|
-
return nil
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
92
|
func decodePluginValue(_ payload: Data) throws(TokamakPluginError) -> Any {
|
|
89
93
|
do { return try JSONSerialization.jsonObject(with: payload, options: .fragmentsAllowed) } catch {
|
|
90
94
|
throw .init(name: "DataCloneError", message: error.localizedDescription)
|
|
@@ -195,13 +199,13 @@ protocol TokamakPlugin: AnyObject {
|
|
|
195
199
|
/// Serial executor for this operation and its cancellation.
|
|
196
200
|
func executor(for method: String) -> DispatchQueue
|
|
197
201
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
202
|
+
/// Throws when the call cannot start, which ends it.
|
|
203
|
+
func callBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
|
|
204
|
+
throws(TokamakPluginError) -> (() -> Void)?
|
|
201
205
|
func callBytes(
|
|
202
206
|
method: String, payload: Data, body: TokamakPluginBody,
|
|
203
207
|
reply: @escaping TokamakPluginBytesReply
|
|
204
|
-
) -> (() -> Void)?
|
|
208
|
+
) throws(TokamakPluginError) -> (() -> Void)?
|
|
205
209
|
func subscribeBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
|
|
206
210
|
throws(TokamakPluginError) -> (() -> Void)
|
|
207
211
|
|
|
@@ -244,17 +248,21 @@ protocol TokamakPlugin: AnyObject {
|
|
|
244
248
|
extension TokamakPlugin {
|
|
245
249
|
func executor(for method: String) -> DispatchQueue { .main }
|
|
246
250
|
|
|
247
|
-
func callBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
|
|
248
|
-
() -> Void
|
|
249
|
-
|
|
250
|
-
|
|
251
|
+
func callBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
|
|
252
|
+
throws(TokamakPluginError) -> (() -> Void)?
|
|
253
|
+
{
|
|
254
|
+
try call(method: method, arguments: decodePluginValue(payload)) {
|
|
255
|
+
reply(TokamakPluginResult($0.flatMap(encodePluginValue), done: true))
|
|
256
|
+
}
|
|
251
257
|
}
|
|
252
258
|
|
|
253
259
|
func callBytes(
|
|
254
260
|
method: String, payload: Data, body: TokamakPluginBody,
|
|
255
261
|
reply: @escaping TokamakPluginBytesReply
|
|
256
|
-
) -> (() -> Void)? {
|
|
257
|
-
|
|
262
|
+
) throws(TokamakPluginError) -> (() -> Void)? {
|
|
263
|
+
try call(method: method, arguments: decodePluginValue(payload), body: body) {
|
|
264
|
+
reply(TokamakPluginResult($0.flatMap(encodePluginValue), done: true))
|
|
265
|
+
}
|
|
258
266
|
}
|
|
259
267
|
|
|
260
268
|
func subscribeBytes(method: String, payload: Data, reply: @escaping TokamakPluginBytesReply)
|
|
@@ -408,10 +416,11 @@ final class TokamakPluginRequests<Key: Hashable> {
|
|
|
408
416
|
request.cancellation = try target.subscribeBytes(
|
|
409
417
|
method: method, payload: payload, reply: reply)
|
|
410
418
|
} else if let body = request.body {
|
|
411
|
-
request.cancellation = target.callBytes(
|
|
419
|
+
request.cancellation = try target.callBytes(
|
|
412
420
|
method: method, payload: payload, body: body, reply: reply)
|
|
413
421
|
} else {
|
|
414
|
-
request.cancellation = target.callBytes(
|
|
422
|
+
request.cancellation = try target.callBytes(
|
|
423
|
+
method: method, payload: payload, reply: reply)
|
|
415
424
|
}
|
|
416
425
|
} catch {
|
|
417
426
|
if remove(key, request) { send(key, TokamakPluginResult(.failure(error), done: true)) }
|