-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp_diagnostics_demo_service.dart
More file actions
61 lines (50 loc) · 1.7 KB
/
app_diagnostics_demo_service.dart
File metadata and controls
61 lines (50 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import 'package:faro_example/shared/models/demo_log_entry.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
typedef AppDiagnosticsLogCallback =
void Function(String message, {DemoLogTone tone});
final appDiagnosticsDemoServiceProvider = Provider<AppDiagnosticsDemoService>(
(ref) => const AppDiagnosticsDemoService(),
);
/// Runs the error and ANR demos shown in the example app.
class AppDiagnosticsDemoService {
const AppDiagnosticsDemoService();
void triggerUnhandledError(AppDiagnosticsLogCallback log) {
log(
'Scheduling an unhandled Error on the async queue.',
tone: DemoLogTone.warning,
);
Future<void>.delayed(const Duration(milliseconds: 10), () {
throw UnsupportedError('This is an Error!');
});
}
void triggerUnhandledException(AppDiagnosticsLogCallback log) {
log(
'Scheduling an unhandled Exception on the async queue.',
tone: DemoLogTone.warning,
);
Future<void>.delayed(const Duration(milliseconds: 10), () {
throw Exception('This is an Exception!');
});
}
Future<void> simulateAnr({
required int seconds,
required AppDiagnosticsLogCallback log,
}) async {
log(
'Blocking the main thread for $seconds seconds.',
tone: DemoLogTone.warning,
);
// Yield once so the warning can paint before the UI freezes.
await Future<void>.delayed(const Duration(milliseconds: 10));
final startTime = DateTime.now();
while (DateTime.now().difference(startTime).inSeconds < seconds) {
for (int i = 0; i < 10000000; i++) {
final _ = i * i * i;
}
}
log(
'ANR simulation completed after $seconds seconds.',
tone: DemoLogTone.highlight,
);
}
}