forked from swiftwasm/WebWorkerKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebWorkerCallDecoder.swift
More file actions
42 lines (32 loc) · 1.2 KB
/
WebWorkerCallDecoder.swift
File metadata and controls
42 lines (32 loc) · 1.2 KB
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
import JavaScriptKit
import Distributed
final public class WebWorkerCallDecoder: DistributedTargetInvocationDecoder {
enum Error: Swift.Error {
case notEnoughArguments(expected: Codable.Type)
}
public typealias SerializationRequirement = Codable
let decoder: JSValueDecoder
let envelope: RemoteCallEnvelope
var argumentsIterator: Array<JSValue>.Iterator
init(system: WebWorkerActorSystem, envelope: RemoteCallEnvelope) {
self.envelope = envelope
self.argumentsIterator = envelope.args.makeIterator()
let decoder = JSValueDecoder()
self.decoder = decoder
}
public func decodeGenericSubstitutions() throws -> [Any.Type] {
envelope.genericSubs.compactMap(_typeByName)
}
public func decodeNextArgument<Argument: Codable>() throws -> Argument {
guard let data = argumentsIterator.next() else {
throw Error.notEnoughArguments(expected: Argument.self)
}
return try decoder.decode(Argument.self, from: data)
}
public func decodeErrorType() throws -> Any.Type? {
nil // not encoded, ok
}
public func decodeReturnType() throws -> Any.Type? {
nil // not encoded, ok
}
}