From 08649d7fba6bbe7b5eb5b4f057b9acb4298df75c Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Fri, 22 Dec 2023 14:31:57 -0500 Subject: [PATCH] Clean up kotlin code --- .../modules/xmtpreactnativesdk/XMTPModule.kt | 46 +++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index 10b6e2853..ac17a845a 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -17,8 +17,8 @@ import expo.modules.xmtpreactnativesdk.wrappers.DecodedMessageWrapper import expo.modules.xmtpreactnativesdk.wrappers.DecryptedLocalAttachment import expo.modules.xmtpreactnativesdk.wrappers.EncryptedLocalAttachment import expo.modules.xmtpreactnativesdk.wrappers.PreparedLocalMessage +import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.delay import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch @@ -127,8 +127,9 @@ class XMTPModule : Module() { private val isDebugEnabled = BuildConfig.DEBUG // TODO: consider making this configurable private val conversations: MutableMap = mutableMapOf() private val subscriptions: MutableMap = mutableMapOf() - var waitForPreEnableIdentityCallback: Boolean = false - var waitForPreCreateIdentityCallback: Boolean = false + private var preEnableIdentityCallbackDeferred: CompletableDeferred? = null + private var preCreateIdentityCallbackDeferred: CompletableDeferred? = null + override fun definition() = ModuleDefinition { Name("XMTP") @@ -155,8 +156,10 @@ class XMTPModule : Module() { val reactSigner = ReactNativeSigner(module = this@XMTPModule, address = address) signer = reactSigner - waitForPreEnableIdentityCallback = hasEnableIdentityCallback == true - waitForPreCreateIdentityCallback = hasCreateIdentityCallback == true + if (hasCreateIdentityCallback == true) + preCreateIdentityCallbackDeferred = CompletableDeferred() + if (hasEnableIdentityCallback == true) + preEnableIdentityCallbackDeferred = CompletableDeferred() val preCreateIdentityCallback: PreEventCallback? = preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true } val preEnableIdentityCallback: PreEventCallback? = @@ -182,8 +185,10 @@ class XMTPModule : Module() { logV("createRandom") val privateKey = PrivateKeyBuilder() - waitForPreEnableIdentityCallback = hasEnableIdentityCallback == true - waitForPreCreateIdentityCallback = hasCreateIdentityCallback == true + if (hasCreateIdentityCallback == true) + preCreateIdentityCallbackDeferred = CompletableDeferred() + if (hasEnableIdentityCallback == true) + preEnableIdentityCallbackDeferred = CompletableDeferred() val preCreateIdentityCallback: PreEventCallback? = preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true } val preEnableIdentityCallback: PreEventCallback? = @@ -600,16 +605,14 @@ class XMTPModule : Module() { client.contacts.consentList.entries.map { ConsentWrapper.encode(it.value) } } - Function("preEnableIdentityCallbackCompleted") { - logV("preEnableIdentityCallbackCompleted") - waitForPreEnableIdentityCallback = false - true - } - Function("preCreateIdentityCallbackCompleted") { logV("preCreateIdentityCallbackCompleted") - waitForPreCreateIdentityCallback = false - true + preCreateIdentityCallbackDeferred?.complete(Unit) + } + + Function("preEnableIdentityCallbackCompleted") { + logV("preEnableIdentityCallbackCompleted") + preEnableIdentityCallbackDeferred?.complete(Unit) } } @@ -736,19 +739,14 @@ class XMTPModule : Module() { private val preEnableIdentityCallback: suspend () -> Unit = { sendEvent("preEnableIdentityCallback") - waitForCallback { waitForPreEnableIdentityCallback } + preEnableIdentityCallbackDeferred?.await() + preCreateIdentityCallbackDeferred == null } private val preCreateIdentityCallback: suspend () -> Unit = { sendEvent("preCreateIdentityCallback") - waitForCallback { waitForPreCreateIdentityCallback } - } - - // Helper function to wait for a callback - private suspend fun waitForCallback(check: () -> Boolean) { - while (check()) { - delay(100) // Wait for 100ms before checking again - } + preCreateIdentityCallbackDeferred?.await() + preCreateIdentityCallbackDeferred = null } }