From 4b3ca8b1c0c9f2a0c4b43b50ddfced72d8b0e1e2 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 6 Jun 2026 14:23:23 +0700 Subject: [PATCH] optimize the signer --- .../src/commonMain/kotlin/su/reya/coop/Signer.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt b/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt index b89df78..dc98991 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Signer.kt @@ -7,34 +7,37 @@ import rust.nostr.sdk.AsyncNostrSigner import rust.nostr.sdk.Event import rust.nostr.sdk.PublicKey import rust.nostr.sdk.UnsignedEvent +import kotlin.concurrent.Volatile import kotlin.time.Duration.Companion.seconds class UniversalSigner(initialSigner: AsyncNostrSigner) : AsyncNostrSigner { private val mutex = Mutex() + + @Volatile private var signer: AsyncNostrSigner = initialSigner + @Volatile var currentUser: PublicKey? = null private set /** * Get the current signer. */ - suspend fun get(): AsyncNostrSigner = mutex.withLock { - signer - } + fun get(): AsyncNostrSigner = signer /** * Switch to a new signer. */ suspend fun switch(newSigner: AsyncNostrSigner) = mutex.withLock { - signer = newSigner - try { - currentUser = withTimeoutOrNull(20.seconds) { + val pubkey = try { + withTimeoutOrNull(20.seconds) { newSigner.getPublicKeyAsync() } } catch (e: Exception) { throw IllegalStateException("Failed to get public key from signer", e) } + signer = newSigner + currentUser = pubkey } override suspend fun getPublicKeyAsync(): PublicKey? {