Skip to content

Commit

Permalink
Synchronous install method (#101)
Browse files Browse the repository at this point in the history
* chore: using synchronous install

* chore: updated version
  • Loading branch information
jerson authored Jan 28, 2024
1 parent b2731a0 commit 02d5b0d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 59 deletions.
53 changes: 24 additions & 29 deletions android/src/main/java/com/fastopenpgp/FastOpenpgpModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import androidx.annotation.NonNull
import com.facebook.react.bridge.*

internal class FastOpenpgpModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
ReactContextBaseJavaModule(reactContext) {

val TAG = "[FastOpenPGPModule]"

external fun initialize(jsiPtr: Long);
external fun destruct();
external fun callJSI(jsiPtr: Long, name: String, payload: ByteArray): ByteArray;
external fun callNative(name: String, payload: ByteArray): ByteArray;
external fun initialize(jsiPtr: Long);
external fun destruct();
external fun callJSI(jsiPtr: Long, name: String, payload: ByteArray): ByteArray;
external fun callNative(name: String, payload: ByteArray): ByteArray;

companion object {
init {
System.loadLibrary("fast-openpgp")
}
companion object {
init {
System.loadLibrary("fast-openpgp")
}
}

@ReactMethod
fun callJSI(name: String, payload: ReadableArray, promise: Promise) {
Expand Down Expand Up @@ -61,26 +61,22 @@ internal class FastOpenpgpModule(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 All @@ -91,4 +87,3 @@ internal class FastOpenpgpModule(reactContext: ReactApplicationContext) :
destruct();
}
}

6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ PODS:
- React-jsinspector (0.72.6)
- React-logger (0.72.6):
- glog
- react-native-fast-openpgp (2.6.0):
- react-native-fast-openpgp (2.7.1):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- React-NativeModulesApple (0.72.6):
Expand Down Expand Up @@ -698,7 +698,7 @@ SPEC CHECKSUMS:
React-jsiexecutor: 3bf18ff7cb03cd8dfdce08fbbc0d15058c1d71ae
React-jsinspector: 194e32c6aab382d88713ad3dd0025c5f5c4ee072
React-logger: cebf22b6cf43434e471dc561e5911b40ac01d289
react-native-fast-openpgp: eb8ca17f55e866965515a9e2b0b4a1dfc26a72d6
react-native-fast-openpgp: 50b906a9d41f1a3c0f0190e755b7fc37060a6b55
React-NativeModulesApple: 02e35e9a51e10c6422f04f5e4076a7c02243fff2
React-perflogger: e3596db7e753f51766bceadc061936ef1472edc3
React-RCTActionSheet: 17ab132c748b4471012abbcdcf5befe860660485
Expand All @@ -723,4 +723,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 71caf16b5cb1532cfe3e9f0ca018f42889cbeede

COCOAPODS: 1.13.0
COCOAPODS: 1.14.3
20 changes: 5 additions & 15 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,8 @@ D4m65Neoc7DBEdvzgK9IUMpwG5N0t+0pfWLhs8AZdMxE7RbP
=kbtq
-----END PGP PUBLIC KEY BLOCK-----`;

OpenPGP.useJSI = true;
const App = () => {

useEffect(()=>{
OpenPGP.useJSI = true
},[])

return (
<>
<StatusBar barStyle="dark-content" />
Expand Down Expand Up @@ -172,13 +168,8 @@ const App = () => {
privateKey={privateKey}
passphrase={passphrase}
/>
<Metadata
publicKey={publicKey}
privateKey={privateKey}
/>
<Convert
privateKey={privateKey}
/>
<Metadata publicKey={publicKey} privateKey={privateKey} />
<Convert privateKey={privateKey} />
</View>
</ScrollView>
</KeyboardAvoidingView>
Expand All @@ -188,14 +179,13 @@ const App = () => {
};

const styles = StyleSheet.create({
container: {
},
container: {},
scrollView: {
backgroundColor: Colors.lighter,
},
body: {
backgroundColor: Colors.white,
minHeight: Dimensions.get("screen").height
minHeight: Dimensions.get('screen').height,
},
});

Expand Down
19 changes: 10 additions & 9 deletions ios/FastOpenpgp.mm
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,21 @@ @implementation FastOpenpgp
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;

fastOpenPGP::install(*runtime);
NSNumber * val = [NSNumber numberWithBool:TRUE];
resolve(val);
fastOpenPGP::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-openpgp",
"version": "2.7.1",
"version": "2.7.2",
"description": "library for use openPGP",
"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 @@ -636,7 +636,7 @@ export default class OpenPGP {
let result: BridgeResponse;
if (this.useJSI) {
if (!this.loaded) {
this.loaded = await FastOpenPGPNativeModules.install();
this.loaded = FastOpenPGPNativeModules.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 FastOpenPGPNativeModules {
/**
* this method will install JSI definitions
*/
install(): Promise<boolean>;
install(): boolean;
}

interface NativeModulesDef {
Expand Down

0 comments on commit 02d5b0d

Please sign in to comment.