flushQueue method

Future<int> flushQueue()

Attempts to deliver all queued entries via the underlying backend.

Stops at the first failure and preserves remaining entries for the next attempt. Clears the queue only when all entries are sent successfully.

Returns the number of successfully delivered entries.

Implementation

Future<int> flushQueue() async {
  final online = await _connectivity.isOnline();
  if (!online) return 0;

  final entries = await _queue.pending();
  if (entries.isEmpty) return 0;

  var sent = 0;
  for (final entry in entries) {
    try {
      await _backend.submit(entry);
      sent++;
    } catch (_) {
      break;
    }
  }

  if (sent == entries.length) {
    await _queue.clear();
  }
  return sent;
}