Skip to content

Commit

Permalink
fix: sign actions should wait for pre-callback completion before proc…
Browse files Browse the repository at this point in the history
…eeding
  • Loading branch information
cameronvoell committed Dec 22, 2023
1 parent e70335a commit 3650b8d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import expo.modules.xmtpreactnativesdk.wrappers.DecryptedLocalAttachment
import expo.modules.xmtpreactnativesdk.wrappers.EncryptedLocalAttachment
import expo.modules.xmtpreactnativesdk.wrappers.PreparedLocalMessage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -126,6 +127,8 @@ class XMTPModule : Module() {
private val isDebugEnabled = BuildConfig.DEBUG // TODO: consider making this configurable
private val conversations: MutableMap<String, Conversation> = mutableMapOf()
private val subscriptions: MutableMap<String, Job> = mutableMapOf()
var waitForPreEnableIdentityCallback: Boolean = false
var waitForPreCreateIdentityCallback: Boolean = false

override fun definition() = ModuleDefinition {
Name("XMTP")
Expand All @@ -151,6 +154,9 @@ class XMTPModule : Module() {
logV("auth")
val reactSigner = ReactNativeSigner(module = this@XMTPModule, address = address)
signer = reactSigner

waitForPreEnableIdentityCallback = hasEnableIdentityCallback == true
waitForPreCreateIdentityCallback = hasCreateIdentityCallback == true
val preCreateIdentityCallback: PreEventCallback? =
preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true }
val preEnableIdentityCallback: PreEventCallback? =
Expand All @@ -175,6 +181,9 @@ class XMTPModule : Module() {
AsyncFunction("createRandom") { environment: String, appVersion: String?, hasCreateIdentityCallback: Boolean?, hasEnableIdentityCallback: Boolean? ->
logV("createRandom")
val privateKey = PrivateKeyBuilder()

waitForPreEnableIdentityCallback = hasEnableIdentityCallback == true
waitForPreCreateIdentityCallback = hasCreateIdentityCallback == true
val preCreateIdentityCallback: PreEventCallback? =
preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true }
val preEnableIdentityCallback: PreEventCallback? =
Expand Down Expand Up @@ -590,6 +599,18 @@ class XMTPModule : Module() {
val client = clients[clientAddress] ?: throw XMTPException("No client")
client.contacts.consentList.entries.map { ConsentWrapper.encode(it.value) }
}

Function("preEnableIdentityCallbackCompleted") {
logV("preEnableIdentityCallbackCompleted")
waitForPreEnableIdentityCallback = false
true
}

Function("preCreateIdentityCallbackCompleted") {
logV("preCreateIdentityCallbackCompleted")
waitForPreCreateIdentityCallback = false
true
}
}

//
Expand Down Expand Up @@ -715,10 +736,19 @@ class XMTPModule : Module() {

private val preEnableIdentityCallback: suspend () -> Unit = {
sendEvent("preEnableIdentityCallback")
waitForCallback { waitForPreEnableIdentityCallback }
}

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
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ export async function consentList(
})
}

export function preEnableIdentityCallbackCompleted() {
XMTPModule.preEnableIdentityCallbackCompleted()
}

export function preCreateIdentityCallbackCompleted() {
XMTPModule.preCreateIdentityCallbackCompleted()
}

export const emitter = new EventEmitter(XMTPModule ?? NativeModulesProxy.XMTP)

export * from './lib/ContentCodec'
Expand Down
2 changes: 2 additions & 0 deletions src/lib/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class Client<ContentTypes> {
opts,
async () => {
await this.executeCallback(opts?.preEnableIdentityCallback)
XMTPModule.preEnableIdentityCallbackCompleted()
}
)

Expand All @@ -244,6 +245,7 @@ export class Client<ContentTypes> {
opts,
async () => {
await this.executeCallback(opts?.preCreateIdentityCallback)
XMTPModule.preCreateIdentityCallbackCompleted()
}
)

Expand Down

0 comments on commit 3650b8d

Please sign in to comment.