@@ -15,6 +15,7 @@ public final class StripeCardReaderService: NSObject {
1515 private let discoveryStatusSubject = CurrentValueSubject < CardReaderServiceDiscoveryStatus , Never > ( . idle)
1616 private let readerEventsSubject = PassthroughSubject < CardReaderEvent , Never > ( )
1717 private let softwareUpdateSubject = CurrentValueSubject < CardReaderSoftwareUpdateState , Never > ( . none)
18+ private var connectionAttemptInvalidated : Bool = false
1819
1920 /// Volatile, in-memory cache of discovered readers. It has to be cleared after we connect to a reader
2021 /// see
@@ -287,6 +288,10 @@ extension StripeCardReaderService: CardReaderService {
287288 return
288289 }
289290
291+ if Terminal . shared. connectionStatus == . connecting {
292+ connectionAttemptInvalidated = true
293+ }
294+
290295 Terminal . shared. clearCachedCredentials ( )
291296 }
292297
@@ -354,6 +359,7 @@ extension StripeCardReaderService: CardReaderService {
354359 } . eraseToAnyPublisher ( )
355360 }
356361
362+ connectionAttemptInvalidated = false
357363 switch stripeReader. deviceType {
358364 case . appleBuiltIn:
359365 return getLocalMobileConfiguration ( stripeReader, options: options) . flatMap { configuration in
@@ -448,6 +454,10 @@ extension StripeCardReaderService: CardReaderService {
448454 }
449455
450456 if let reader = reader {
457+ if self . connectionAttemptInvalidated {
458+ _ = self . disconnect ( )
459+ promise ( . failure( CardReaderServiceError . connection ( underlyingError: . connectionAttemptInvalidated) ) )
460+ }
451461 self . connectedReadersSubject. send ( [ CardReader ( reader: reader) ] )
452462 self . switchStatusToIdle ( )
453463 promise ( . success( CardReader ( reader: reader) ) )
@@ -482,6 +492,10 @@ extension StripeCardReaderService: CardReaderService {
482492 }
483493
484494 if let reader = reader {
495+ if self . connectionAttemptInvalidated {
496+ _ = self . disconnect ( )
497+ promise ( . failure( CardReaderServiceError . connection ( underlyingError: . connectionAttemptInvalidated) ) )
498+ }
485499 self . connectedReadersSubject. send ( [ CardReader ( reader: reader) ] )
486500 self . switchStatusToIdle ( )
487501 promise ( . success( CardReader ( reader: reader) ) )
0 commit comments