Add explicit destructor

This commit is contained in:
Alekos Filini 2024-10-11 19:08:20 +02:00 committed by Overtorment
parent d9d8592179
commit f577c0ef8c
4 changed files with 19 additions and 5 deletions

View File

@ -11,6 +11,7 @@ import com.facebook.react.bridge.WritableNativeMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import xyz.twenty_two.PortalSdk
@ -19,7 +20,7 @@ import xyz.twenty_two.GenerateMnemonicWords
class LibportalReactNativeModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
var scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
var instance: PortalSdk? = null
override fun getName(): String {
@ -28,10 +29,15 @@ class LibportalReactNativeModule(reactContext: ReactApplicationContext) :
@ReactMethod
fun constructor(useFastOps: Boolean, promise: Promise) {
if (instance == null) {
instance = PortalSdk(useFastOps)
promise.resolve(null)
}
scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
instance = PortalSdk(useFastOps)
promise.resolve(null)
}
@ReactMethod
fun destructor() {
scope.cancel()
instance = null
}
@ReactMethod

View File

@ -6,6 +6,8 @@ RCT_EXTERN_METHOD(constructor: (BOOL)useFastOps
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(destructor)
RCT_EXTERN_METHOD(poll: (RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

View File

@ -29,6 +29,8 @@ class LibportalReactNative: NSObject {
self.sdk = PortalSdk(useFastOps: useFastOps)
resolve(nil)
}
@objc func destructor() -> Void {}
@objc func poll(_ resolve: @escaping RCTPromiseResolveBlock, withRejecter reject: @escaping RCTPromiseRejectBlock) -> Void {
Task {

View File

@ -23,6 +23,10 @@ export class PortalSdk {
.catch((e: any) => console.warn(e))
}
destroy() {
LibportalReactNative.destructor()
}
poll(): Promise<NfcOut> {
return LibportalReactNative.poll()
}