@@ -72,27 +72,19 @@ private actor LightningConnectionManager {
72
72
73
73
static let shared = LightningConnectionManager ( )
74
74
75
- private var connectionTask : Task < LightningSmartCardConnection , Error > ?
76
75
private var pendingConnectionPromise : Promise < LightningSmartCardConnection > ?
77
76
private var connectionState : ( connectionID: ConnectionID , didCloseConnection: ( Promise < Error ? > ) ) ?
78
77
79
78
private init ( ) { }
80
79
81
80
func connect( ) async throws -> LightningSmartCardConnection {
82
- // If a connection task is already running, await its result
83
- if let connectionTask {
84
- trace ( message: " awaiting existing connection task " )
85
- _ = try await connectionTask. value
86
- // we cancel this task because only one of multiple
87
- // concurrent connections can succed
88
- throw ConnectionError . cancelled
81
+ // If there is already a connection the caller must close the connection first.
82
+ if connectionState != nil || pendingConnectionPromise != nil {
83
+ throw ConnectionError . busy
89
84
}
90
85
91
86
// Otherwise, create and store a new connection task.
92
87
let task = Task { ( ) -> LightningSmartCardConnection in
93
- // When the task finishes (on any path), clear it to allow a new connection.
94
- defer { self . connectionTask = nil }
95
-
96
88
trace ( message: " begin new connection task " )
97
89
98
90
do {
@@ -121,12 +113,12 @@ private actor LightningConnectionManager {
121
113
trace ( message: " connection failed: \( error. localizedDescription) " )
122
114
// Cleanup on failure
123
115
self . pendingConnectionPromise = nil
116
+ self . connectionState = nil
124
117
await EAAccessoryWrapper . shared. stopMonitoring ( )
125
118
throw error
126
119
}
127
120
}
128
121
129
- self . connectionTask = task
130
122
return try await task. value
131
123
}
132
124
0 commit comments