From 3a6b401f81a1a2515d025354d606cb735c73f027 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Thu, 14 Nov 2024 19:10:30 -0600 Subject: [PATCH] handle sync/async witnessing --- Sources/XyoClient/Panel/Panel.swift | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Sources/XyoClient/Panel/Panel.swift b/Sources/XyoClient/Panel/Panel.swift index 319fecb..f0341cd 100644 --- a/Sources/XyoClient/Panel/Panel.swift +++ b/Sources/XyoClient/Panel/Panel.swift @@ -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 {