flush method
Fuerza el envío de todos los eventos pendientes
Si force es true, envía eventos incluso si el modo debug está habilitado
Implementation
Future<void> flush({bool force = false}) async {
if (!_isStarted) return;
// Verificar si el envío está activo (excepto si es forzado)
if (!_sendActive && !force) {
ObslyLogger.debug('Send is disabled - events will not be sent to server (use force=true to override)');
return;
}
try {
final events = await storage.getAllEvents();
if (events.isEmpty) {
ObslyLogger.debug('No pending events to flush');
return;
}
ObslyLogger.debug('Flushing ${events.length} pending events');
// Enviar eventos en lotes
const batchSize = 50;
for (int i = 0; i < events.length; i += batchSize) {
final batch = events.skip(i).take(batchSize).toList();
try {
final eventsToSend = batch.map((e) => ObslyEvent.fromJson(e)).toList();
final success = await sender.sendBatch(eventsToSend);
if (success) {
// Eliminar eventos enviados del storage
await storage.deleteEventsByMap(batch.cast<Map<String, dynamic>>());
}
} catch (e, stackTrace) {
ObslyLogger.error('Error sending batch: $e', e, stackTrace);
}
}
} catch (e, stackTrace) {
ObslyLogger.error('Error during flush: $e', e, stackTrace);
}
}