Skip to content

Commit cce9b25

Browse files
committed
Dispatch not on main thread
1 parent e42faf2 commit cce9b25

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

android/src/main/java/com/reactnativestripesdk/PaymentOptionDisplayDataMapper.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@ import com.reactnativestripesdk.getBase64FromBitmap
44
import com.reactnativestripesdk.getBitmapFromDrawable
55
import com.reactnativestripesdk.utils.mapFromPaymentSheetBillingDetails
66
import com.stripe.android.paymentelement.EmbeddedPaymentElement
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.withContext
79
import kotlinx.coroutines.withTimeout
810

911
/**
1012
* Serialize Stripe's PaymentOptionDisplayData into a WritableMap
1113
* that can be sent over the RN bridge.
1214
*/
13-
fun EmbeddedPaymentElement.PaymentOptionDisplayData.toWritableMap(): WritableMap =
15+
suspend fun EmbeddedPaymentElement.PaymentOptionDisplayData.toWritableMap(): WritableMap =
1416
Arguments.createMap().apply {
1517
putString("label", label)
1618
putString("paymentMethodType", paymentMethodType)
1719
putMap("billingDetails", mapFromPaymentSheetBillingDetails(billingDetails))
1820

19-
// Direct access to imageLoader and call it synchronously with timeout
21+
// Load image off the main thread with a timeout
2022
val imageBase64 =
2123
try {
22-
// Load synchronously with 5 second timeout
23-
val drawable =
24-
kotlinx.coroutines.runBlocking {
25-
withTimeout(5000L) {
26-
imageLoader()
24+
withContext(Dispatchers.Default) {
25+
val drawable =
26+
withTimeout(5_000L) {
27+
withContext(Dispatchers.IO) {
28+
imageLoader()
29+
}
2730
}
28-
}
29-
30-
getBitmapFromDrawable(drawable)?.let { bitmap ->
31-
getBase64FromBitmap(bitmap)
32-
} ?: ""
31+
getBitmapFromDrawable(drawable)?.let { bitmap ->
32+
getBase64FromBitmap(bitmap)
33+
} ?: ""
34+
}
3335
} catch (e: Exception) {
3436
// If imageLoader fails or times out, return empty string
3537
""

0 commit comments

Comments
 (0)