Skip to content

Commit

Permalink
handle sync/async witnessing
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Nov 15, 2024
1 parent 6935764 commit 3a6b401
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Sources/XyoClient/Panel/Panel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,40 @@ public class XyoPanel {
public typealias XyoPanelReportCallback = (([String]) -> Void)

private var _archivists: [XyoArchivistApiClient]
private var _witnesses: [WitnessModuleSync]
private var _witnesses: [WitnessModule]
private var _previous_hash: String?

@available(iOS 15, *)
public func report() async throws
-> [Payload]
{
let payloads = self._witnesses.map { witness in
witness.observe()
}.flatMap({ $0 })
var payloads: [Payload] = []

// Collect payloads from both synchronous and asynchronous witnesses
for witness in _witnesses {
if let syncWitness = witness as? WitnessSync {
// For synchronous witnesses, call the sync `observe` method directly
payloads.append(contentsOf: syncWitness.observe())
} else if let asyncWitness = witness as? WitnessAsync {
// For asynchronous witnesses, call the async `observe` method using `await`
do {
let asyncPayloads = try await asyncWitness.observe()
payloads.append(contentsOf: asyncPayloads)
} catch {
print("Error observing async witness: \(error)")
// Handle error as needed, possibly continue or throw
}
}
}

// Build the BoundWitness
let (bw, _) = try BoundWitnessBuilder()
.payloads(payloads)
.signers(self._witnesses.map({ $0.account }))
.signers(self._witnesses.map { $0.account })
.build(_previous_hash)
self._previous_hash = bw._hash

// Collect results from archivists using async tasks
var allResults: [[Payload]] = []
await withTaskGroup(of: [Payload]?.self) { group in
for instance in _archivists {
Expand Down

0 comments on commit 3a6b401

Please sign in to comment.