Skip to content

Commit 02d5b0d

Browse files
authored
Synchronous install method (#101)
* chore: using synchronous install * chore: updated version
1 parent b2731a0 commit 02d5b0d

File tree

7 files changed

+45
-59
lines changed

7 files changed

+45
-59
lines changed

android/src/main/java/com/fastopenpgp/FastOpenpgpModule.kt

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import androidx.annotation.NonNull
55
import com.facebook.react.bridge.*
66

77
internal class FastOpenpgpModule(reactContext: ReactApplicationContext) :
8-
ReactContextBaseJavaModule(reactContext) {
8+
ReactContextBaseJavaModule(reactContext) {
99

1010
val TAG = "[FastOpenPGPModule]"
1111

12-
external fun initialize(jsiPtr: Long);
13-
external fun destruct();
14-
external fun callJSI(jsiPtr: Long, name: String, payload: ByteArray): ByteArray;
15-
external fun callNative(name: String, payload: ByteArray): ByteArray;
12+
external fun initialize(jsiPtr: Long);
13+
external fun destruct();
14+
external fun callJSI(jsiPtr: Long, name: String, payload: ByteArray): ByteArray;
15+
external fun callNative(name: String, payload: ByteArray): ByteArray;
1616

17-
companion object {
18-
init {
19-
System.loadLibrary("fast-openpgp")
20-
}
17+
companion object {
18+
init {
19+
System.loadLibrary("fast-openpgp")
2120
}
21+
}
2222

2323
@ReactMethod
2424
fun callJSI(name: String, payload: ReadableArray, promise: Promise) {
@@ -61,26 +61,22 @@ internal class FastOpenpgpModule(reactContext: ReactApplicationContext) :
6161
}.start()
6262
}
6363

64-
@ReactMethod
65-
fun install(promise: Promise) {
66-
Thread {
67-
reactApplicationContext.runOnJSQueueThread {
68-
Log.d(TAG, "installing")
69-
try {
70-
val contextHolder = this.reactApplicationContext.javaScriptContextHolder!!.get()
71-
if (contextHolder.toInt() == 0) {
72-
promise.resolve(false)
73-
return@runOnJSQueueThread
74-
}
75-
initialize(contextHolder)
76-
Log.i(TAG, "successfully installed")
77-
promise.resolve(true)
78-
} catch (exception: java.lang.Exception) {
79-
Log.e(TAG, "failed to install JSI", exception)
80-
promise.reject(exception)
81-
}
64+
@ReactMethod(isBlockingSynchronousMethod = true)
65+
fun install(): Boolean {
66+
Log.d(TAG, "installing")
67+
try {
68+
val contextHolder = this.reactApplicationContext.javaScriptContextHolder!!.get()
69+
if (contextHolder.toInt() == 0) {
70+
Log.d(TAG, "context not available")
71+
return false
8272
}
83-
}.start()
73+
initialize(contextHolder)
74+
Log.i(TAG, "successfully installed")
75+
return true
76+
} catch (exception: java.lang.Exception) {
77+
Log.e(TAG, "failed to install JSI", exception)
78+
return false
79+
}
8480
}
8581

8682
override fun getName(): String {
@@ -91,4 +87,3 @@ internal class FastOpenpgpModule(reactContext: ReactApplicationContext) :
9187
destruct();
9288
}
9389
}
94-

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ PODS:
375375
- React-jsinspector (0.72.6)
376376
- React-logger (0.72.6):
377377
- glog
378-
- react-native-fast-openpgp (2.6.0):
378+
- react-native-fast-openpgp (2.7.1):
379379
- RCT-Folly (= 2021.07.22.00)
380380
- React-Core
381381
- React-NativeModulesApple (0.72.6):
@@ -698,7 +698,7 @@ SPEC CHECKSUMS:
698698
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
699699
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
700700
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
701-
react-native-fast-openpgp: eb8ca17f55e866965515a9e2b0b4a1dfc26a72d6
701+
react-native-fast-openpgp: 50b906a9d41f1a3c0f0190e755b7fc37060a6b55
702702
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
703703
React-perflogger: e3596db7e753f51766bceadc061936ef1472edc3
704704
React-RCTActionSheet: 17ab132c748b4471012abbcdcf5befe860660485
@@ -723,4 +723,4 @@ SPEC CHECKSUMS:
723723

724724
PODFILE CHECKSUM: 71caf16b5cb1532cfe3e9f0ca018f42889cbeede
725725

726-
COCOAPODS: 1.13.0
726+
COCOAPODS: 1.14.3

example/src/App.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,8 @@ D4m65Neoc7DBEdvzgK9IUMpwG5N0t+0pfWLhs8AZdMxE7RbP
116116
=kbtq
117117
-----END PGP PUBLIC KEY BLOCK-----`;
118118

119+
OpenPGP.useJSI = true;
119120
const App = () => {
120-
121-
useEffect(()=>{
122-
OpenPGP.useJSI = true
123-
},[])
124-
125121
return (
126122
<>
127123
<StatusBar barStyle="dark-content" />
@@ -172,13 +168,8 @@ const App = () => {
172168
privateKey={privateKey}
173169
passphrase={passphrase}
174170
/>
175-
<Metadata
176-
publicKey={publicKey}
177-
privateKey={privateKey}
178-
/>
179-
<Convert
180-
privateKey={privateKey}
181-
/>
171+
<Metadata publicKey={publicKey} privateKey={privateKey} />
172+
<Convert privateKey={privateKey} />
182173
</View>
183174
</ScrollView>
184175
</KeyboardAvoidingView>
@@ -188,14 +179,13 @@ const App = () => {
188179
};
189180

190181
const styles = StyleSheet.create({
191-
container: {
192-
},
182+
container: {},
193183
scrollView: {
194184
backgroundColor: Colors.lighter,
195185
},
196186
body: {
197187
backgroundColor: Colors.white,
198-
minHeight: Dimensions.get("screen").height
188+
minHeight: Dimensions.get('screen').height,
199189
},
200190
});
201191

ios/FastOpenpgp.mm

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,21 @@ @implementation FastOpenpgp
9898
resolve(result);
9999
}
100100

101-
RCT_REMAP_METHOD(install,installWithResolver:(RCTPromiseResolveBlock)resolve
102-
withReject:(RCTPromiseRejectBlock)reject)
101+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install)
103102
{
104103
RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge;
105104
if (!cxxBridge.runtime) {
106-
NSNumber * val = [NSNumber numberWithBool:NO];
107-
resolve(val);
108-
return;
105+
return @false;
109106
}
110-
jsi::Runtime * runtime = (jsi::Runtime *)cxxBridge.runtime;
107+
using namespace facebook;
108+
auto jsiRuntime = (jsi::Runtime *)cxxBridge.runtime;
109+
if (jsiRuntime == nil) {
110+
return @false;
111+
}
112+
auto &runtime = *jsiRuntime;
111113

112-
fastOpenPGP::install(*runtime);
113-
NSNumber * val = [NSNumber numberWithBool:TRUE];
114-
resolve(val);
114+
fastOpenPGP::install(runtime);
115+
return @true;
115116
}
116117

117118
+ (BOOL)requiresMainQueueSetup {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-fast-openpgp",
3-
"version": "2.7.1",
3+
"version": "2.7.2",
44
"description": "library for use openPGP",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ export default class OpenPGP {
636636
let result: BridgeResponse;
637637
if (this.useJSI) {
638638
if (!this.loaded) {
639-
this.loaded = await FastOpenPGPNativeModules.install();
639+
this.loaded = FastOpenPGPNativeModules.install();
640640
console.log(
641641
this.TAG,
642642
`(${name})`,

src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ interface FastOpenPGPNativeModules {
4646
/**
4747
* this method will install JSI definitions
4848
*/
49-
install(): Promise<boolean>;
49+
install(): boolean;
5050
}
5151

5252
interface NativeModulesDef {

0 commit comments

Comments
 (0)