Skip to content

Commit

Permalink
Merge pull request #3 from dapperlabs/different_listen_for_transactions
Browse files Browse the repository at this point in the history
transaction listener update
  • Loading branch information
seanlee-dapper authored May 10, 2023
2 parents b2bc610 + 628b330 commit 22711f4
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions Sources/Mercato/Mercato.swift
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
import Foundation
import StoreKit

public typealias TransactionUpdate = ((Transaction, _ jwsRepresentation: String) async -> ())

public class Mercato {

private var purchaseController = PurchaseController()
private var productService = ProductService()

private var updateListenerTask: Task<(), Never>? = nil


public init()
{

}

func listenForTransactions(finishAutomatically: Bool = true, updateBlock: TransactionUpdate?)
{
let task = Task.detached
{
for await result in Transaction.updates
{
do {
let transaction = try checkVerified(result)

if finishAutomatically
{
await transaction.finish()
}

await updateBlock?(transaction, result.jwsRepresentation)
} catch {
print("Transaction failed verification")
}
}
}

self.updateListenerTask = task
}

func listenForTransactions() async -> [(Transaction, String)] {
var pending = [(Transaction, String)]()
for await result in Transaction.updates
{
do {
let transaction = try checkVerified(result)
pending.append((transaction, result.jwsRepresentation))
} catch {
print("Transaction failed verification")
}
}
return pending
}

//TODO: throw an error if productId are invalid
public func retrieveProducts(productIds: Set<String>) async throws -> [Product]
Expand Down Expand Up @@ -83,26 +69,27 @@ public class Mercato {
throw error
}
}

deinit {
updateListenerTask?.cancel()
}
}

extension Mercato
{
fileprivate static let shared: Mercato = .init()

public static func listenForTransactions(finishAutomatically: Bool = true, updateBlock: TransactionUpdate?)
public static func listenForTransactions() async -> [(Transaction, String)]
{
shared.listenForTransactions(finishAutomatically: finishAutomatically, updateBlock: updateBlock)
return await shared.listenForTransactions()
}

public static func retrieveProducts(productIds: Set<String>) async throws -> [Product]
{
try await shared.retrieveProducts(productIds: productIds)
}


public static func retrieveProduct(productId: String) async throws -> Product
{
try await shared.retrieveProduct(productId: productId)
}

@discardableResult
public static func purchase(product: Product,
quantity: Int = 1,
Expand Down

0 comments on commit 22711f4

Please sign in to comment.