Skip to content

Commit 33f29a8

Browse files
committed
Fix POS stuck in loading state when tapping POS tab before tab visibility / site settings are refreshed where there are multiple subscribers to the same async stream and only the first one gets the value.
1 parent 9feda59 commit 33f29a8

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

WooCommerce/Classes/ViewRelated/Dashboard/Settings/POS/POSTabEligibilityChecker.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ protocol POSEntryPointEligibilityCheckerProtocol {
4747
final class POSTabEligibilityChecker: POSEntryPointEligibilityCheckerProtocol {
4848
private var siteSettingsEligibility: POSEligibilityState?
4949
private var featureFlagEligibility: POSEligibilityState?
50+
private var siteSettingsTask: Task<[SiteSetting], Never>?
5051

5152
private let siteID: Int64
5253
private let userInterfaceIdiom: UIUserInterfaceIdiom
@@ -238,14 +239,28 @@ private extension POSTabEligibilityChecker {
238239
}
239240

240241
func waitForSiteSettingsRefresh() async -> [SiteSetting] {
241-
for await siteSettings in siteSettings.settingsStream {
242-
guard siteSettings.siteID == siteID, siteSettings.settings.isNotEmpty, siteSettings.source != .initialLoad else {
243-
continue
242+
// Uses a shared task so that multiple calls can await the same result since site settings can be emitted only once.
243+
if let existingTask = siteSettingsTask {
244+
return await existingTask.value
245+
}
246+
247+
let task = Task<[SiteSetting], Never> { [weak self] in
248+
guard let self else { return [] }
249+
250+
for await siteSettings in siteSettings.settingsStream {
251+
guard siteSettings.siteID == self.siteID, siteSettings.settings.isNotEmpty, siteSettings.source != .initialLoad else {
252+
continue
253+
}
254+
siteSettingsTask = nil
255+
return siteSettings.settings
244256
}
245-
return siteSettings.settings
257+
// If we get here, the stream completed without yielding any values for our site ID which is unexpected.
258+
siteSettingsTask = nil
259+
return []
246260
}
247-
// If we get here, the stream completed without yielding any values for our site ID which is unexpected.
248-
return []
261+
262+
siteSettingsTask = task
263+
return await task.value
249264
}
250265

251266
func isEligibleFromCountryAndCurrencyCode(countryCode: CountryCode, currencyCode: CurrencyCode) -> POSEligibilityState {

0 commit comments

Comments
 (0)