Skip to content

Commit

Permalink
ISSUE-1765: Make suspend coroutine wrappers cancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaPavlovskii committed Aug 7, 2024
1 parent f8ee985 commit 219893a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.revenuecat.purchases.CacheFetchPolicy.CACHED_OR_FETCHED
import com.revenuecat.purchases.data.LogInResult
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlin.coroutines.suspendCancellableCoroutine

/**
* Get latest available customer info.
Expand All @@ -21,7 +21,7 @@ import kotlin.coroutines.suspendCoroutine
suspend fun Purchases.awaitCustomerInfo(
fetchPolicy: CacheFetchPolicy = CacheFetchPolicy.default(),
): CustomerInfo {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
getCustomerInfoWith(
fetchPolicy,
onSuccess = continuation::resume,
Expand All @@ -43,7 +43,7 @@ suspend fun Purchases.awaitCustomerInfo(
@JvmSynthetic
@Throws(PurchasesTransactionException::class)
suspend fun Purchases.awaitLogIn(appUserID: String): LogInResult {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
logInWith(
appUserID,
onSuccess = { customerInfo, created ->
Expand All @@ -66,7 +66,7 @@ suspend fun Purchases.awaitLogIn(appUserID: String): LogInResult {
@JvmSynthetic
@Throws(PurchasesTransactionException::class)
suspend fun Purchases.awaitLogOut(): CustomerInfo {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
logOutWith(
onSuccess = { continuation.resume(it) },
onError = { continuation.resumeWithException(PurchasesException(it)) },
Expand All @@ -87,7 +87,7 @@ suspend fun Purchases.awaitLogOut(): CustomerInfo {
@JvmSynthetic
@Throws(PurchasesException::class)
suspend fun Purchases.awaitSyncPurchases(): CustomerInfo {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
syncPurchasesWith(
onSuccess = continuation::resume,
onError = { continuation.resumeWithException(PurchasesException(it)) },
Expand All @@ -114,7 +114,7 @@ suspend fun Purchases.awaitSyncPurchases(): CustomerInfo {
@JvmSynthetic
@Throws(PurchasesException::class)
suspend fun Purchases.awaitSyncAttributesAndOfferingsIfNeeded(): Offerings {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
syncAttributesAndOfferingsIfNeededWith(
onSuccess = continuation::resume,
onError = { continuation.resumeWithException(PurchasesException(it)) },
Expand All @@ -140,7 +140,7 @@ suspend fun Purchases.awaitSyncAttributesAndOfferingsIfNeeded(): Offerings {
@JvmSynthetic
@Throws(PurchasesException::class)
suspend fun Purchases.getAmazonLWAConsentStatus(): AmazonLWAConsentStatus {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
getAmazonLWAConsentStatusWith(
onSuccess = continuation::resume,
onError = { continuation.resumeWithException(PurchasesException(it)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.revenuecat.purchases

import com.revenuecat.purchases.models.StoreProduct
import com.revenuecat.purchases.models.StoreTransaction
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

/**
* Fetch the configured offerings for this users. Offerings allows you to configure your in-app
Expand All @@ -22,7 +22,7 @@ import kotlin.coroutines.suspendCoroutine
@JvmSynthetic
@Throws(PurchasesException::class)
suspend fun Purchases.awaitOfferings(): Offerings {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
getOfferingsWith(
onSuccess = continuation::resume,
onError = { continuation.resumeWithException(PurchasesException(it)) },
Expand All @@ -49,7 +49,7 @@ suspend fun Purchases.awaitOfferings(): Offerings {
@JvmSynthetic
@Throws(PurchasesTransactionException::class)
suspend fun Purchases.awaitPurchase(purchaseParams: PurchaseParams): PurchaseResult {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
purchase(
purchaseParams = purchaseParams,
callback = purchaseCompletedCallback(
Expand Down Expand Up @@ -82,7 +82,7 @@ suspend fun Purchases.awaitGetProducts(
productIds: List<String>,
type: ProductType? = null,
): List<StoreProduct> {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
getProductsWith(
productIds,
type,
Expand Down Expand Up @@ -113,7 +113,7 @@ suspend fun Purchases.awaitGetProducts(
@JvmSynthetic
@Throws(PurchasesTransactionException::class)
suspend fun Purchases.awaitRestore(): CustomerInfo {
return suspendCoroutine { continuation ->
return suspendCancellableCoroutine { continuation ->
restorePurchasesWith(
onSuccess = { continuation.resume(it) },
onError = { continuation.resumeWithException(PurchasesException(it)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.Purchases
import com.revenuecat.purchases.PurchasesException
import com.revenuecat.purchases.getCustomerInfoWith
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

Expand All @@ -35,7 +36,7 @@ internal fun windowAspectRatio(): Float {
* Evaluates [shouldDisplayBlock] with the current CustomerInfo to determine if a paywall should be displayed.
*/
internal suspend fun shouldDisplayPaywall(shouldDisplayBlock: (CustomerInfo) -> Boolean): Boolean {
return suspendCoroutine { continuation -> shouldDisplayPaywall(shouldDisplayBlock, continuation::resume) }
return suspendCancellableCoroutine { continuation -> shouldDisplayPaywall(shouldDisplayBlock, continuation::resume) }
}

/**
Expand Down

0 comments on commit 219893a

Please sign in to comment.