Skip to content

Commit

Permalink
chore: using install sync to avoid race condition (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerson authored Jan 28, 2024
1 parent d7b0d20 commit b47b1a7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
34 changes: 15 additions & 19 deletions android/src/main/java/com/fastrsa/FastRsaModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,22 @@ internal class FastRsaModule(reactContext: ReactApplicationContext) :
}.start()
}

@ReactMethod
fun install(promise: Promise) {
Thread {
reactApplicationContext.runOnJSQueueThread {
Log.d(TAG, "installing")
try {
val contextHolder = this.reactApplicationContext.javaScriptContextHolder!!.get()
if (contextHolder.toInt() == 0) {
promise.resolve(false)
return@runOnJSQueueThread
}
initialize(contextHolder)
Log.i(TAG, "successfully installed")
promise.resolve(true)
} catch (exception: java.lang.Exception) {
Log.e(TAG, "failed to install JSI", exception)
promise.reject(exception)
}
@ReactMethod(isBlockingSynchronousMethod = true)
fun install(): Boolean {
Log.d(TAG, "installing")
try {
val contextHolder = this.reactApplicationContext.javaScriptContextHolder!!.get()
if (contextHolder.toInt() == 0) {
Log.d(TAG, "context not available")
return false
}
}.start()
initialize(contextHolder)
Log.i(TAG, "successfully installed")
return true
} catch (exception: java.lang.Exception) {
Log.e(TAG, "failed to install JSI", exception)
return false
}
}

override fun getName(): String {
Expand Down
19 changes: 10 additions & 9 deletions ios/FastRsa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,21 @@ @implementation FastRsa
resolve(result);
}

RCT_REMAP_METHOD(install,installWithResolver:(RCTPromiseResolveBlock)resolve
withReject:(RCTPromiseRejectBlock)reject)
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install)
{
RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge;
if (!cxxBridge.runtime) {
NSNumber * val = [NSNumber numberWithBool:NO];
resolve(val);
return;
return @false;
}
jsi::Runtime * runtime = (jsi::Runtime *)cxxBridge.runtime;
using namespace facebook;
auto jsiRuntime = (jsi::Runtime *)cxxBridge.runtime;
if (jsiRuntime == nil) {
return @false;
}
auto &runtime = *jsiRuntime;

fastRSA::install(*runtime);
NSNumber * val = [NSNumber numberWithBool:TRUE];
resolve(val);
fastRSA::install(runtime);
return @true;
}

+ (BOOL)requiresMainQueueSetup {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-fast-rsa",
"version": "2.4.1",
"version": "2.4.2",
"description": "library for use RSA",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export default class RSA {
let result: BridgeResponse;
if (this.useJSI) {
if (!this.loaded) {
this.loaded = await FastRSANativeModules.install();
this.loaded = FastRSANativeModules.install();
console.log(
this.TAG,
`(${name})`,
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ interface FastRSANativeModules {
/**
* this method will install JSI definitions
*/
install(): Promise<boolean>;
install(): boolean;
}

interface NativeModulesDef {
Expand Down

0 comments on commit b47b1a7

Please sign in to comment.