Skip to content

Commit b47b1a7

Browse files
authored
chore: using install sync to avoid race condition (#87)
1 parent d7b0d20 commit b47b1a7

File tree

5 files changed

+28
-31
lines changed

5 files changed

+28
-31
lines changed

android/src/main/java/com/fastrsa/FastRsaModule.kt

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,22 @@ internal class FastRsaModule(reactContext: ReactApplicationContext) :
6060
}.start()
6161
}
6262

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

8581
override fun getName(): String {

ios/FastRsa.mm

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

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

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

116117
+ (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-rsa",
3-
"version": "2.4.1",
3+
"version": "2.4.2",
44
"description": "library for use RSA",
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
@@ -540,7 +540,7 @@ export default class RSA {
540540
let result: BridgeResponse;
541541
if (this.useJSI) {
542542
if (!this.loaded) {
543-
this.loaded = await FastRSANativeModules.install();
543+
this.loaded = FastRSANativeModules.install();
544544
console.log(
545545
this.TAG,
546546
`(${name})`,

src/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ interface FastRSANativeModules {
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)