diff --git a/android/src/main/java/xyz/twenty_two/react_native/LibportalReactNativeModule.kt b/android/src/main/java/xyz/twenty_two/react_native/LibportalReactNativeModule.kt index 25fd91d..5a92d52 100644 --- a/android/src/main/java/xyz/twenty_two/react_native/LibportalReactNativeModule.kt +++ b/android/src/main/java/xyz/twenty_two/react_native/LibportalReactNativeModule.kt @@ -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 diff --git a/ios/LibportalReactNative.mm b/ios/LibportalReactNative.mm index 9360aa1..1b317c9 100644 --- a/ios/LibportalReactNative.mm +++ b/ios/LibportalReactNative.mm @@ -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) diff --git a/ios/LibportalReactNative.swift b/ios/LibportalReactNative.swift index 8e076f3..0f55e1b 100644 --- a/ios/LibportalReactNative.swift +++ b/ios/LibportalReactNative.swift @@ -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 { diff --git a/src/index.tsx b/src/index.tsx index 474bba9..684d622 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -23,6 +23,10 @@ export class PortalSdk { .catch((e: any) => console.warn(e)) } + destroy() { + LibportalReactNative.destructor() + } + poll(): Promise { return LibportalReactNative.poll() }