|
1 | 1 | import Foundation |
2 | 2 | import VssRustClientFfi |
3 | 3 |
|
| 4 | +/// Actor to coordinate VSS client setup (ensures only one setup runs at a time) |
| 5 | +private actor VssSetupCoordinator { |
| 6 | + private enum SetupState { |
| 7 | + case idle |
| 8 | + case inProgress(Task<Void, Error>) |
| 9 | + case completed |
| 10 | + } |
| 11 | + |
| 12 | + private var state: SetupState = .idle |
| 13 | + |
| 14 | + func awaitSetup(setupAction: @escaping () async throws -> Void) async throws { |
| 15 | + switch state { |
| 16 | + case .completed: |
| 17 | + Logger.debug("VssSetupCoordinator: already completed, returning", context: "VssBackupClient") |
| 18 | + return |
| 19 | + |
| 20 | + case let .inProgress(existingTask): |
| 21 | + Logger.debug("VssSetupCoordinator: setup in progress, waiting for existing task", context: "VssBackupClient") |
| 22 | + try await existingTask.value |
| 23 | + Logger.debug("VssSetupCoordinator: existing task completed", context: "VssBackupClient") |
| 24 | + return |
| 25 | + |
| 26 | + case .idle: |
| 27 | + Logger.debug("VssSetupCoordinator: idle, starting new setup", context: "VssBackupClient") |
| 28 | + let task = Task { |
| 29 | + try await setupAction() |
| 30 | + } |
| 31 | + state = .inProgress(task) |
| 32 | + |
| 33 | + do { |
| 34 | + try await task.value |
| 35 | + state = .completed |
| 36 | + Logger.debug("VssSetupCoordinator: setup completed successfully", context: "VssBackupClient") |
| 37 | + } catch { |
| 38 | + // Reset on any error to allow retry attempts |
| 39 | + state = .idle |
| 40 | + Logger.debug("VssSetupCoordinator: setup failed, resetting to idle", context: "VssBackupClient") |
| 41 | + throw error |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + func reset() { |
| 47 | + Logger.debug("VssSetupCoordinator: reset called", context: "VssBackupClient") |
| 48 | + if case let .inProgress(task) = state { |
| 49 | + task.cancel() |
| 50 | + } |
| 51 | + state = .idle |
| 52 | + } |
| 53 | +} |
| 54 | + |
4 | 55 | class VssBackupClient { |
5 | 56 | static let shared = VssBackupClient() |
6 | 57 |
|
7 | | - private var isSetup: Task<Void, Error>? |
| 58 | + private let setupCoordinator = VssSetupCoordinator() |
8 | 59 |
|
9 | 60 | private init() {} |
10 | 61 |
|
11 | | - func reset() { |
12 | | - isSetup = nil |
| 62 | + func reset() async { |
| 63 | + await setupCoordinator.reset() |
13 | 64 | } |
14 | 65 |
|
15 | | - func setup(walletIndex: Int = 0) async throws { |
| 66 | + private func setup(walletIndex: Int = 0) async throws { |
16 | 67 | do { |
17 | 68 | try await withTimeout(seconds: 30) { |
18 | 69 | Logger.debug("VSS client setting up…", context: "VssBackupClient") |
@@ -87,26 +138,9 @@ class VssBackupClient { |
87 | 138 | } |
88 | 139 |
|
89 | 140 | private func awaitSetup() async throws { |
90 | | - if let existingSetup = isSetup { |
91 | | - do { |
92 | | - try await existingSetup.value |
93 | | - } catch let error as CancellationError { |
94 | | - isSetup = nil |
95 | | - throw error |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - let setupTask = Task { |
| 141 | + try await setupCoordinator.awaitSetup { [self] in |
100 | 142 | try await setup() |
101 | 143 | } |
102 | | - isSetup = setupTask |
103 | | - |
104 | | - do { |
105 | | - try await setupTask.value |
106 | | - } catch let error as CancellationError { |
107 | | - isSetup = nil |
108 | | - throw error |
109 | | - } |
110 | 144 | } |
111 | 145 |
|
112 | 146 | private func withTimeout<T>(seconds: TimeInterval, operation: @escaping () async throws -> T) async throws -> T { |
|
0 commit comments