-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswift-native-module.mustache
66 lines (54 loc) · 1.82 KB
/
swift-native-module.mustache
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
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// swiftformat:disable redundantRawValues
import Foundation
public protocol {{moduleName}}: EditorNativeModule {
{{#methods}}
func {{methodName}}({{parametersDeclaration}}{{#returnType}}{{#parametersDeclaration.length}}, {{/parametersDeclaration.length}}completion: @escaping (Result<{{returnType}}, Error>) -> Void{{/returnType}})
{{/methods}}
}
public extension {{moduleName}} {
var moduleBridge: NativeModuleBridge {
{{customTags.bridgeName}}(self)
}
}
class {{customTags.bridgeName}}: NativeModuleBridge {
static let name = "{{customTags.invokePath}}"
lazy var methodMap: [String: NativeMethod] = [
{{#methods}}
"{{methodName}}": {{methodName}},
{{/methods}}
]
private let instance: {{moduleName}}
private lazy var decoder = JSONDecoder()
init(_ instance: {{moduleName}}) {
self.instance = instance
}
{{#methods}}
private func {{methodName}}(parametersData: Data, completion: @escaping (Result<Encodable?, Error>) -> Void) {
{{#parameters.length}}
struct Parameters: Decodable {
{{#parameters}}
var {{name}}: {{type}}
{{/parameters}}
}
let parameters: Parameters
do {
parameters = try decoder.decode(Parameters.self, from: parametersData)
}
catch {
logAssertFail("Parameters of {{methodName}} are invalid: \(error)")
completion(.failure(NativeMethodError.invalidParameters(parametersData)))
return
}
{{/parameters.length}}
instance.{{methodName}}{{#parameters.length}}({{#parameters}}{{name}}: parameters.{{name}}{{^last}}, {{/last}}{{/parameters}}){{/parameters.length}}{{#returnType}} { result in
completion(result.toEncodable())
}{{/returnType}}
}
{{/methods}}
}
{{#associatedTypes}}
{{> swift-named-type}}
{{/associatedTypes}}