Skip to content

Commit 44042b0

Browse files
committed
fix cakepay order refresh so awaiters can be sure a refresh has occurred
1 parent 1a804a5 commit 44042b0

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

lib/services/cakepay/cakepay_orders_service.dart

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CakePayOrdersService extends ChangeNotifier {
1313
static const Duration defaultPollInterval = Duration(seconds: 15);
1414

1515
final Map<String, CakePayOrder> _orders = {};
16-
final Set<String> _inflight = {};
16+
final Map<String, Completer<void>> _inFlight = {};
1717
final Map<String, _Poll> _polls = {};
1818
bool _refreshingAll = false;
1919

@@ -34,26 +34,34 @@ class CakePayOrdersService extends ChangeNotifier {
3434
return list;
3535
}
3636

37-
bool isRefreshing(String orderId) => _inflight.contains(orderId);
37+
bool isRefreshing(String orderId) => _inFlight.containsKey(orderId);
3838
bool get isRefreshingAll => _refreshingAll;
3939

40-
/// Fetch a single order. No-ops if a fetch for [orderId] is already in
41-
/// flight.
40+
/// returns existing future if already in flight
4241
Future<void> refreshOne(String orderId) async {
43-
if (_inflight.contains(orderId)) return;
44-
_inflight.add(orderId);
42+
final Completer<void>? pending = _inFlight[orderId];
43+
if (pending != null) return pending.future;
44+
45+
final Completer<void> completer = Completer<void>();
46+
_inFlight[orderId] = completer;
4547
notifyListeners();
46-
try {
47-
final resp = await CakePayService.instance.client.getOrder(orderId);
48-
if (!resp.hasError && resp.value != null) {
49-
_putIfChanged(resp.value!);
48+
49+
unawaited(() async {
50+
try {
51+
final resp = await CakePayService.instance.client.getOrder(orderId);
52+
if (!resp.hasError && resp.value != null) {
53+
_putIfChanged(resp.value!);
54+
}
55+
completer.complete();
56+
} catch (e, s) {
57+
completer.completeError(e, s);
58+
} finally {
59+
_inFlight.remove(orderId);
60+
notifyListeners();
5061
}
51-
} catch (_) {
52-
// Silently leave the cached value in place.
53-
} finally {
54-
_inflight.remove(orderId);
55-
notifyListeners();
56-
}
62+
}());
63+
64+
return completer.future;
5765
}
5866

5967
/// Fetch every locally-tracked order in parallel.

0 commit comments

Comments
 (0)