Skip to content

Commit

Permalink
fix: cancel coroutine on invalidate
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianMazur committed Oct 2, 2024
1 parent b19024b commit 555c8a4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions android/src/main/java/com/oblador/keychain/KeychainModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import com.oblador.keychain.exceptions.EmptyParameterException
import com.oblador.keychain.exceptions.KeyStoreAccessException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -127,7 +129,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
private val prefsStorage: PrefsStorage

/** Launches a coroutine to perform non-blocking UI operations */
private val mainCoroutineScope = CoroutineScope(Dispatchers.Default)
private val coroutineScope = CoroutineScope(Dispatchers.Default)

// endregion
// region Initialization
Expand Down Expand Up @@ -171,6 +173,13 @@ class KeychainModule(reactContext: ReactApplicationContext) :
override fun getName(): String {
return KEYCHAIN_MODULE
}

override fun invalidate() {
super.invalidate()
if (coroutineScope.isActive) {
coroutineScope.cancel("$KEYCHAIN_MODULE has been destroyed.")
}
}

/** {@inheritDoc} */
override fun getConstants(): Map<String, Any> {
Expand All @@ -190,7 +199,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
options: ReadableMap?,
promise: Promise
) {
mainCoroutineScope.launch {
coroutineScope.launch {
try {
throwIfEmptyLoginPassword(username, password)
val level = getSecurityLevelOrDefault(options)
Expand Down Expand Up @@ -246,7 +255,7 @@ class KeychainModule(reactContext: ReactApplicationContext) :
}

private fun getGenericPassword(alias: String, options: ReadableMap?, promise: Promise) {
mainCoroutineScope.launch {
coroutineScope.launch {
try {
val resultSet = prefsStorage.getEncryptedEntry(alias)
if (resultSet == null) {
Expand Down

0 comments on commit 555c8a4

Please sign in to comment.