43 lines
1.1 KiB
Dart
43 lines
1.1 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/scheduler.dart';
|
|
|
|
class TerminalDiagnosticLog extends ChangeNotifier {
|
|
TerminalDiagnosticLog({this.maxEntries = 40});
|
|
|
|
final int maxEntries;
|
|
final List<String> _entries = <String>[];
|
|
bool _notificationScheduled = false;
|
|
|
|
List<String> get entries => List.unmodifiable(_entries);
|
|
|
|
void add(String event, [String? detail]) {
|
|
final message = detail == null || detail.isEmpty
|
|
? event
|
|
: '$event | $detail';
|
|
_entries.insert(0, message);
|
|
if (_entries.length > maxEntries) {
|
|
_entries.removeLast();
|
|
}
|
|
_notifySafely();
|
|
}
|
|
|
|
void _notifySafely() {
|
|
final schedulerPhase = SchedulerBinding.instance.schedulerPhase;
|
|
if (schedulerPhase == SchedulerPhase.idle ||
|
|
schedulerPhase == SchedulerPhase.postFrameCallbacks) {
|
|
notifyListeners();
|
|
return;
|
|
}
|
|
|
|
if (_notificationScheduled) {
|
|
return;
|
|
}
|
|
|
|
_notificationScheduled = true;
|
|
SchedulerBinding.instance.addPostFrameCallback((_) {
|
|
_notificationScheduled = false;
|
|
notifyListeners();
|
|
});
|
|
}
|
|
}
|