Skip to content

Commit fc81997

Browse files
committed
make clearOrder() async
1 parent f4d7c91 commit fc81997

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

WooCommerce/Classes/POS/Controllers/PointOfSaleOrderController.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protocol PointOfSaleOrderControllerProtocol {
3535
@discardableResult
3636
func syncOrder(for cart: Cart, retryHandler: @escaping () async -> Void) async -> Result<SyncOrderState, Error>
3737
func sendReceipt(recipientEmail: String) async throws
38-
func clearOrder()
38+
func clearOrder() async
3939
func collectCashPayment(changeDueAmount: String?) async throws
4040
}
4141

@@ -143,16 +143,20 @@ protocol PointOfSaleOrderControllerProtocol {
143143
}
144144
}
145145

146-
func clearOrder() {
147-
clearAutoDraftIfNeeded(for: order)
146+
func clearOrder() async {
147+
await clearAutoDraftIfNeeded(for: order)
148148
order = nil
149149
orderState = .idle
150150
}
151151

152-
private func clearAutoDraftIfNeeded(for order: Order?) {
153-
if let order, order.status == .autoDraft {
154-
DispatchQueue.main.async { [weak self] in
155-
let action = OrderAction.deleteOrder(siteID: order.siteID, order: order, deletePermanently: true) { _ in }
152+
private func clearAutoDraftIfNeeded(for order: Order?) async {
153+
guard let order, order.status == .autoDraft else { return }
154+
155+
await withCheckedContinuation { continuation in
156+
Task { @MainActor [weak self] in
157+
let action = OrderAction.deleteOrder(siteID: order.siteID, order: order, deletePermanently: true) { _ in
158+
continuation.resume()
159+
}
156160
self?.stores.dispatch(action)
157161
}
158162
}

WooCommerce/Classes/POS/Models/PointOfSaleAggregateModel.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ extension PointOfSaleAggregateModel {
173173

174174
func startNewCart() {
175175
removeAllItemsFromCart()
176-
orderController.clearOrder()
176+
Task {
177+
await orderController.clearOrder()
178+
}
177179
setStateForEditing()
178180
viewStateCoordinator.reset()
179181
}
@@ -621,7 +623,9 @@ extension PointOfSaleAggregateModel {
621623

622624
// Before exiting Point of Sale, we warn the merchant about losing their in-progress order.
623625
// We need to clear it down as any accidental retention can cause issues especially when reconnecting card readers.
624-
orderController.clearOrder()
626+
Task {
627+
await orderController.clearOrder()
628+
}
625629

626630
// Ideally, we could rely on the POS being deallocated to cancel all these. Since we have memory leak issues,
627631
// cancelling them explicitly helps reduce the risk of user-visible bugs while we work on the memory leaks.

WooCommerce/Classes/POS/Utils/PointOfSalePreviewOrderController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class PointOfSalePreviewOrderController: PointOfSaleOrderControllerProtocol {
1818

1919
func sendReceipt(recipientEmail: String) async throws { }
2020

21-
func clearOrder() { }
21+
func clearOrder() async { }
2222

2323
func collectCashPayment(changeDueAmount: String?) async throws {}
2424
}

WooCommerce/WooCommerceTests/POS/Controllers/PointOfSaleOrderControllerTests.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ struct PointOfSaleOrderControllerTests {
589589
}
590590

591591
// When
592-
sut.clearOrder()
592+
await sut.clearOrder()
593593

594594
// Then
595595
#expect(deleteActionWasDispatched == false)
@@ -620,8 +620,7 @@ struct PointOfSaleOrderControllerTests {
620620
}
621621

622622
// When
623-
sut.clearOrder()
624-
try await Task.sleep(nanoseconds: 100_000_000)
623+
await sut.clearOrder()
625624

626625
// Then
627626
#expect(deleteActionWasDispatched == true)
@@ -651,7 +650,7 @@ struct PointOfSaleOrderControllerTests {
651650
}
652651

653652
// When
654-
sut.clearOrder()
653+
await sut.clearOrder()
655654

656655
// Then
657656
#expect(deleteActionWasDispatched == false)

WooCommerce/WooCommerceTests/POS/Mocks/MockPointOfSaleOrderController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class MockPointOfSaleOrderController: PointOfSaleOrderControllerProtocol {
3737
}
3838

3939
var clearOrderWasCalled: Bool = false
40-
func clearOrder() {
40+
func clearOrder() async {
4141
clearOrderWasCalled = true
4242
}
4343

0 commit comments

Comments
 (0)