fix: preserve sync outbox enqueue compatibility

This commit is contained in:
bponders12-cell 2026-09-01 23:13:59 -04:00
parent a67316479d
commit 3857437d57
1 changed files with 6 additions and 17 deletions

View File

@ -12,9 +12,6 @@ class SyncOutbox {
Future<void> _saveChain = Future<void>.value(); Future<void> _saveChain = Future<void>.value();
bool _isLoaded = false; bool _isLoaded = false;
/// The namespace is account-scoped. Pending work recorded before login is
/// migrated from the local namespace into the newly authenticated account
/// so offline rides do not become stranded when the user signs in later.
SyncOutbox(this._prefs, {String? storageNamespace}) SyncOutbox(this._prefs, {String? storageNamespace})
: _storageNamespace = _normalizeNamespace(storageNamespace) { : _storageNamespace = _normalizeNamespace(storageNamespace) {
load(); load();
@ -62,7 +59,6 @@ class SyncOutbox {
final localKey = '${_outboxStorageKeyPrefix}_local'; final localKey = '${_outboxStorageKeyPrefix}_local';
final rawJson = _prefs.getString(localKey); final rawJson = _prefs.getString(localKey);
if (rawJson == null || rawJson.trim().isEmpty) return; if (rawJson == null || rawJson.trim().isEmpty) return;
var migrated = 0; var migrated = 0;
try { try {
final decoded = jsonDecode(rawJson); final decoded = jsonDecode(rawJson);
@ -71,8 +67,6 @@ class SyncOutbox {
try { try {
final map = entry is Map<String, dynamic> ? entry : Map<String, dynamic>.from(entry as Map); final map = entry is Map<String, dynamic> ? entry : Map<String, dynamic>.from(entry as Map);
final item = SyncItem.fromJson(map); final item = SyncItem.fromJson(map);
// Local namespace work cannot legitimately be cloud-synced yet;
// keep only pending work and rebind it to the current provider.
if (item.status.isPending) { if (item.status.isPending) {
_items[item.key] = item.status == SyncStatus.uploading _items[item.key] = item.status == SyncStatus.uploading
? item.copyWith(status: SyncStatus.queued, clearNextRetry: true) ? item.copyWith(status: SyncStatus.queued, clearNextRetry: true)
@ -88,7 +82,6 @@ class SyncOutbox {
debugPrint('[SyncOutbox] Local outbox migration skipped: $e'); debugPrint('[SyncOutbox] Local outbox migration skipped: $e');
return; return;
} }
if (migrated > 0) { if (migrated > 0) {
_prefs.setString(storageKey, jsonEncode(_items.values.map((e) => e.toJson()).toList())); _prefs.setString(storageKey, jsonEncode(_items.values.map((e) => e.toJson()).toList()));
_prefs.remove(localKey); _prefs.remove(localKey);
@ -104,9 +97,7 @@ class SyncOutbox {
_items.remove(terminal.removeAt(0).key); _items.remove(terminal.removeAt(0).key);
} }
} }
if (_items.length > maxOutboxCapacity) { if (_items.length > maxOutboxCapacity) throw StateError('Sync outbox capacity reached with no evictable terminal items.');
throw StateError('Sync outbox capacity reached with no evictable terminal items.');
}
return _prefs.setString(storageKey, jsonEncode(_items.values.map((e) => e.toJson()).toList())); return _prefs.setString(storageKey, jsonEncode(_items.values.map((e) => e.toJson()).toList()));
}); });
_saveChain = operation.then((_) {}, onError: (_) {}); _saveChain = operation.then((_) {}, onError: (_) {});
@ -118,10 +109,10 @@ class SyncOutbox {
List<SyncItem> getPendingItems() => _items.values.where((i) => i.status.isPending).toList(growable: false); List<SyncItem> getPendingItems() => _items.values.where((i) => i.status.isPending).toList(growable: false);
List<SyncItem> getEligibleItems(DateTime now) => _items.values.where((i) => i.isEligibleForAttempt(now)).toList(growable: false); List<SyncItem> getEligibleItems(DateTime now) => _items.values.where((i) => i.isEligibleForAttempt(now)).toList(growable: false);
Future<SyncItem> enqueue(int localRideId, String providerId, {DateTime? now}) async { /// [autoProcess] is retained for source compatibility with older callers;
if (localRideId <= 0 || providerId.trim().isEmpty) { /// queue processing is owned by SyncManager.
throw ArgumentError('A positive localRideId and non-empty providerId are required.'); Future<SyncItem> enqueue(int localRideId, String providerId, {DateTime? now, bool autoProcess = true}) async {
} if (localRideId <= 0 || providerId.trim().isEmpty) throw ArgumentError('A positive localRideId and non-empty providerId are required.');
final normalizedProvider = providerId.trim(); final normalizedProvider = providerId.trim();
final key = '$normalizedProvider:$localRideId'; final key = '$normalizedProvider:$localRideId';
final existing = _items[key]; final existing = _items[key];
@ -132,9 +123,7 @@ class SyncOutbox {
await _save(); await _save();
return rearmed; return rearmed;
} }
if (_items.length >= maxOutboxCapacity && !_items.values.any((i) => i.status.isTerminal)) { if (_items.length >= maxOutboxCapacity && !_items.values.any((i) => i.status.isTerminal)) throw StateError('Sync outbox capacity reached; pending rides were not discarded.');
throw StateError('Sync outbox capacity reached; pending rides were not discarded.');
}
final item = SyncItem(localRideId: localRideId, providerId: normalizedProvider, status: SyncStatus.queued, createdAt: now ?? DateTime.now()); final item = SyncItem(localRideId: localRideId, providerId: normalizedProvider, status: SyncStatus.queued, createdAt: now ?? DateTime.now());
_items[key] = item; _items[key] = item;
await _save(); await _save();