Skip to content

Commit 726d023

Browse files
committed
Limit the calls to Terminal SDK for local mobile reader support to 30 seconds
1 parent 2b81a33 commit 726d023

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Settings/In-Person Payments/CardReaderSupportDeterminer.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +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: Bool] = [:]
19+
private static var deviceSupportsLocalMobileReader: [Int64: ExpiringBool] = [:]
2020

2121
init(siteID: Int64,
2222
configuration: CardPresentPaymentsConfiguration = CardPresentConfigurationLoader().configuration,
@@ -61,11 +61,10 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
6161
@MainActor
6262
func deviceSupportsLocalMobileReader() async -> Bool {
6363
/// There may be crashes due to multiple consecutive calls checkDeviceSupport
64-
/// Local mobile reader (Tap to Pay) support shouldn't change during the app lifecycle
65-
/// Only a change in operating system version can affect it which would require a restart of the app
64+
/// Limit the calls to once every 30 seconds and cache the result
6665
///
67-
if let cachedResult = Self.deviceSupportsLocalMobileReader[siteID] {
68-
return cachedResult
66+
if let cachedResult = Self.deviceSupportsLocalMobileReader[siteID], !cachedResult.isExpired {
67+
return cachedResult.value
6968
}
7069

7170

@@ -80,7 +79,7 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
8079
stores.dispatch(action)
8180
}
8281

83-
Self.deviceSupportsLocalMobileReader[siteID] = deviceSupportsLocalMobileReader
82+
Self.deviceSupportsLocalMobileReader[siteID] = ExpiringBool(value: deviceSupportsLocalMobileReader, expirationInSeconds: 30)
8483
return deviceSupportsLocalMobileReader
8584
}
8685

@@ -97,3 +96,15 @@ final class CardReaderSupportDeterminer: CardReaderSupportDetermining {
9796
}
9897
}
9998
}
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

Comments
 (0)