feat: 添加用户行为分析功能
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user