feat: 添加用户行为分析功能

This commit is contained in:
qzl
2026-04-01 18:35:32 +08:00
parent 24eda6ff51
commit 2cdf075e92
25 changed files with 1321 additions and 5 deletions
@@ -0,0 +1,46 @@
import 'dart:async';
import '../events/events.dart';
class EventQueue {
final List<BaseAnalyticsEvent> _queue = [];
final int maxSize;
final Duration flushInterval;
final void Function(List<BaseAnalyticsEvent>) onFlush;
Timer? _timer;
EventQueue({
this.maxSize = 50,
this.flushInterval = const Duration(seconds: 30),
required this.onFlush,
});
void start() {
_timer?.cancel();
_timer = Timer.periodic(flushInterval, (_) => _tryFlush());
}
void stop() {
_timer?.cancel();
_timer = null;
}
void add(BaseAnalyticsEvent event) {
_queue.add(event);
if (_queue.length >= maxSize) {
_tryFlush();
}
}
void _tryFlush() {
if (_queue.isEmpty) return;
final events = List<BaseAnalyticsEvent>.from(_queue);
_queue.clear();
onFlush(events);
}
List<BaseAnalyticsEvent> get pendingEvents => List.unmodifiable(_queue);
int get pendingCount => _queue.length;
}