@gnolang/gnonative 0.1.2
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.
- package/.eslintrc.js +5 -0
- package/.prettierrc.js +12 -0
- package/Makefile +62 -0
- package/README.md +118 -0
- package/android/build.gradle +93 -0
- package/android/libs/gnocore-sources.jar +0 -0
- package/android/libs/gnocore.aar +0 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/land/gno/gnonative/GnonativeModule.kt +150 -0
- package/android/src/main/java/land/gno/gnonative/GnonativeView.kt +7 -0
- package/android/src/main/java/land/gno/gnonative/GoBridgeError.kt +6 -0
- package/android/src/main/java/land/gno/gnonative/JavaPromiseBlock.kt +42 -0
- package/build/Gnonative.types.d.ts +7 -0
- package/build/Gnonative.types.d.ts.map +1 -0
- package/build/Gnonative.types.js +2 -0
- package/build/Gnonative.types.js.map +1 -0
- package/build/GnonativeModule.d.ts +3 -0
- package/build/GnonativeModule.d.ts.map +1 -0
- package/build/GnonativeModule.js +5 -0
- package/build/GnonativeModule.js.map +1 -0
- package/build/GnonativeModule.web.d.ts +7 -0
- package/build/GnonativeModule.web.d.ts.map +1 -0
- package/build/GnonativeModule.web.js +12 -0
- package/build/GnonativeModule.web.js.map +1 -0
- package/build/GnonativeView.d.ts +4 -0
- package/build/GnonativeView.d.ts.map +1 -0
- package/build/GnonativeView.js +7 -0
- package/build/GnonativeView.js.map +1 -0
- package/build/GnonativeView.web.d.ts +4 -0
- package/build/GnonativeView.web.d.ts.map +1 -0
- package/build/GnonativeView.web.js +7 -0
- package/build/GnonativeView.web.js.map +1 -0
- package/build/GoBridge.d.ts +12 -0
- package/build/GoBridge.d.ts.map +1 -0
- package/build/GoBridge.js +27 -0
- package/build/GoBridge.js.map +1 -0
- package/build/grpc/client.d.ts +4 -0
- package/build/grpc/client.d.ts.map +1 -0
- package/build/grpc/client.js +12 -0
- package/build/grpc/client.js.map +1 -0
- package/build/grpc/error.d.ts +15 -0
- package/build/grpc/error.d.ts.map +1 -0
- package/build/grpc/error.js +70 -0
- package/build/grpc/error.js.map +1 -0
- package/build/grpc/transport_native.d.ts +4 -0
- package/build/grpc/transport_native.d.ts.map +1 -0
- package/build/grpc/transport_native.js +140 -0
- package/build/grpc/transport_native.js.map +1 -0
- package/build/hooks/types.d.ts +17 -0
- package/build/hooks/types.d.ts.map +1 -0
- package/build/hooks/types.js +2 -0
- package/build/hooks/types.js.map +1 -0
- package/build/hooks/use-gno.d.ts +35 -0
- package/build/hooks/use-gno.d.ts.map +1 -0
- package/build/hooks/use-gno.js +248 -0
- package/build/hooks/use-gno.js.map +1 -0
- package/build/index.d.ts +9 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +13 -0
- package/build/index.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/Frameworks/GnoCore.xcframework/Info.plist +44 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/A/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64/GnoCore.framework/Versions/Current/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/A/Resources/Info.plist +6 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/GnoCore +0 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/GnoCore.h +13 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/GnoGnonative.objc.h +93 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/Universe.objc.h +29 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Headers/ref.h +35 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Modules/module.modulemap +8 -0
- package/ios/Frameworks/GnoCore.xcframework/ios-arm64_x86_64-simulator/GnoCore.framework/Versions/Current/Resources/Info.plist +6 -0
- package/ios/GnoError.swift +31 -0
- package/ios/Gnonative.podspec +28 -0
- package/ios/GnonativeModule.swift +186 -0
- package/ios/GnonativeView.swift +7 -0
- package/ios/PromiseBlock.swift +45 -0
- package/package.json +50 -0
- package/src/Gnonative.types.ts +7 -0
- package/src/GnonativeModule.ts +5 -0
- package/src/GnonativeModule.web.ts +13 -0
- package/src/GnonativeView.tsx +11 -0
- package/src/GnonativeView.web.tsx +11 -0
- package/src/GoBridge.ts +44 -0
- package/src/grpc/client.ts +14 -0
- package/src/grpc/error.ts +89 -0
- package/src/grpc/transport_native.ts +197 -0
- package/src/hooks/types.ts +19 -0
- package/src/hooks/use-gno.ts +423 -0
- package/src/index.ts +20 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
|
|
3
|
+
// Redefine GnoError with more context.
|
|
4
|
+
open class GnoError: Exception {
|
|
5
|
+
public enum ErrorCase {
|
|
6
|
+
case alreadyStarted
|
|
7
|
+
case createConfig
|
|
8
|
+
case notStarted
|
|
9
|
+
case coreError(NSError)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
private var errorDescription: String
|
|
13
|
+
|
|
14
|
+
public init(_ error: ErrorCase, file: String = #fileID, line: UInt = #line, function: String = #function) {
|
|
15
|
+
switch error {
|
|
16
|
+
case .alreadyStarted:
|
|
17
|
+
self.errorDescription = "Service is already started"
|
|
18
|
+
case .createConfig:
|
|
19
|
+
self.errorDescription = "unable to create config"
|
|
20
|
+
case .notStarted:
|
|
21
|
+
self.errorDescription = "Service hasn't started yet"
|
|
22
|
+
case .coreError(let error):
|
|
23
|
+
self.errorDescription = error.localizedDescription
|
|
24
|
+
}
|
|
25
|
+
super.init(file: file, line: line, function: function)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
open override var reason: String {
|
|
29
|
+
return self.errorDescription
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, '..', 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'Gnonative'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.description = package['description']
|
|
10
|
+
s.license = package['license']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.homepage = package['homepage']
|
|
13
|
+
s.platforms = { :ios => '13.4', :tvos => '13.4' }
|
|
14
|
+
s.swift_version = '5.4'
|
|
15
|
+
s.source = { git: 'https://github.com/gnolang/gnonative' }
|
|
16
|
+
s.static_framework = true
|
|
17
|
+
|
|
18
|
+
s.dependency 'ExpoModulesCore'
|
|
19
|
+
s.libraries = 'resolv'
|
|
20
|
+
s.vendored_frameworks = "Frameworks/GnoCore.xcframework"
|
|
21
|
+
s.source_files = "**/*.{h,m,mm,swift}"
|
|
22
|
+
s.private_header_files = "Frameworks/GnoCore.xcframework/**/*.h"
|
|
23
|
+
# Swift/Objective-C compatibility
|
|
24
|
+
s.pod_target_xcconfig = {
|
|
25
|
+
'DEFINES_MODULE' => 'YES',
|
|
26
|
+
'SWIFT_COMPILATION_MODE' => 'wholemodule'
|
|
27
|
+
}
|
|
28
|
+
end
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import ExpoModulesCore
|
|
2
|
+
import GnoCore
|
|
3
|
+
|
|
4
|
+
public class GnonativeModule: Module {
|
|
5
|
+
// Each module class must implement the definition function. The definition consists of components
|
|
6
|
+
// that describes the module's functionality and behavior.
|
|
7
|
+
// See https://docs.expo.dev/modules/module-api for more details about available components.
|
|
8
|
+
var logger: Logger = Logger(
|
|
9
|
+
category: String(describing: "GnoNative")
|
|
10
|
+
)
|
|
11
|
+
var appRootDir: String?
|
|
12
|
+
var tmpDir: String?
|
|
13
|
+
var bridge: GnoGnonativeBridge?
|
|
14
|
+
var socketPort: Int = 0
|
|
15
|
+
|
|
16
|
+
public func definition() -> ModuleDefinition {
|
|
17
|
+
// Sets the name of the module that JavaScript code will use to refer to the module. Takes a string as an argument.
|
|
18
|
+
// Can be inferred from module's class name, but it's recommended to set it explicitly for clarity.
|
|
19
|
+
// The module will be accessible from `requireNativeModule('Gnonative')` in JavaScript.
|
|
20
|
+
Name("Gnonative")
|
|
21
|
+
|
|
22
|
+
// Sets constant properties on the module. Can take a dictionary or a closure that returns a dictionary.
|
|
23
|
+
Constants([
|
|
24
|
+
"PI": Double.pi
|
|
25
|
+
])
|
|
26
|
+
|
|
27
|
+
OnCreate {
|
|
28
|
+
do {
|
|
29
|
+
self.appRootDir = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true).path
|
|
30
|
+
} catch let error as NSError {
|
|
31
|
+
self.logger.error("\(String(describing: error.code))")
|
|
32
|
+
}
|
|
33
|
+
self.tmpDir = FileManager.default.compatTemporaryDirectory.path
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
OnDestroy {
|
|
37
|
+
do {
|
|
38
|
+
if self.bridge != nil {
|
|
39
|
+
try self.bridge?.close()
|
|
40
|
+
self.bridge = nil
|
|
41
|
+
}
|
|
42
|
+
} catch let error as NSError {
|
|
43
|
+
self.logger.error("\(String(describing: error.code))")
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Defines event names that the module can send to JavaScript.
|
|
48
|
+
Events("onChange")
|
|
49
|
+
|
|
50
|
+
AsyncFunction("initBridge") { (promise: Promise) in
|
|
51
|
+
var err: NSError?
|
|
52
|
+
|
|
53
|
+
do {
|
|
54
|
+
if self.bridge != nil {
|
|
55
|
+
throw GnoError(.alreadyStarted)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// init the bridge service
|
|
59
|
+
|
|
60
|
+
guard let config = GnoGnonativeBridgeConfig() else {
|
|
61
|
+
throw GnoError(.createConfig)
|
|
62
|
+
}
|
|
63
|
+
config.rootDir = self.appRootDir!
|
|
64
|
+
config.tmpDir = self.tmpDir!
|
|
65
|
+
|
|
66
|
+
// On simulator we can't create an UDS, see comment below
|
|
67
|
+
#if targetEnvironment(simulator)
|
|
68
|
+
config.useTcpListener = true
|
|
69
|
+
config.disableUdsListener = true
|
|
70
|
+
#endif
|
|
71
|
+
|
|
72
|
+
let bridge = GnoGnonativeNewBridge(config, &err);
|
|
73
|
+
if err != nil {
|
|
74
|
+
throw err!
|
|
75
|
+
}
|
|
76
|
+
self.bridge = bridge
|
|
77
|
+
|
|
78
|
+
promise.resolve(true)
|
|
79
|
+
} catch let error {
|
|
80
|
+
promise.reject(error)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
AsyncFunction("getTcpPort") { (promise: Promise) in
|
|
85
|
+
do {
|
|
86
|
+
guard let service = self.bridge else {
|
|
87
|
+
throw GnoError(.notStarted)
|
|
88
|
+
}
|
|
89
|
+
self.socketPort = service.getTcpPort()
|
|
90
|
+
self.logger.info("gRPC server port: \(self.socketPort)")
|
|
91
|
+
promise.resolve(self.socketPort)
|
|
92
|
+
} catch let error {
|
|
93
|
+
promise.reject(error)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
AsyncFunction("closeBridge") { (promise: Promise) in
|
|
98
|
+
do {
|
|
99
|
+
guard let service = self.bridge else {
|
|
100
|
+
throw GnoError(.notStarted)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try service.close()
|
|
104
|
+
self.bridge = nil
|
|
105
|
+
} catch let error {
|
|
106
|
+
promise.reject(error)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
AsyncFunction("invokeGrpcMethod") { (method: String, jsonMessage: String, promise: Promise) in
|
|
111
|
+
do {
|
|
112
|
+
guard let service = self.bridge else {
|
|
113
|
+
throw GnoError(.notStarted)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let block = PromiseBlock(promise: promise)
|
|
117
|
+
service.invokeGrpcMethod(with: block, method: method as String, jsonMessage: jsonMessage as String)
|
|
118
|
+
} catch let err {
|
|
119
|
+
promise.reject(err)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
AsyncFunction("createStreamClient") { (method: String, jsonMessage: String, promise: Promise) in
|
|
124
|
+
do {
|
|
125
|
+
guard let service = self.bridge else {
|
|
126
|
+
throw GnoError(.notStarted)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let block = PromiseBlock(promise: promise)
|
|
130
|
+
service.createStreamClient(with: block, method: method as String, jsonMessage: jsonMessage as String)
|
|
131
|
+
} catch let err {
|
|
132
|
+
promise.reject(err)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
AsyncFunction("streamClientReceive") { (id: String, promise: Promise) in
|
|
137
|
+
do {
|
|
138
|
+
guard let service = self.bridge else {
|
|
139
|
+
throw GnoError(.notStarted)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let block = PromiseBlock(promise: promise)
|
|
143
|
+
service.streamClientReceive(with: block, id_: id as String)
|
|
144
|
+
} catch let err {
|
|
145
|
+
promise.reject(err)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
AsyncFunction("closeStreamClient") { (id: String, promise: Promise) in
|
|
150
|
+
do {
|
|
151
|
+
guard let service = self.bridge else {
|
|
152
|
+
throw GnoError(.notStarted)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let block = PromiseBlock(promise: promise)
|
|
156
|
+
service.closeStreamClient(with: block, id_: id as String)
|
|
157
|
+
} catch let err {
|
|
158
|
+
promise.reject(err)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Enables the module to be used as a native view. Definition components that are accepted as part of the
|
|
163
|
+
// view definition: Prop, Events.
|
|
164
|
+
View(GnonativeView.self) {
|
|
165
|
+
// Defines a setter for the `name` prop.
|
|
166
|
+
Prop("name") { (view: GnonativeView, prop: String) in
|
|
167
|
+
print(prop)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
extension FileManager {
|
|
174
|
+
public var compatTemporaryDirectory: URL {
|
|
175
|
+
if #available(iOS 10.0, *) {
|
|
176
|
+
return temporaryDirectory
|
|
177
|
+
} else {
|
|
178
|
+
return (try? url(
|
|
179
|
+
for: .itemReplacementDirectory,
|
|
180
|
+
in: .userDomainMask,
|
|
181
|
+
appropriateFor: nil,
|
|
182
|
+
create: true)
|
|
183
|
+
) ?? URL(fileURLWithPath: NSTemporaryDirectory())
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//
|
|
2
|
+
// PromiseBlock.swift
|
|
3
|
+
// GnoExpo
|
|
4
|
+
//
|
|
5
|
+
// Created by Guilhem Fanton on 10/07/2023.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import ExpoModulesCore
|
|
9
|
+
import GnoCore
|
|
10
|
+
|
|
11
|
+
var promises = Set<PromiseBlock>()
|
|
12
|
+
|
|
13
|
+
// PromiseBlock aim to keep reference over promise object so go can play with
|
|
14
|
+
// until the promise is resolved
|
|
15
|
+
class PromiseBlock: NSObject, GnoGnonativePromiseBlockProtocol {
|
|
16
|
+
static func == (lhs: PromiseBlock, rhs: PromiseBlock) -> Bool {
|
|
17
|
+
return lhs === rhs
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
var promise: Promise
|
|
21
|
+
|
|
22
|
+
init(promise: Promise) {
|
|
23
|
+
self.promise = promise
|
|
24
|
+
super.init()
|
|
25
|
+
self.store()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func callResolve(_ reply: String?) {
|
|
29
|
+
self.promise.resolve(reply ?? "")
|
|
30
|
+
self.remove() // cleanup the promise
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
func callReject(_ error: Error?) {
|
|
34
|
+
self.promise.reject(error ?? Exception(name: "Unknown Error", description: "unknown reject error"))
|
|
35
|
+
self.remove() // cleanup the promise
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func store() {
|
|
39
|
+
promises.insert(self)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func remove() {
|
|
43
|
+
promises.remove(self)
|
|
44
|
+
}
|
|
45
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gnolang/gnonative",
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Develop for Gno using your app's native language ",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"types": "build/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "expo-module build",
|
|
9
|
+
"clean": "expo-module clean",
|
|
10
|
+
"lint": "expo-module lint",
|
|
11
|
+
"test": "expo-module test",
|
|
12
|
+
"prepare": "expo-module prepare",
|
|
13
|
+
"prepublishOnly": "expo-module prepublishOnly",
|
|
14
|
+
"expo-module": "expo-module",
|
|
15
|
+
"open:ios": "open -a \"Xcode\" example/ios",
|
|
16
|
+
"open:android": "open -a \"Android Studio\" example/android"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react-native",
|
|
20
|
+
"expo",
|
|
21
|
+
"gnonative",
|
|
22
|
+
"Gnonative"
|
|
23
|
+
],
|
|
24
|
+
"repository": "https://github.com/gnolang/gnonative",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/gnolang/gnonative/issues"
|
|
27
|
+
},
|
|
28
|
+
"author": "D4ryl00 <d4ryl00@gmail.com> (https://github.com/D4ryl00)",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"homepage": "https://github.com/gnolang/gnonative#readme",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@buf/gnolang_gnonative.bufbuild_es": "^1.8.0-20240327143536-1ec48ff39f30.2",
|
|
33
|
+
"@buf/gnolang_gnonative.connectrpc_es": "^1.4.0-20240327143536-1ec48ff39f30.2",
|
|
34
|
+
"@bufbuild/protobuf": "^1.8.0",
|
|
35
|
+
"@connectrpc/connect": "^1.4.0",
|
|
36
|
+
"@connectrpc/connect-web": "^1.4.0",
|
|
37
|
+
"fast-text-encoding": "^1.0.6",
|
|
38
|
+
"js-base64": "^3.7.7"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/react": "^18.0.25",
|
|
42
|
+
"expo-module-scripts": "^3.4.1",
|
|
43
|
+
"expo-modules-core": "~1.11.12"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"expo": "*",
|
|
47
|
+
"react": "*",
|
|
48
|
+
"react-native": "*"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EventEmitter } from 'expo-modules-core';
|
|
2
|
+
|
|
3
|
+
const emitter = new EventEmitter({} as any);
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
PI: Math.PI,
|
|
7
|
+
async setValueAsync(value: string): Promise<void> {
|
|
8
|
+
emitter.emit('onChange', { value });
|
|
9
|
+
},
|
|
10
|
+
hello() {
|
|
11
|
+
return 'Hello world! 👋';
|
|
12
|
+
},
|
|
13
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { requireNativeViewManager } from 'expo-modules-core';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
|
|
4
|
+
import { GnonativeViewProps } from './Gnonative.types';
|
|
5
|
+
|
|
6
|
+
const NativeView: React.ComponentType<GnonativeViewProps> =
|
|
7
|
+
requireNativeViewManager('Gnonative');
|
|
8
|
+
|
|
9
|
+
export default function GnonativeView(props: GnonativeViewProps) {
|
|
10
|
+
return <NativeView {...props} />;
|
|
11
|
+
}
|
package/src/GoBridge.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import GnonativeModule from './GnonativeModule';
|
|
2
|
+
|
|
3
|
+
export interface GoBridgeInterface {
|
|
4
|
+
initBridge(): Promise<void>;
|
|
5
|
+
closeBridge(): Promise<void>;
|
|
6
|
+
getTcpPort(): Promise<number>;
|
|
7
|
+
invokeGrpcMethod(method: string, jsonMessage: string): Promise<string>;
|
|
8
|
+
createStreamClient(method: string, jsonMessage: string): Promise<string>;
|
|
9
|
+
streamClientReceive(id: string): Promise<string>;
|
|
10
|
+
closeStreamClient(id: string): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
class GoBridge implements GoBridgeInterface {
|
|
14
|
+
initBridge(): Promise<void> {
|
|
15
|
+
return GnonativeModule.initBridge();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
closeBridge(): Promise<void> {
|
|
19
|
+
return GnonativeModule.closeBridge();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getTcpPort(): Promise<number> {
|
|
23
|
+
return GnonativeModule.getTcpPort();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
invokeGrpcMethod(method: string, jsonMessage: string): Promise<string> {
|
|
27
|
+
return GnonativeModule.invokeGrpcMethod(method, jsonMessage);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
createStreamClient(method: string, jsonMessage: string): Promise<string> {
|
|
31
|
+
return GnonativeModule.createStreamClient(method, jsonMessage);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
streamClientReceive(id: string): Promise<string> {
|
|
35
|
+
return GnonativeModule.streamClientReceive(id);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
closeStreamClient(id: string): Promise<void> {
|
|
39
|
+
return GnonativeModule.closeStreamClient(id);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const goBridge: GoBridgeInterface = new GoBridge();
|
|
44
|
+
export { goBridge as GoBridge };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { GnoNativeService } from '@buf/gnolang_gnonative.connectrpc_es/rpc_connect';
|
|
2
|
+
import { createPromiseClient, PromiseClient } from '@connectrpc/connect';
|
|
3
|
+
|
|
4
|
+
import { createNativeGrpcTransport } from './transport_native';
|
|
5
|
+
|
|
6
|
+
// Create a GnoNativeService client
|
|
7
|
+
export function createClient(_port: number): PromiseClient<typeof GnoNativeService> {
|
|
8
|
+
const transport = createNativeGrpcTransport({
|
|
9
|
+
baseUrl: '',
|
|
10
|
+
useBinaryFormat: false,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
return createPromiseClient(GnoNativeService, transport);
|
|
14
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { ErrCode } from '@buf/gnolang_gnonative.bufbuild_es/rpc_pb';
|
|
2
|
+
import { Code, ConnectError } from '@connectrpc/connect';
|
|
3
|
+
|
|
4
|
+
class GRPCError extends Error {
|
|
5
|
+
public GrpcCode: Code;
|
|
6
|
+
|
|
7
|
+
public error: ConnectError;
|
|
8
|
+
|
|
9
|
+
constructor(e: ConnectError | null | undefined) {
|
|
10
|
+
if (!e) {
|
|
11
|
+
// this should not happen, but should not break the app either.
|
|
12
|
+
// instead simply create a empty error and warn about this
|
|
13
|
+
console.warn(`GRPCError: (${e}) grpc error provided, empty error returned`);
|
|
14
|
+
e = new ConnectError('');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
super(e.rawMessage);
|
|
18
|
+
|
|
19
|
+
this.error = e;
|
|
20
|
+
this.GrpcCode = e.code;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private extractErrCode(match: RegExpMatchArray | null) {
|
|
24
|
+
if (match === null || match.length < 2) {
|
|
25
|
+
return ErrCode.Undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const code = parseInt(match[1]);
|
|
29
|
+
if (typeof code !== 'number') {
|
|
30
|
+
return ErrCode.Undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return code;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// errCodes parses the error message formatted like `ErrType(#ErrNumber): WrappedErrType(#WrappedErrNumber)`
|
|
37
|
+
// and returns the corresponding ErrCodes or [ErrCode.Undefined] if some errors occur.
|
|
38
|
+
private errCodes(): ErrCode[] {
|
|
39
|
+
const errCodes: ErrCode[] = [];
|
|
40
|
+
|
|
41
|
+
if (this.message === '') {
|
|
42
|
+
return [ErrCode.Undefined];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const matches = this.message.matchAll(/\w+\(#(\d+)\)/g);
|
|
46
|
+
|
|
47
|
+
for (const match of matches) {
|
|
48
|
+
const code = this.extractErrCode(match);
|
|
49
|
+
errCodes.push(code);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return errCodes;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// errCode parses the error message formatted like `ErrType(#ErrNumber): WrappedErrType(#WrappedErrNumber)`
|
|
56
|
+
// and returns the corresponding parent ErrCode or ErrCode.Undefined if some errors occur.
|
|
57
|
+
public errCode(): ErrCode {
|
|
58
|
+
if (this.message === '') {
|
|
59
|
+
return ErrCode.Undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const match = this.message.match(/\w+\(#(\d+)\)/);
|
|
63
|
+
|
|
64
|
+
return this.extractErrCode(match);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
public grpcErrorCode(): Code {
|
|
68
|
+
return this.GrpcCode;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public toJSON(): any {
|
|
72
|
+
return {
|
|
73
|
+
message: this.message,
|
|
74
|
+
grpcErrorCode: Code[this.GrpcCode],
|
|
75
|
+
errorCode: ErrCode[this.errCode()],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public hasErrCode(error: ErrCode): boolean {
|
|
80
|
+
for (const err of this.errCodes()) {
|
|
81
|
+
if (err === error) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { GRPCError };
|