@@ -16,6 +16,7 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
1616 private let configuration : CardPresentPaymentsConfiguration
1717 private let siteID : Int64
1818 private var locationManager : CLLocationManager = CLLocationManager ( )
19+ private static var deviceSupportsLocalMobileReader : [ Int64 : ExpiringBool ] = [ : ]
1920
2021 init ( siteID: Int64 ,
2122 configuration: CardPresentPaymentsConfiguration = CardPresentConfigurationLoader ( ) . configuration,
@@ -59,7 +60,15 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
5960
6061 @MainActor
6162 func deviceSupportsLocalMobileReader( ) async -> Bool {
62- await withCheckedContinuation { continuation in
63+ /// There may be crashes due to multiple consecutive calls checkDeviceSupport
64+ /// Limit the calls to once every 30 seconds and cache the result
65+ ///
66+ if let cachedResult = Self . deviceSupportsLocalMobileReader [ siteID] , !cachedResult. isExpired {
67+ return cachedResult. value
68+ }
69+
70+
71+ let deviceSupportsLocalMobileReader = await withCheckedContinuation { continuation in
6372 let action = CardPresentPaymentAction . checkDeviceSupport (
6473 siteID: siteID,
6574 cardReaderType: . appleBuiltIn,
@@ -69,6 +78,9 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
6978 }
7079 stores. dispatch ( action)
7180 }
81+
82+ Self . deviceSupportsLocalMobileReader [ siteID] = ExpiringBool ( value: deviceSupportsLocalMobileReader, expirationInSeconds: 30 )
83+ return deviceSupportsLocalMobileReader
7284 }
7385
7486 @MainActor
@@ -84,3 +96,15 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
8496 }
8597 }
8698}
99+
100+ // MARK: - Helper for caching local reader device support check
101+
102+ private struct ExpiringBool {
103+ let value : Bool
104+ let expirationInSeconds : Int
105+ private let created = Date ( )
106+
107+ var isExpired : Bool {
108+ Date ( ) . timeIntervalSince ( created) > Double ( expirationInSeconds)
109+ }
110+ }
0 commit comments