Skip to content

Commit 8453ed9

Browse files
committed
Pause transaction listener while purchasing
1 parent c79669d commit 8453ed9

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

Yosemite/Yosemite/Stores/InAppPurchaseStore.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Combine
12
import Foundation
23
import Storage
34
import StoreKit
@@ -9,6 +10,7 @@ public class InAppPurchaseStore: Store {
910
private var listenTask: Task<Void, Error>?
1011
private let remote: InAppPurchasesRemote
1112
private var useBackend = true
13+
private var pauseTransactionListener = CurrentValueSubject<Bool, Never>(false)
1214

1315
public override init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network) {
1416
remote = InAppPurchasesRemote(network: network)
@@ -93,6 +95,12 @@ private extension InAppPurchaseStore {
9395

9496

9597
logInfo("Purchasing product \(product.id) for site \(siteID) with options \(purchaseOptions)")
98+
logInfo("Pausing transaction listener")
99+
pauseTransactionListener.send(true)
100+
defer {
101+
logInfo("Resuming transaction listener")
102+
pauseTransactionListener.send(false)
103+
}
96104
let purchaseResult = try await product.purchase(options: purchaseOptions)
97105
switch purchaseResult {
98106
case .success(let result):
@@ -251,11 +259,16 @@ private extension InAppPurchaseStore {
251259
assert(listenTask == nil, "InAppPurchaseStore.listenForTransactions() called while already listening for transactions")
252260

253261
listenTask = Task.detached { [weak self] in
262+
guard let self else {
263+
return
264+
}
254265
for await result in Transaction.updates {
255266
do {
256-
try await self?.handleCompletedTransaction(result)
267+
// Wait until the purchase finishes
268+
_ = await self.pauseTransactionListener.values.contains(false)
269+
try await self.handleCompletedTransaction(result)
257270
} catch {
258-
self?.logError("Error handling transaction update: \(error)")
271+
self.logError("Error handling transaction update: \(error)")
259272
}
260273
}
261274
}

0 commit comments

Comments
 (0)