@@ -161,7 +161,6 @@ public final class DarwinCentral: CentralManager, ObservableObject {
161161 let connected = await self . peripherals
162162 . filter { $0. value }
163163 . keys
164-
165164 for peripheral in connected {
166165 await disconnect ( peripheral)
167166 }
@@ -334,14 +333,16 @@ public final class DarwinCentral: CentralManager, ObservableObject {
334333 #if DEBUG
335334 log ? ( " Queue \( function) for peripheral \( peripheral) " )
336335 #endif
337- return try await withThrowingContinuation ( for: peripheral, function: function) { continuation in
336+ let value = try await withThrowingContinuation ( for: peripheral, function: function) { continuation in
338337 let queuedOperation = QueuedOperation ( operation: operation ( continuation) )
339338 self . async { [ unowned self] in
340339 self . stopScan ( ) // stop scanning
341340 let context = self . continuation ( for: peripheral)
342341 context. operations. push ( queuedOperation)
343342 }
344343 }
344+ try Task . checkCancellation ( )
345+ return value
345346 }
346347
347348 private func dequeue< Operation> (
@@ -351,7 +352,7 @@ public final class DarwinCentral: CentralManager, ObservableObject {
351352 filter: ( DarwinCentral . Operation ) -> ( Operation ? )
352353 ) where Operation: DarwinCentralOperation {
353354 #if DEBUG
354- log ? ( " Dequeue \( function) for peripheral \( peripheral) with \( result ) " )
355+ log ? ( " Dequeue \( function) for peripheral \( peripheral) " )
355356 #endif
356357 let context = self . continuation ( for: peripheral)
357358 context. operations. popFirst ( where: { filter ( $0. operation) } ) { ( queuedOperation, operation) in
@@ -803,7 +804,11 @@ internal extension DarwinCentral {
803804 guard let peripheralObject = validatePeripheral ( operation. peripheral, for: operation. continuation) else {
804805 return false
805806 }
806- // connect
807+ // disconnect
808+ guard peripheralObject. state != . disconnected else {
809+ operation. continuation. resume ( ) // already disconnected
810+ return false
811+ }
807812 self . centralManager. cancelPeripheralConnection ( peripheralObject)
808813 return true
809814 }
@@ -1182,7 +1187,6 @@ internal extension DarwinCentral {
11821187 let state = unsafeBitCast ( centralManager. state, to: DarwinBluetoothState . self)
11831188 log ( " Did update state \( state) " )
11841189 self . central. objectWillChange. send ( )
1185- //self.central.continuation.state?.yield(state)
11861190 }
11871191
11881192 @objc ( centralManager: didDiscoverPeripheral: advertisementData: RSSI: )
0 commit comments