Skip to content

Commit 1a23559

Browse files
committed
Handle empty storage by waiting for remote
1 parent 3f240a2 commit 1a23559

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

Yosemite/Yosemite/PointOfSale/PointOfSaleCouponService.swift

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,37 @@ public final class PointOfSaleCouponService: PointOfSaleItemServiceProtocol {
7878
public func providePointOfSaleItems(pageNumber: Int) async throws -> PagedItems<POSItem> {
7979
let coupons = await providePointOfSaleCoupons()
8080

81-
syncCouponsFromRemote(pageNumber: pageNumber)
82-
83-
return .init(items: coupons, hasMorePages: false)
81+
if !coupons.isEmpty {
82+
// Fire-and-forget sync
83+
Task.detached {
84+
await self.syncCouponsFromRemote(pageNumber: pageNumber)
85+
}
86+
return .init(items: coupons, hasMorePages: false)
87+
} else {
88+
// Wait for the sync to complete
89+
await syncCouponsFromRemote(pageNumber: pageNumber)
90+
let refreshedCoupons = await providePointOfSaleCoupons()
91+
return .init(items: refreshedCoupons, hasMorePages: false)
92+
}
8493
}
8594

86-
private func syncCouponsFromRemote(pageNumber: Int) {
95+
private func syncCouponsFromRemote(pageNumber: Int) async {
8796
guard let stores = stores else {
8897
return
8998
}
90-
let action = CouponAction.synchronizeCoupons(siteID: siteID,
91-
pageNumber: pageNumber,
92-
pageSize: 25,
93-
onCompletion: { _ in })
94-
stores.dispatch(action)
99+
100+
await withCheckedContinuation { continuation in
101+
let action = CouponAction.synchronizeCoupons(
102+
siteID: siteID,
103+
pageNumber: pageNumber,
104+
pageSize: 25,
105+
onCompletion: { _ in
106+
continuation.resume()
107+
}
108+
)
109+
Task { @MainActor in
110+
stores.dispatch(action)
111+
}
112+
}
95113
}
96114
}

0 commit comments

Comments
 (0)