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();
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})
: _storageNamespace = _normalizeNamespace(storageNamespace) {
load();
@ -62,7 +59,6 @@ class SyncOutbox {
final localKey = '${_outboxStorageKeyPrefix}_local';
final rawJson = _prefs.getString(localKey);
if (rawJson == null || rawJson.trim().isEmpty) return;
var migrated = 0;
try {
final decoded = jsonDecode(rawJson);
@ -71,8 +67,6 @@ class SyncOutbox {
try {
final map = entry is Map<String, dynamic> ? entry : Map<String, dynamic>.from(entry as 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) {
_items[item.key] = item.status == SyncStatus.uploading
? item.copyWith(status: SyncStatus.queued, clearNextRetry: true)
@ -88,7 +82,6 @@ class SyncOutbox {
debugPrint('[SyncOutbox] Local outbox migration skipped: $e');
return;
}
if (migrated > 0) {
_prefs.setString(storageKey, jsonEncode(_items.values.map((e) => e.toJson()).toList()));
_prefs.remove(localKey);
@ -104,9 +97,7 @@ class SyncOutbox {
_items.remove(terminal.removeAt(0).key);
}
}
if (_items.length > maxOutboxCapacity) {
throw StateError('Sync outbox capacity reached with no evictable terminal items.');
}
if (_items.length > maxOutboxCapacity) throw StateError('Sync outbox capacity reached with no evictable terminal items.');
return _prefs.setString(storageKey, jsonEncode(_items.values.map((e) => e.toJson()).toList()));
});
_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> getEligibleItems(DateTime now) => _items.values.where((i) => i.isEligibleForAttempt(now)).toList(growable: false);
Future<SyncItem> enqueue(int localRideId, String providerId, {DateTime? now}) async {
if (localRideId <= 0 || providerId.trim().isEmpty) {
throw ArgumentError('A positive localRideId and non-empty providerId are required.');
}
/// [autoProcess] is retained for source compatibility with older callers;
/// queue processing is owned by SyncManager.
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 key = '$normalizedProvider:$localRideId';
final existing = _items[key];
@ -132,9 +123,7 @@ class SyncOutbox {
await _save();
return rearmed;
}
if (_items.length >= maxOutboxCapacity && !_items.values.any((i) => i.status.isTerminal)) {
throw StateError('Sync outbox capacity reached; pending rides were not discarded.');
}
if (_items.length >= maxOutboxCapacity && !_items.values.any((i) => i.status.isTerminal)) 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());
_items[key] = item;
await _save();